mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-08 14:10:52 -07:00
Improve sorting of json and xml export attributes
This commit is contained in:
parent
270e07341a
commit
a39c6c1047
2 changed files with 30 additions and 2 deletions
|
@ -1650,12 +1650,14 @@ class Export(object):
|
|||
writer.writerows(csv_data)
|
||||
|
||||
elif self.file_format == 'json':
|
||||
json_data = json.dumps(result, indent=4, ensure_ascii=False, sort_keys=True)
|
||||
json_data = json.dumps(helpers.sort_obj(result),
|
||||
indent=4, ensure_ascii=False)
|
||||
with open(filepath, 'w', encoding='utf-8') as outfile:
|
||||
outfile.write(json_data)
|
||||
|
||||
elif self.file_format == 'xml':
|
||||
xml_data = helpers.dict_to_xml({self.media_type: result}, root_node='export', indent=4)
|
||||
xml_data = helpers.dict_to_xml({self.media_type: helpers.sort_obj(result)},
|
||||
root_node='export', indent=4)
|
||||
with open(filepath, 'w', encoding='utf-8') as outfile:
|
||||
outfile.write(xml_data)
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ import cloudinary
|
|||
from cloudinary.api import delete_resources_by_tag
|
||||
from cloudinary.uploader import upload
|
||||
from cloudinary.utils import cloudinary_url
|
||||
from collections import OrderedDict
|
||||
import datetime
|
||||
from functools import reduce, wraps
|
||||
import hashlib
|
||||
|
@ -1244,6 +1245,31 @@ def sort_attrs(attr):
|
|||
return len(a), a
|
||||
|
||||
|
||||
def sort_obj(obj):
|
||||
if isinstance(obj, list):
|
||||
result_obj = []
|
||||
for item in obj:
|
||||
result_obj.append(sort_obj(item))
|
||||
elif isinstance(obj, dict):
|
||||
result_start = []
|
||||
result_end = []
|
||||
for k, v in obj.items():
|
||||
if isinstance(v, list):
|
||||
for item in v:
|
||||
if isinstance(item, dict):
|
||||
result_end.append([k, sort_obj(v)])
|
||||
else:
|
||||
result_start.append([k, sort_obj(v)])
|
||||
else:
|
||||
result_start.append([k, sort_obj(v)])
|
||||
|
||||
result_obj = OrderedDict(sorted(result_start) + sorted(result_end))
|
||||
else:
|
||||
result_obj = obj
|
||||
|
||||
return result_obj
|
||||
|
||||
|
||||
def get_attrs_to_dict(obj, attrs):
|
||||
d = {}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue