From 534dff1aa89e766e35d2b929d97486c482c003c0 Mon Sep 17 00:00:00 2001 From: Flinner Yuu Date: Fri, 31 Mar 2023 13:30:21 +0300 Subject: [PATCH] feat: add yaml-to-json script --- bin/bin/yj | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 bin/bin/yj diff --git a/bin/bin/yj b/bin/bin/yj new file mode 100755 index 0000000..2697432 --- /dev/null +++ b/bin/bin/yj @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +# I didn't write this! + +import json,yaml,sys,os + +if len(sys.argv) != 2: + print('Usage:\n '+os.path.basename(__file__)+' /path/file{.json|.yml}') + print('\nConverts JSON to YAML, and vice-versa') + sys.exit(0) + +path = sys.argv[1] + +if not os.path.isfile(path): + print('Bad or non-existant file: '+path) + sys.exit(1) + +with open(path) as file: + + if path.lower().endswith('json'): + print(yaml.dump(json.load(file), Dumper=yaml.CDumper, allow_unicode=True)) + elif path.lower().endswith('yaml') or path.lower().endswith('yml'): + print(json.dumps(yaml.load(file, Loader=yaml.SafeLoader), ensure_ascii=False ,indent=2)) + else: + print('Bad file extension. Must be yml or json') +