From d9a2e9d57c879720489a22bc94d8b979026a5424 Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Wed, 8 Sep 2021 00:18:33 +0200 Subject: [PATCH] use pure xml.etree method to parse XML (fixes #32) --- deefuzzer/core.py | 2 +- deefuzzer/tools/__init__.py | 2 +- deefuzzer/tools/utils.py | 11 ++++++----- deefuzzer/tools/xmltodict3.py | 26 ++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 deefuzzer/tools/xmltodict3.py diff --git a/deefuzzer/core.py b/deefuzzer/core.py index 2b87a88..1126ba9 100644 --- a/deefuzzer/core.py +++ b/deefuzzer/core.py @@ -271,7 +271,7 @@ class DeeFuzzer(Thread): try: if 'station_instance' in self.station_settings[i]: # Check for station running here - if self.station_settings[i]['station_instance'].isAlive(): + if self.station_settings[i]['station_instance'].is_alive: # Station exists and is alive. Don't recreate. self.station_settings[i]['retries'] = 0 continue diff --git a/deefuzzer/tools/__init__.py b/deefuzzer/tools/__init__.py index 0ccf127..f68aba8 100644 --- a/deefuzzer/tools/__init__.py +++ b/deefuzzer/tools/__init__.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from .xmltodict import * +from .xmltodict3 import * from .PyRSS2Gen import * from .mediabase import * from .mp3 import * diff --git a/deefuzzer/tools/utils.py b/deefuzzer/tools/utils.py index 7b74f07..7b70857 100644 --- a/deefuzzer/tools/utils.py +++ b/deefuzzer/tools/utils.py @@ -15,8 +15,9 @@ import re import string import mimetypes from itertools import chain +from xml.etree import cElementTree as ElementTree from deefuzzer.tools import * -import xmltodict + mimetypes.add_type('application/x-yaml', '.yaml') @@ -88,10 +89,10 @@ def get_conf_dict(file): # Do the type check first, so we don't load huge files that won't be used if 'xml' in mime_type: - confile = open(file, 'r') - data = confile.read() - confile.close() - return xmltodict.parse(data) + tree = ElementTree.parse(file) + root = tree.getroot() + xmldict = etree_to_dict(root) + return xmldict elif 'yaml' in mime_type or 'yml' in mime_type: import yaml diff --git a/deefuzzer/tools/xmltodict3.py b/deefuzzer/tools/xmltodict3.py new file mode 100644 index 0000000..9715061 --- /dev/null +++ b/deefuzzer/tools/xmltodict3.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# taken from https://stackoverflow.com/questions/2148119/how-to-convert-an-xml-string-to-a-dictionary +# thanks to K3---rnc + + +from collections import defaultdict + +def etree_to_dict(t): + d = {t.tag: {} if t.attrib else None} + children = list(t) + if children: + dd = defaultdict(list) + for dc in map(etree_to_dict, children): + for k, v in dc.items(): + dd[k].append(v) + d = {t.tag: {k:v[0] if len(v) == 1 else v for k, v in dd.items()}} + if t.attrib: + d[t.tag].update(('@' + k, v) for k, v in t.attrib.items()) + if t.text: + text = t.text.strip() + if children or t.attrib: + if text: + d[t.tag]['#text'] = text + else: + d[t.tag] = text + return d \ No newline at end of file -- 2.47.3