Cleanup save handling of xml.etree.ElementTree

This commit is contained in:
croneter 2020-12-27 13:13:01 +01:00
parent 0dda58ebd3
commit 1f2b19ce42
5 changed files with 10 additions and 9 deletions

View file

@ -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 iterparse as _iterparse
from xml.etree.ElementTree import tostring 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): class UnicodeXMLParser(DefusedXMLParser):
""" """

View file

@ -262,7 +262,7 @@ class DownloadUtils(object):
return r return r
try: try:
# xml response # xml response
r = utils.defused_etree.fromstring(r.content) r = utils.etree.fromstring(r.content)
return r return r
except Exception: except Exception:
r.encoding = 'utf-8' r.encoding = 'utf-8'

View file

@ -10,10 +10,8 @@ from unicodedata import normalize
from threading import Lock from threading import Lock
import urllib import urllib
# Originally tried faster cElementTree, but does NOT work reliably with Kodi # 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 # etree parse unsafe; make sure we're always receiving unicode
from . import defused_etree from . import defused_etree as etree
from xml.etree.ElementTree import ParseError
from functools import wraps from functools import wraps
import re import re
import gc import gc
@ -697,7 +695,7 @@ class XmlKodiSetting(object):
def __enter__(self): def __enter__(self):
try: try:
self.tree = defused_etree.parse(self.path) self.tree = etree.parse(self.path)
except IOError: except IOError:
# Document is blank or missing # Document is blank or missing
if self.force_create is False: if self.force_create is False:
@ -707,14 +705,14 @@ class XmlKodiSetting(object):
# Create topmost xml entry # Create topmost xml entry
self.tree = etree.ElementTree(etree.Element(self.top_element)) self.tree = etree.ElementTree(etree.Element(self.top_element))
self.write_xml = True self.write_xml = True
except ParseError: except etree.ParseError:
LOG.error('Error parsing %s', self.path) LOG.error('Error parsing %s', self.path)
# "Kodi cannot parse {0}. PKC will not function correctly. Please # "Kodi cannot parse {0}. PKC will not function correctly. Please
# visit {1} and correct your file!" # visit {1} and correct your file!"
messageDialog(lang(29999), lang(39716).format( messageDialog(lang(29999), lang(39716).format(
self.filename, self.filename,
'http://kodi.wiki')) '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() self.root = self.tree.getroot()
return self return self

View file

@ -242,7 +242,7 @@ class Alexa_Websocket(WebSocket):
self.__class__.__name__) self.__class__.__name__)
LOG.debug('%s: %s', self.__class__.__name__, message) LOG.debug('%s: %s', self.__class__.__name__, message)
try: try:
message = utils.defused_etree.fromstring(message) message = utils.etree.fromstring(message)
except Exception as ex: except Exception as ex:
LOG.error('%s: Error decoding message from Alexa: %s', LOG.error('%s: Error decoding message from Alexa: %s',
self.__class__.__name__, ex) self.__class__.__name__, ex)

View file

@ -22,7 +22,7 @@ PATH = path_ops.translate_path('special://userdata/')
def get_etree(topelement): def get_etree(topelement):
try: try:
xml = utils.defused_etree.parse( xml = utils.etree.parse(
path_ops.path.join(PATH, '%s.xml' % topelement)) path_ops.path.join(PATH, '%s.xml' % topelement))
except IOError: except IOError:
# Document is blank or missing # Document is blank or missing