Catch ParseError from defusedxml correctly

This commit is contained in:
croneter 2018-10-14 12:15:09 +02:00
parent 76fbf3ac83
commit 02b20a9b74
3 changed files with 12 additions and 11 deletions

View file

@ -494,7 +494,7 @@ class InitialSetup(object):
xml.set_setting(['video', 'ignoresecondsatstart'], xml.set_setting(['video', 'ignoresecondsatstart'],
value='60') value='60')
reboot = xml.write_xml reboot = xml.write_xml
except etree.ParseError: except utils.ParseError:
cache = None cache = None
reboot = False reboot = False
# Kodi default cache if no setting is set # Kodi default cache if no setting is set
@ -529,7 +529,7 @@ class InitialSetup(object):
etree.SubElement(source, 'allowsharing').text = "true" etree.SubElement(source, 'allowsharing').text = "true"
if reboot is False: if reboot is False:
reboot = xml.write_xml reboot = xml.write_xml
except etree.ParseError: except utils.ParseError:
pass pass
# Do we need to migrate stuff? # Do we need to migrate stuff?

View file

@ -65,7 +65,7 @@ def excludefromscan_music_folders(xml):
element.text) element.text)
parent.remove(element) parent.remove(element)
xml_file.write_xml = True xml_file.write_xml = True
except (utils.etree.ParseError, IOError): except (utils.ParseError, IOError):
LOG.error('Could not adjust advancedsettings.xml') LOG.error('Could not adjust advancedsettings.xml')
if reboot is True: if reboot is True:
# 'New Plex music library detected. Sorry, but we need to # 'New Plex music library detected. Sorry, but we need to

View file

@ -12,10 +12,12 @@ from unicodedata import normalize
try: try:
import xml.etree.cElementTree as etree import xml.etree.cElementTree as etree
import defusedxml.cElementTree as defused_etree # etree parse unsafe import defusedxml.cElementTree as defused_etree # etree parse unsafe
from xml.etree.ElementTree import ParseError
ETREE = 'cElementTree' ETREE = 'cElementTree'
except ImportError: except ImportError:
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
import defusedxml.ElementTree as defused_etree # etree parse unsafe import defusedxml.ElementTree as defused_etree # etree parse unsafe
from xml.etree.ElementTree import ParseError
ETREE = 'ElementTree' ETREE = 'ElementTree'
from functools import wraps, partial from functools import wraps, partial
from urllib import quote_plus from urllib import quote_plus
@ -653,7 +655,7 @@ class XmlKodiSetting(object):
Raises IOError if the file does not exist or is empty and force_create Raises IOError if the file does not exist or is empty and force_create
has been set to False. has been set to False.
Raises etree.ParseError if the file could not be parsed by etree Raises utils.ParseError if the file could not be parsed by etree
xml.write_xml Set to True if we need to write the XML to disk xml.write_xml Set to True if we need to write the XML to disk
""" """
@ -678,19 +680,18 @@ class XmlKodiSetting(object):
if self.force_create is False: if self.force_create is False:
LOG.debug('%s does not seem to exist; not creating', self.path) LOG.debug('%s does not seem to exist; not creating', self.path)
# This will abort __enter__ # This will abort __enter__
self.__exit__(IOError, None, None) self.__exit__(IOError('File not found'), None, None)
# Create topmost xml entry # Create topmost xml entry
self.tree = etree.ElementTree( self.tree = etree.ElementTree(etree.Element(self.top_element))
element=etree.Element(self.top_element))
self.write_xml = True self.write_xml = True
except etree.ParseError: except 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__(etree.ParseError, None, None) self.__exit__(ParseError('Error parsing XML'), None, None)
self.root = self.tree.getroot() self.root = self.tree.getroot()
return self return self
@ -703,7 +704,7 @@ class XmlKodiSetting(object):
# Indent and make readable # Indent and make readable
indent(self.root) indent(self.root)
# Safe the changed xml # Safe the changed xml
self.tree.write(self.path, encoding="UTF-8") self.tree.write(self.path, encoding='utf-8')
def _is_empty(self, element, empty_elements): def _is_empty(self, element, empty_elements):
empty = True empty = True
@ -836,7 +837,7 @@ def passwords_xml():
# Document is blank or missing # Document is blank or missing
root = etree.Element('passwords') root = etree.Element('passwords')
skip_find = True skip_find = True
except etree.ParseError: except ParseError:
LOG.error('Error parsing %s', xmlpath) LOG.error('Error parsing %s', xmlpath)
# "Kodi cannot parse {0}. PKC will not function correctly. Please visit # "Kodi cannot parse {0}. PKC will not function correctly. Please visit
# {1} and correct your file!" # {1} and correct your file!"