mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-08 14:10:52 -07:00
Improve XML export
This commit is contained in:
parent
8d18e98ca7
commit
627129dd95
2 changed files with 13 additions and 7 deletions
|
@ -1598,7 +1598,7 @@ class Export(object):
|
||||||
outfile.write(json_data)
|
outfile.write(json_data)
|
||||||
|
|
||||||
elif self.file_format == 'xml':
|
elif self.file_format == 'xml':
|
||||||
xml_data = helpers.dict2xml(result, root_node=self.media_type)
|
xml_data = helpers.dict2xml({self.media_type: result}, root_node='export')
|
||||||
with open(filepath, 'w', encoding='utf-8') as outfile:
|
with open(filepath, 'w', encoding='utf-8') as outfile:
|
||||||
outfile.write(xml_data)
|
outfile.write(xml_data)
|
||||||
|
|
||||||
|
|
|
@ -1385,7 +1385,7 @@ def escape_xml(value):
|
||||||
|
|
||||||
|
|
||||||
# https://gist.github.com/reimund/5435343/
|
# https://gist.github.com/reimund/5435343/
|
||||||
def dict2xml(d, root_node=None):
|
def dict2xml(d, root_node=None, indent=4, level=0):
|
||||||
wrap = not bool(root_node is None or isinstance(d, list))
|
wrap = not bool(root_node is None or isinstance(d, list))
|
||||||
root = root_node or 'objects'
|
root = root_node or 'objects'
|
||||||
root_singular = root[:-1] if root.endswith('s') and isinstance(d, list) else root
|
root_singular = root[:-1] if root.endswith('s') and isinstance(d, list) else root
|
||||||
|
@ -1395,28 +1395,34 @@ def dict2xml(d, root_node=None):
|
||||||
if isinstance(d, dict):
|
if isinstance(d, dict):
|
||||||
for key, value in sorted(d.items()):
|
for key, value in sorted(d.items()):
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
children.append(dict2xml(value, key))
|
children.append(dict2xml(value, key, level=level + 1))
|
||||||
elif isinstance(value, list):
|
elif isinstance(value, list):
|
||||||
children.append(dict2xml(value, key))
|
children.append(dict2xml(value, key, level=level + 1))
|
||||||
else:
|
else:
|
||||||
xml = '{} {}="{}"'.format(xml, key, escape_xml(value))
|
xml = '{} {}="{}"'.format(xml, key, escape_xml(value))
|
||||||
elif isinstance(d, list):
|
elif isinstance(d, list):
|
||||||
for value in d:
|
for value in d:
|
||||||
children.append(dict2xml(value, root_singular))
|
# Custom tag replacement for collections/playlists
|
||||||
|
if isinstance(value, dict) and root in ('children', 'items'):
|
||||||
|
root_singular = value.get('type', root_singular)
|
||||||
|
children.append(dict2xml(value, root_singular, level=level))
|
||||||
else:
|
else:
|
||||||
children.append(escape_xml(d))
|
children.append(escape_xml(d))
|
||||||
|
|
||||||
end_tag = '>' if len(children) > 0 else '/>'
|
end_tag = '>' if len(children) > 0 else '/>'
|
||||||
|
end_tag += '\n' if isinstance(d, list) or isinstance(d, dict) else ''
|
||||||
|
spaces = ' ' * level * indent
|
||||||
|
|
||||||
if wrap or isinstance(d, dict):
|
if wrap or isinstance(d, dict):
|
||||||
xml = '<{}{}{}'.format(root, xml, end_tag)
|
xml = '{}<{}{}{}'.format(spaces, root, xml, end_tag)
|
||||||
|
|
||||||
if len(children) > 0:
|
if len(children) > 0:
|
||||||
for child in children:
|
for child in children:
|
||||||
xml = '{}{}'.format(xml, child)
|
xml = '{}{}'.format(xml, child)
|
||||||
|
|
||||||
if wrap or isinstance(d, dict):
|
if wrap or isinstance(d, dict):
|
||||||
xml = '{}</{}>'.format(xml, root)
|
spaces = spaces if isinstance(d, dict) else ''
|
||||||
|
xml = '{}{}</{}>\n'.format(xml, spaces, root)
|
||||||
|
|
||||||
return xml
|
return xml
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue