mirror of
https://github.com/SickGear/SickGear.git
synced 2025-01-08 11:03:38 +00:00
Merge pull request #884 from JackDandy/feature/UpdateXmltodict
Update xmltodict library 0.9.2 (eac0031) to 0.10.2 (375d3a6).
This commit is contained in:
commit
9f1f94d59f
2 changed files with 82 additions and 12 deletions
|
@ -25,6 +25,7 @@
|
||||||
* Update socks from SocksiPy 1.0 to PySocks 1.6.5 (b4323df)
|
* Update socks from SocksiPy 1.0 to PySocks 1.6.5 (b4323df)
|
||||||
* Update Tornado Web Server 4.5.dev1 (92f29b8) to 4.5.dev1 (38e493e)
|
* Update Tornado Web Server 4.5.dev1 (92f29b8) to 4.5.dev1 (38e493e)
|
||||||
* Update unidecode library 0.04.18 to 0.04.20 (1e18d98)
|
* Update unidecode library 0.04.18 to 0.04.20 (1e18d98)
|
||||||
|
* Update xmltodict library 0.9.2 (eac0031) to 0.10.2 (375d3a6)
|
||||||
|
|
||||||
|
|
||||||
[develop changelog]
|
[develop changelog]
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
#!/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 defusedexpat import pyexpat as expat
|
||||||
|
except ImportError:
|
||||||
from xml.parsers import expat
|
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
|
||||||
|
@ -29,7 +32,7 @@ except NameError: # pragma no cover
|
||||||
_unicode = str
|
_unicode = str
|
||||||
|
|
||||||
__author__ = 'Martin Blech'
|
__author__ = 'Martin Blech'
|
||||||
__version__ = '0.9.2'
|
__version__ = '0.10.2'
|
||||||
__license__ = 'MIT'
|
__license__ = 'MIT'
|
||||||
|
|
||||||
|
|
||||||
|
@ -51,7 +54,7 @@ class _DictSAXHandler(object):
|
||||||
strip_whitespace=True,
|
strip_whitespace=True,
|
||||||
namespace_separator=':',
|
namespace_separator=':',
|
||||||
namespaces=None,
|
namespaces=None,
|
||||||
force_list=()):
|
force_list=None):
|
||||||
self.path = []
|
self.path = []
|
||||||
self.stack = []
|
self.stack = []
|
||||||
self.data = []
|
self.data = []
|
||||||
|
@ -68,6 +71,7 @@ class _DictSAXHandler(object):
|
||||||
self.strip_whitespace = strip_whitespace
|
self.strip_whitespace = strip_whitespace
|
||||||
self.namespace_separator = namespace_separator
|
self.namespace_separator = namespace_separator
|
||||||
self.namespaces = namespaces
|
self.namespaces = namespaces
|
||||||
|
self.namespace_declarations = OrderedDict()
|
||||||
self.force_list = force_list
|
self.force_list = force_list
|
||||||
|
|
||||||
def _build_name(self, full_name):
|
def _build_name(self, full_name):
|
||||||
|
@ -88,16 +92,29 @@ class _DictSAXHandler(object):
|
||||||
return attrs
|
return attrs
|
||||||
return self.dict_constructor(zip(attrs[0::2], attrs[1::2]))
|
return self.dict_constructor(zip(attrs[0::2], attrs[1::2]))
|
||||||
|
|
||||||
|
def startNamespaceDecl(self, prefix, uri):
|
||||||
|
self.namespace_declarations[prefix or ''] = uri
|
||||||
|
|
||||||
def startElement(self, full_name, attrs):
|
def startElement(self, full_name, attrs):
|
||||||
name = self._build_name(full_name)
|
name = self._build_name(full_name)
|
||||||
attrs = self._attrs_to_dict(attrs)
|
attrs = self._attrs_to_dict(attrs)
|
||||||
|
if attrs and self.namespace_declarations:
|
||||||
|
attrs['xmlns'] = self.namespace_declarations
|
||||||
|
self.namespace_declarations = OrderedDict()
|
||||||
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:
|
||||||
attrs = self.dict_constructor(
|
attr_entries = []
|
||||||
(self.attr_prefix+self._build_name(key), value)
|
for key, value in attrs.items():
|
||||||
for (key, value) in attrs.items())
|
key = self.attr_prefix+self._build_name(key)
|
||||||
|
if self.postprocessor:
|
||||||
|
entry = self.postprocessor(self.path, key, value)
|
||||||
|
else:
|
||||||
|
entry = (key, value)
|
||||||
|
if entry:
|
||||||
|
attr_entries.append(entry)
|
||||||
|
attrs = self.dict_constructor(attr_entries)
|
||||||
else:
|
else:
|
||||||
attrs = None
|
attrs = None
|
||||||
self.item = attrs or None
|
self.item = attrs or None
|
||||||
|
@ -155,12 +172,20 @@ class _DictSAXHandler(object):
|
||||||
else:
|
else:
|
||||||
item[key] = [value, data]
|
item[key] = [value, data]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
if key in self.force_list:
|
if self._should_force_list(key, data):
|
||||||
item[key] = [data]
|
item[key] = [data]
|
||||||
else:
|
else:
|
||||||
item[key] = data
|
item[key] = data
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
def _should_force_list(self, key, value):
|
||||||
|
if not self.force_list:
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
return key in self.force_list
|
||||||
|
except TypeError:
|
||||||
|
return self.force_list(self.path[:-1], key, value)
|
||||||
|
|
||||||
|
|
||||||
def parse(xml_input, encoding=None, expat=expat, process_namespaces=False,
|
def parse(xml_input, encoding=None, expat=expat, process_namespaces=False,
|
||||||
namespace_separator=':', **kwargs):
|
namespace_separator=':', **kwargs):
|
||||||
|
@ -199,7 +224,7 @@ def parse(xml_input, encoding=None, expat=expat, process_namespaces=False,
|
||||||
Streaming example::
|
Streaming example::
|
||||||
|
|
||||||
>>> def handle(path, item):
|
>>> def handle(path, item):
|
||||||
... print 'path:%s item:%s' % (path, item)
|
... print('path:%s item:%s' % (path, item))
|
||||||
... return True
|
... return True
|
||||||
...
|
...
|
||||||
>>> xmltodict.parse(\"\"\"
|
>>> xmltodict.parse(\"\"\"
|
||||||
|
@ -261,6 +286,10 @@ def parse(xml_input, encoding=None, expat=expat, process_namespaces=False,
|
||||||
'interfaces':
|
'interfaces':
|
||||||
{'interface':
|
{'interface':
|
||||||
[ {'name': 'em0', 'ip_address': '10.0.0.1' } ] } } }
|
[ {'name': 'em0', 'ip_address': '10.0.0.1' } ] } } }
|
||||||
|
|
||||||
|
`force_list` can also be a callable that receives `path`, `key` and
|
||||||
|
`value`. This is helpful in cases where the logic that decides whether
|
||||||
|
a list should be forced is more complex.
|
||||||
"""
|
"""
|
||||||
handler = _DictSAXHandler(namespace_separator=namespace_separator,
|
handler = _DictSAXHandler(namespace_separator=namespace_separator,
|
||||||
**kwargs)
|
**kwargs)
|
||||||
|
@ -279,6 +308,7 @@ def parse(xml_input, encoding=None, expat=expat, process_namespaces=False,
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# Jython's expat does not support ordered_attributes
|
# Jython's expat does not support ordered_attributes
|
||||||
pass
|
pass
|
||||||
|
parser.StartNamespaceDeclHandler = handler.startNamespaceDecl
|
||||||
parser.StartElementHandler = handler.startElement
|
parser.StartElementHandler = handler.startElement
|
||||||
parser.EndElementHandler = handler.endElement
|
parser.EndElementHandler = handler.endElement
|
||||||
parser.CharacterDataHandler = handler.characters
|
parser.CharacterDataHandler = handler.characters
|
||||||
|
@ -290,6 +320,21 @@ def parse(xml_input, encoding=None, expat=expat, process_namespaces=False,
|
||||||
return handler.item
|
return handler.item
|
||||||
|
|
||||||
|
|
||||||
|
def _process_namespace(name, namespaces, ns_sep=':', attr_prefix='@'):
|
||||||
|
if not namespaces:
|
||||||
|
return name
|
||||||
|
try:
|
||||||
|
ns, name = name.rsplit(ns_sep, 1)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
ns_res = namespaces.get(ns.strip(attr_prefix))
|
||||||
|
name = '{0}{1}{2}{3}'.format(
|
||||||
|
attr_prefix if ns.startswith(attr_prefix) else '',
|
||||||
|
ns_res, ns_sep, name) if ns_res else name
|
||||||
|
return name
|
||||||
|
|
||||||
|
|
||||||
def _emit(key, value, content_handler,
|
def _emit(key, value, content_handler,
|
||||||
attr_prefix='@',
|
attr_prefix='@',
|
||||||
cdata_key='#text',
|
cdata_key='#text',
|
||||||
|
@ -298,7 +343,10 @@ def _emit(key, value, content_handler,
|
||||||
pretty=False,
|
pretty=False,
|
||||||
newl='\n',
|
newl='\n',
|
||||||
indent='\t',
|
indent='\t',
|
||||||
|
namespace_separator=':',
|
||||||
|
namespaces=None,
|
||||||
full_document=True):
|
full_document=True):
|
||||||
|
key = _process_namespace(key, namespaces, namespace_separator, attr_prefix)
|
||||||
if preprocessor is not None:
|
if preprocessor is not None:
|
||||||
result = preprocessor(key, value)
|
result = preprocessor(key, value)
|
||||||
if result is None:
|
if result is None:
|
||||||
|
@ -325,6 +373,15 @@ def _emit(key, value, content_handler,
|
||||||
cdata = iv
|
cdata = iv
|
||||||
continue
|
continue
|
||||||
if ik.startswith(attr_prefix):
|
if ik.startswith(attr_prefix):
|
||||||
|
ik = _process_namespace(ik, namespaces, namespace_separator,
|
||||||
|
attr_prefix)
|
||||||
|
if ik == '@xmlns' and isinstance(iv, dict):
|
||||||
|
for k, v in iv.items():
|
||||||
|
attr = 'xmlns{0}'.format(':{0}'.format(k) if k else '')
|
||||||
|
attrs[attr] = _unicode(v)
|
||||||
|
continue
|
||||||
|
if not isinstance(iv, _unicode):
|
||||||
|
iv = _unicode(iv)
|
||||||
attrs[ik[len(attr_prefix):]] = iv
|
attrs[ik[len(attr_prefix):]] = iv
|
||||||
continue
|
continue
|
||||||
children.append((ik, iv))
|
children.append((ik, iv))
|
||||||
|
@ -336,7 +393,8 @@ def _emit(key, value, content_handler,
|
||||||
for child_key, child_value in children:
|
for child_key, child_value in children:
|
||||||
_emit(child_key, child_value, content_handler,
|
_emit(child_key, child_value, content_handler,
|
||||||
attr_prefix, cdata_key, depth+1, preprocessor,
|
attr_prefix, cdata_key, depth+1, preprocessor,
|
||||||
pretty, newl, indent)
|
pretty, newl, indent, namespaces=namespaces,
|
||||||
|
namespace_separator=namespace_separator)
|
||||||
if cdata is not None:
|
if cdata is not None:
|
||||||
content_handler.characters(cdata)
|
content_handler.characters(cdata)
|
||||||
if pretty and children:
|
if pretty and children:
|
||||||
|
@ -347,6 +405,7 @@ def _emit(key, value, content_handler,
|
||||||
|
|
||||||
|
|
||||||
def unparse(input_dict, output=None, encoding='utf-8', full_document=True,
|
def unparse(input_dict, output=None, encoding='utf-8', full_document=True,
|
||||||
|
short_empty_elements=False,
|
||||||
**kwargs):
|
**kwargs):
|
||||||
"""Emit an XML document for the given `input_dict` (reverse of `parse`).
|
"""Emit an XML document for the given `input_dict` (reverse of `parse`).
|
||||||
|
|
||||||
|
@ -368,6 +427,9 @@ def unparse(input_dict, output=None, encoding='utf-8', full_document=True,
|
||||||
if output is None:
|
if output is None:
|
||||||
output = StringIO()
|
output = StringIO()
|
||||||
must_return = True
|
must_return = True
|
||||||
|
if short_empty_elements:
|
||||||
|
content_handler = XMLGenerator(output, encoding, True)
|
||||||
|
else:
|
||||||
content_handler = XMLGenerator(output, encoding)
|
content_handler = XMLGenerator(output, encoding)
|
||||||
if full_document:
|
if full_document:
|
||||||
content_handler.startDocument()
|
content_handler.startDocument()
|
||||||
|
@ -387,16 +449,23 @@ def unparse(input_dict, output=None, encoding='utf-8', full_document=True,
|
||||||
if __name__ == '__main__': # pragma: no cover
|
if __name__ == '__main__': # pragma: no cover
|
||||||
import sys
|
import sys
|
||||||
import marshal
|
import marshal
|
||||||
|
try:
|
||||||
|
stdin = sys.stdin.buffer
|
||||||
|
stdout = sys.stdout.buffer
|
||||||
|
except AttributeError:
|
||||||
|
stdin = sys.stdin
|
||||||
|
stdout = sys.stdout
|
||||||
|
|
||||||
(item_depth,) = sys.argv[1:]
|
(item_depth,) = sys.argv[1:]
|
||||||
item_depth = int(item_depth)
|
item_depth = int(item_depth)
|
||||||
|
|
||||||
|
|
||||||
def handle_item(path, item):
|
def handle_item(path, item):
|
||||||
marshal.dump((path, item), sys.stdout)
|
marshal.dump((path, item), stdout)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
root = parse(sys.stdin,
|
root = parse(stdin,
|
||||||
item_depth=item_depth,
|
item_depth=item_depth,
|
||||||
item_callback=handle_item,
|
item_callback=handle_item,
|
||||||
dict_constructor=dict)
|
dict_constructor=dict)
|
||||||
|
|
Loading…
Reference in a new issue