From 1f2b19ce424fea8fc8a525d72c2db9295ab6f90a Mon Sep 17 00:00:00 2001 From: croneter Date: Sun, 27 Dec 2020 13:13:01 +0100 Subject: [PATCH] Cleanup save handling of xml.etree.ElementTree --- resources/lib/defused_etree.py | 3 +++ resources/lib/downloadutils.py | 2 +- resources/lib/utils.py | 10 ++++------ resources/lib/websocket_client.py | 2 +- resources/lib/windows/direct_path_sources.py | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/resources/lib/defused_etree.py b/resources/lib/defused_etree.py index 773e4c74..a3c2c1ab 100644 --- a/resources/lib/defused_etree.py +++ b/resources/lib/defused_etree.py @@ -12,6 +12,9 @@ from xml.etree.ElementTree import parse as _parse from xml.etree.ElementTree import iterparse as _iterparse from xml.etree.ElementTree import tostring +# Enable creation of new xmls and xml elements +from xml.etree.ElementTree import ElementTree, Element, SubElement, ParseError + class UnicodeXMLParser(DefusedXMLParser): """ diff --git a/resources/lib/downloadutils.py b/resources/lib/downloadutils.py index fdc05917..4914016a 100644 --- a/resources/lib/downloadutils.py +++ b/resources/lib/downloadutils.py @@ -262,7 +262,7 @@ class DownloadUtils(object): return r try: # xml response - r = utils.defused_etree.fromstring(r.content) + r = utils.etree.fromstring(r.content) return r except Exception: r.encoding = 'utf-8' diff --git a/resources/lib/utils.py b/resources/lib/utils.py index 355c0801..e7a62ccc 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -10,10 +10,8 @@ from unicodedata import normalize from threading import Lock import urllib # Originally tried faster cElementTree, but does NOT work reliably with Kodi -import xml.etree.ElementTree as etree # etree parse unsafe; make sure we're always receiving unicode -from . import defused_etree -from xml.etree.ElementTree import ParseError +from . import defused_etree as etree from functools import wraps import re import gc @@ -697,7 +695,7 @@ class XmlKodiSetting(object): def __enter__(self): try: - self.tree = defused_etree.parse(self.path) + self.tree = etree.parse(self.path) except IOError: # Document is blank or missing if self.force_create is False: @@ -707,14 +705,14 @@ class XmlKodiSetting(object): # Create topmost xml entry self.tree = etree.ElementTree(etree.Element(self.top_element)) self.write_xml = True - except ParseError: + except etree.ParseError: LOG.error('Error parsing %s', self.path) # "Kodi cannot parse {0}. PKC will not function correctly. Please # visit {1} and correct your file!" messageDialog(lang(29999), lang(39716).format( self.filename, 'http://kodi.wiki')) - self.__exit__(ParseError('Error parsing XML'), None, None) + self.__exit__(etree.ParseError('Error parsing XML'), None, None) self.root = self.tree.getroot() return self diff --git a/resources/lib/websocket_client.py b/resources/lib/websocket_client.py index b0468617..f61d9f6f 100644 --- a/resources/lib/websocket_client.py +++ b/resources/lib/websocket_client.py @@ -242,7 +242,7 @@ class Alexa_Websocket(WebSocket): self.__class__.__name__) LOG.debug('%s: %s', self.__class__.__name__, message) try: - message = utils.defused_etree.fromstring(message) + message = utils.etree.fromstring(message) except Exception as ex: LOG.error('%s: Error decoding message from Alexa: %s', self.__class__.__name__, ex) diff --git a/resources/lib/windows/direct_path_sources.py b/resources/lib/windows/direct_path_sources.py index d7baa287..44a098a3 100644 --- a/resources/lib/windows/direct_path_sources.py +++ b/resources/lib/windows/direct_path_sources.py @@ -22,7 +22,7 @@ PATH = path_ops.translate_path('special://userdata/') def get_etree(topelement): try: - xml = utils.defused_etree.parse( + xml = utils.etree.parse( path_ops.path.join(PATH, '%s.xml' % topelement)) except IOError: # Document is blank or missing