Update dict_to_xml helper function

This commit is contained in:
JonnyWong16 2020-10-04 01:35:11 -07:00
parent e3214946a3
commit 842a76aae1
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
2 changed files with 9 additions and 8 deletions

View file

@ -1640,7 +1640,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.dict_to_xml({self.media_type: result}, root_node='export') xml_data = helpers.dict_to_xml({self.media_type: result}, root_node='export', indent=4)
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)

View file

@ -1385,7 +1385,8 @@ def escape_xml(value):
# https://gist.github.com/reimund/5435343/ # https://gist.github.com/reimund/5435343/
def dict_to_xml(d, root_node=None, indent=4, level=0): def dict_to_xml(d, root_node=None, indent=None, level=0):
line_break = '' if indent is None else '\n'
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,9 +1396,9 @@ def dict_to_xml(d, root_node=None, indent=4, level=0):
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(dict_to_xml(value, key, level=level + 1)) children.append(dict_to_xml(value, key, indent, level + 1))
elif isinstance(value, list): elif isinstance(value, list):
children.append(dict_to_xml(value, key, level=level + 1)) children.append(dict_to_xml(value, key, indent, 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):
@ -1405,13 +1406,13 @@ def dict_to_xml(d, root_node=None, indent=4, level=0):
# Custom tag replacement for collections/playlists # Custom tag replacement for collections/playlists
if isinstance(value, dict) and root in ('children', 'items'): if isinstance(value, dict) and root in ('children', 'items'):
root_singular = value.get('type', root_singular) root_singular = value.get('type', root_singular)
children.append(dict_to_xml(value, root_singular, level=level)) children.append(dict_to_xml(value, root_singular, indent, 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 '' end_tag += line_break if isinstance(d, list) or isinstance(d, dict) else ''
spaces = ' ' * level * indent spaces = ' ' * level * (indent or 0)
if wrap or isinstance(d, dict): if wrap or isinstance(d, dict):
xml = '{}<{}{}{}'.format(spaces, root, xml, end_tag) xml = '{}<{}{}{}'.format(spaces, root, xml, end_tag)
@ -1422,7 +1423,7 @@ def dict_to_xml(d, root_node=None, indent=4, level=0):
if wrap or isinstance(d, dict): if wrap or isinstance(d, dict):
spaces = spaces if isinstance(d, dict) else '' spaces = spaces if isinstance(d, dict) else ''
xml = '{}{}</{}>\n'.format(xml, spaces, root) xml = '{}{}</{}>{}'.format(xml, spaces, root, line_break)
return xml return xml