aboutsummaryrefslogtreecommitdiffstats
path: root/bin/yamltojson
diff options
context:
space:
mode:
authorlj1412 <lji@research.att.com>2017-02-14 14:53:03 +0000
committerlj1412 <lji@research.att.com>2017-02-14 14:53:05 +0000
commit563c1c21a69fea27e3e6c7b342f56ffb36aaea31 (patch)
tree051c5d0ec822652c09a46031ff20b61fec6ab82c /bin/yamltojson
parentcc9731f24400bf177809c4260db98c6516c8041b (diff)
Init dcae.utils.buildtools
Change-Id: I3e51d97f30a4f619eed3468963ee7c47792a1050 Signed-off-by: lj1412 <lji@research.att.com>
Diffstat (limited to 'bin/yamltojson')
-rwxr-xr-xbin/yamltojson39
1 files changed, 39 insertions, 0 deletions
diff --git a/bin/yamltojson b/bin/yamltojson
new file mode 100755
index 0000000..6aff82f
--- /dev/null
+++ b/bin/yamltojson
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+# -*- indent-tabs-mode: nil -*- vi: set expandtab:
+
+"""
+NAME
+ yamltojson - convert a yaml file to a json file
+
+SYNOPSIS
+ yamltojson file.yaml ...
+
+DESCRIPTION
+ Read in a yaml file (whose name must end with ".yaml") and create cor‐
+ responding json files, whose names will end with ".json".
+"""
+
+import sys, re, yaml
+try:
+ import simplejson as json
+except:
+ import json
+
+def die(msg):
+ """ generate a FATAL message to stdout and exit """
+ print("%s:FATAL:%s" % (date(), msg))
+ sys.exit(2)
+
+for fname in sys.argv[1:]:
+ if fname.endswith(".yaml"):
+ y = None
+ with open(fname, "r") as fd:
+ try:
+ contents = fd.read()
+ contents = re.sub("^\t+", " ", contents, flags=re.M)
+ y = yaml.safe_load(contents)
+ except:
+ die("Invalid yaml in '%s'" % fname)
+ jsonfname = fname[:-5] + ".json"
+ with open(jsonfname, "w") as fd:
+ json.dump(y, fd, indent=4)