| Total Complexity | 8 |
| Total Lines | 51 |
| Duplicated Lines | 25.49 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 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): |
|
|
|
|||
| 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 |