From 8a2dc9184a6d4db665d12690207b265e96a2490f Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Tue, 8 Mar 2022 15:30:06 +0100 Subject: [PATCH] add toml script for #343 --- scripts/yaml-toml.py | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 scripts/yaml-toml.py diff --git a/scripts/yaml-toml.py b/scripts/yaml-toml.py new file mode 100755 index 0000000..adaa406 --- /dev/null +++ b/scripts/yaml-toml.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +from ruamel.yaml import YAML as yaml +from deepdiff import DeepDiff +import toml +import os +import sys +import pprint +import json + +# pip install toml deepdiff + +def _yaml_load(path): + """load from yaml""" + with open(path, 'r', encoding='utf8') as file: + data = yaml() + data.typ = 'rt' + content = data.load(file) + return content + +def _yaml_dump(content, where): + """dump to yaml""" + data = yaml() + data.default_flow_style = False + data.indent = 2 + data.typ = 'rt' + data.dump(content, where) + +def _toml_load(path): + """load from toml""" + with open(path, 'r', encoding='utf8') as file: + data = file.read() + content = toml.loads(data) + return content + +def _toml_dump(content, where): + with open(where, 'w') as f: + toml.dump(content, f) + +if __name__ == '__main__': + if len(sys.argv) < 3: + print("usage:") + print(" {} to-toml ".format(sys.argv[0])) + print(" {} to-yaml ".format(sys.argv[0])) + print(" {} compare ".format(sys.argv[0])) + sys.exit(1) + + act = sys.argv[1] + arg = sys.argv[2] + if len(sys.argv) > 3: + arg2 = sys.argv[3] + out = '/tmp/res' + + if act == "to-toml": + + content = _yaml_load(arg) + o = out + '.toml' + _toml_dump(content, o) + print("saved to {}".format(o)) + + elif act == "to-yaml": + + content = _toml_load(arg) + o = out + '.yaml' + _yaml_dump(content, o) + print("saved to {}".format(o)) + + elif act == "compare": + a = _yaml_load(arg) + + print("YAML dict:") + print(json.dumps(a, indent=4)) + b = _toml_load(arg2) + + print("TOML dict:") + print(json.dumps(b, indent=4)) + + diff = DeepDiff(a, b, ignore_order=True, view='tree') + print(diff)