Update xmltodict==0.14.2

This commit is contained in:
JonnyWong16 2024-11-16 14:42:31 -08:00
commit 1eda731b08
No known key found for this signature in database
GPG key ID: B1F1F9807184697A

View file

@ -1,19 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
"Makes working with XML feel like you are working with JSON" "Makes working with XML feel like you are working with JSON"
try: from xml.parsers import expat
from defusedexpat import pyexpat as expat
except ImportError:
from xml.parsers import expat
from xml.sax.saxutils import XMLGenerator from xml.sax.saxutils import XMLGenerator
from xml.sax.xmlreader import AttributesImpl from xml.sax.xmlreader import AttributesImpl
try: # pragma no cover from io import StringIO
from cStringIO import StringIO
except ImportError: # pragma no cover
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
_dict = dict _dict = dict
import platform import platform
@ -22,17 +13,8 @@ if tuple(map(int, platform.python_version_tuple()[:2])) < (3, 7):
from inspect import isgenerator from inspect import isgenerator
try: # pragma no cover
_basestring = basestring
except NameError: # pragma no cover
_basestring = str
try: # pragma no cover
_unicode = unicode
except NameError: # pragma no cover
_unicode = str
__author__ = 'Martin Blech' __author__ = 'Martin Blech'
__version__ = '0.13.0' __version__ = "0.14.2"
__license__ = 'MIT' __license__ = 'MIT'
@ -40,7 +22,7 @@ class ParsingInterrupted(Exception):
pass pass
class _DictSAXHandler(object): class _DictSAXHandler:
def __init__(self, def __init__(self,
item_depth=0, item_depth=0,
item_callback=lambda *args: True, item_callback=lambda *args: True,
@ -107,7 +89,7 @@ class _DictSAXHandler(object):
attrs['xmlns'] = self.namespace_declarations attrs['xmlns'] = self.namespace_declarations
self.namespace_declarations = self.dict_constructor() self.namespace_declarations = self.dict_constructor()
self.path.append((name, attrs or None)) self.path.append((name, attrs or None))
if len(self.path) > self.item_depth: if len(self.path) >= self.item_depth:
self.stack.append((self.item, self.data)) self.stack.append((self.item, self.data))
if self.xml_attribs: if self.xml_attribs:
attr_entries = [] attr_entries = []
@ -135,7 +117,7 @@ class _DictSAXHandler(object):
should_continue = self.item_callback(self.path, item) should_continue = self.item_callback(self.path, item)
if not should_continue: if not should_continue:
raise ParsingInterrupted() raise ParsingInterrupted
if self.stack: if self.stack:
data = (None if not self.data data = (None if not self.data
else self.cdata_separator.join(self.data)) else self.cdata_separator.join(self.data))
@ -335,9 +317,8 @@ def parse(xml_input, encoding=None, expat=expat, process_namespaces=False,
""" """
handler = _DictSAXHandler(namespace_separator=namespace_separator, handler = _DictSAXHandler(namespace_separator=namespace_separator,
**kwargs) **kwargs)
if isinstance(xml_input, _unicode): if isinstance(xml_input, str):
if not encoding: encoding = encoding or 'utf-8'
encoding = 'utf-8'
xml_input = xml_input.encode(encoding) xml_input = xml_input.encode(encoding)
if not process_namespaces: if not process_namespaces:
namespace_separator = None namespace_separator = None
@ -372,8 +353,8 @@ def parse(xml_input, encoding=None, expat=expat, process_namespaces=False,
parser.ParseFile(xml_input) parser.ParseFile(xml_input)
elif isgenerator(xml_input): elif isgenerator(xml_input):
for chunk in xml_input: for chunk in xml_input:
parser.Parse(chunk,False) parser.Parse(chunk, False)
parser.Parse(b'',True) parser.Parse(b'', True)
else: else:
parser.Parse(xml_input, True) parser.Parse(xml_input, True)
return handler.item return handler.item
@ -412,9 +393,7 @@ def _emit(key, value, content_handler,
if result is None: if result is None:
return return
key, value = result key, value = result
if (not hasattr(value, '__iter__') if not hasattr(value, '__iter__') or isinstance(value, (str, dict)):
or isinstance(value, _basestring)
or isinstance(value, dict)):
value = [value] value = [value]
for index, v in enumerate(value): for index, v in enumerate(value):
if full_document and depth == 0 and index > 0: if full_document and depth == 0 and index > 0:
@ -422,16 +401,13 @@ def _emit(key, value, content_handler,
if v is None: if v is None:
v = _dict() v = _dict()
elif isinstance(v, bool): elif isinstance(v, bool):
if v: v = 'true' if v else 'false'
v = _unicode('true') elif not isinstance(v, (dict, str)):
else: if expand_iter and hasattr(v, '__iter__'):
v = _unicode('false')
elif not isinstance(v, dict):
if expand_iter and hasattr(v, '__iter__') and not isinstance(v, _basestring):
v = _dict(((expand_iter, v),)) v = _dict(((expand_iter, v),))
else: else:
v = _unicode(v) v = str(v)
if isinstance(v, _basestring): if isinstance(v, str):
v = _dict(((cdata_key, v),)) v = _dict(((cdata_key, v),))
cdata = None cdata = None
attrs = _dict() attrs = _dict()
@ -445,14 +421,16 @@ def _emit(key, value, content_handler,
attr_prefix) attr_prefix)
if ik == '@xmlns' and isinstance(iv, dict): if ik == '@xmlns' and isinstance(iv, dict):
for k, v in iv.items(): for k, v in iv.items():
attr = 'xmlns{}'.format(':{}'.format(k) if k else '') attr = 'xmlns{}'.format(f':{k}' if k else '')
attrs[attr] = _unicode(v) attrs[attr] = str(v)
continue continue
if not isinstance(iv, _unicode): if not isinstance(iv, str):
iv = _unicode(iv) iv = str(iv)
attrs[ik[len(attr_prefix):]] = iv attrs[ik[len(attr_prefix):]] = iv
continue continue
children.append((ik, iv)) children.append((ik, iv))
if isinstance(indent, int):
indent = ' ' * indent
if pretty: if pretty:
content_handler.ignorableWhitespace(depth * indent) content_handler.ignorableWhitespace(depth * indent)
content_handler.startElement(key, AttributesImpl(attrs)) content_handler.startElement(key, AttributesImpl(attrs))