Skip to content

Commit

Permalink
(fixup) PR Feedback (#847)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryxias authored and ryandeivert committed Dec 17, 2018
1 parent 19fffb5 commit bb07e54
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions tests/scripts/sort_configs.py
Expand Up @@ -6,7 +6,6 @@

from collections import OrderedDict

SCRIPT_NAME = 'JsonFileSorter'
CONF_LOGS_FILE = os.path.join(os.path.dirname(__file__), '../../conf/logs.json')


Expand All @@ -17,17 +16,20 @@ class JsonFileSorter(object):
"""

def __init__(self):
self._logger = logging.getLogger(SCRIPT_NAME)
self._logger = logging.getLogger()

def sort_json_file(self, file_path):
self._logger.info('Sorting file: {}...'.format(file_path))

with open(file_path, 'r') as infile:
original_text = infile.read().strip()

# Load the JSON document using OrderedDict so that it preserves original ordering
# Load the JSON document using OrderedDict, as it allows us to preserve the ordering
# of the internal json keys. This is important for certain schemas, such as ones parsed
# via csv, as the ordering of the keys does matter.
schema = json.loads(original_text, object_pairs_hook=OrderedDict)

# Sort the loaded schema by top-level key. Preserve the ordering of internal keys.
ordered_schema = OrderedDict(sorted(schema.items(), key=lambda k: k[0]))

with open(file_path, 'w') as outfile:
Expand Down

0 comments on commit bb07e54

Please sign in to comment.