Passed
Pull Request — master (#317)
by Jaspar
01:18
created

gvmtools.script_utils.create_xml_tree()   A

Complexity

Conditions 4

Size

Total Lines 13
Code Lines 11

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nop 1
dl 13
loc 13
rs 9.85
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018-2020 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
from sys import stderr
20
from lxml import etree
21
22
23
def yes_or_no(question):
24
    reply = str(input(question + ' (y/n): ')).lower().strip()
25
    if reply[0] == ('y'):
26
        return True
27
    if reply[0] == ('n'):
28
        return False
29
    else:
30
        return yes_or_no("Please enter 'y' or 'n'")
31
32
33
def error_and_exit(msg):
34
    print("\nError: {}\n".format(msg), file=stderr)
35
    quit(1)
36
37
38 View Code Duplication
def create_xml_tree(xml_doc):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
39
    try:
40
        xml_tree = etree.parse(xml_doc)
41
        xml_tree = xml_tree.getroot()
42
    except IOError as err:
43
        error_and_exit("Failed to read xml_file: {} (exit)".format(str(err)))
44
    except etree.Error as err:
45
        error_and_exit("Failed to parse xml_file: {} (exit)".format(str(err)))
46
47
    if len(xml_tree) == 0:
48
        error_and_exit("XML file is empty (exit)")
49
50
    return xml_tree
51