diff options
author | Chris West (Faux) <git@goeswhere.com> | 2015-10-01 20:31:42 +0100 |
---|---|---|
committer | Chris West (Faux) <git@goeswhere.com> | 2015-10-01 20:31:42 +0100 |
commit | f741591d203f59faf6e041cbc6b6974ad2246144 (patch) | |
tree | 837fab3395d7b7d5bbbd4e7040a344ac7e8faddd /ghetto_json | |
download | ghetto-json-f741591d203f59faf6e041cbc6b6974ad2246144.tar.gz ghetto-json-f741591d203f59faf6e041cbc6b6974ad2246144.tar.bz2 ghetto-json-f741591d203f59faf6e041cbc6b6974ad2246144.tar.xz ghetto-json-f741591d203f59faf6e041cbc6b6974ad2246144.zip |
initial commit
Diffstat (limited to 'ghetto_json')
-rwxr-xr-x | ghetto_json | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/ghetto_json b/ghetto_json new file mode 100755 index 0000000..075accb --- /dev/null +++ b/ghetto_json @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +import json +import sys +import shlex + + +def main(params_list): + params = dict(x.split("=", 2) for x in params_list) + path = params.pop('path') + changed = False + + with open(path) as f: + obj = json.load(f) + for (key, target) in params.items(): + parts = key.split('.') + ref = obj + for part in parts[:-1]: + if part not in ref: + ref[part] = {} + ref = ref[part] + + last_part = parts[-1] + if target == 'unset': + if last_part in ref: + del ref[last_part] + changed = True + else: + if target.isdigit(): + target = int(target) + if target == 'null': + target = None + if target == 'false': + target = False + if target == 'true': + target = True + if last_part not in ref or ref[last_part] != target: + ref[last_part] = target + changed = True + + if changed: + with open(path, 'w') as f: + json.dump(obj, f, indent=2, separators=(',', ': '), sort_keys=True) + + print(json.dumps({'changed': changed})) + + +if __name__ == '__main__': + if len(sys.argv) == 2: + main(shlex.split(open(sys.argv[1]).read())) + else: + main(sys.argv[1:]) + |