Last active
February 11, 2020 13:43
-
-
Save ilittleangel/efd4d9f2e8df52323ba909fed095575c to your computer and use it in GitHub Desktop.
Python Script to sort a Hocon file with a list, using a first level field key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sort a list in a HOCON file with Python3.6 or higher | |
# Input HOCON file could be as following | |
# Notice that input file could be a single list with data | |
# or with an `input_metadata` field with the list itself. | |
# | With `input_metadata` key | With no `input_metadata` | | |
# |---------------------------|----------------------------| | |
# | { | | | |
# | input_metadata: [ | [ | | |
# | { | { | | |
# | id: "000003", | id: "000003", | | |
# | name: "metadata1", | name: "metadata1", | | |
# | attributes: {}, | attributes: {}, | | |
# | type: "type1" | type: "type1" | | |
# | } | } | | |
# | { | { | | |
# | id: "000001", | id: "000001", | | |
# | name: "metadata1", | name: "metadata1", | | |
# | attributes: {}, | attributes: {}, | | |
# | type: "type1" | type: "type1" | | |
# | } | } | | |
# | { | { | | |
# | id: "000002", | id: "000002", | | |
# | name: "metadata1", | name: "metadata1", | | |
# | attributes: {}, | attributes: {}, | | |
# | type: "type2" | type: "type2" | | |
# | } | } | | |
# | ] | ] | | |
# | } | | | |
import os | |
import sys | |
from glob import glob | |
from pyhocon import ConfigFactory, ConfigTree, HOCONConverter | |
def error(message): | |
print(f'\nError: {message}\n') | |
print('Usage: sortHoconByKey.py <inputFile.conf> <sortKey>') | |
sys.exit(1) | |
def clear(): | |
path = glob('*.tmp') | |
for file in path: | |
os.remove(file) | |
def prepare_input_file(file): | |
with open(file, 'r') as f: | |
content = f.read() | |
if 'process_data' not in content: | |
tmp = open(f'{file}.tmp', 'w') | |
tmp.write('['.rstrip('\r\n') + '\n' + content + '\n' + ']'.rstrip('\r\n')) | |
tmp.close() | |
return ConfigFactory.parse_file(tmp.name) | |
else: | |
return ConfigFactory.parse_file(file)['input_metadata'] | |
def sort_keys(config): | |
res = [] | |
for config_tree in config: | |
new = ConfigTree() | |
for k, v in sorted(config_tree.items()): | |
new.put(key=k, value=v) | |
res.append(new) | |
return res | |
def sort_hocon_by_key(file, key): | |
sorted_hocon = sorted(prepare_input_file(file), key=lambda config_tree: str(config_tree[key]).lower()) | |
config = ConfigTree() | |
config.put(key='input_metadata', value=sort_keys(sorted_hocon)) | |
output = HOCONConverter.to_hocon(config=config, indent=2, level=1).replace(' = ', ': ') | |
with open(f'{file}.out', 'w') as f: | |
f.write(output) | |
if __name__ == '__main__': | |
try: | |
sort_hocon_by_key(sys.argv[1], sys.argv[2]) | |
clear() | |
print('Done!') | |
except Exception as e: | |
error(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment