From f741591d203f59faf6e041cbc6b6974ad2246144 Mon Sep 17 00:00:00 2001 From: "Chris West (Faux)" Date: Thu, 1 Oct 2015 20:31:42 +0100 Subject: initial commit --- ghetto_json | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 ghetto_json (limited to 'ghetto_json') 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:]) + -- cgit v1.2.3