Assorted shell and Python scripts

Rewrite yaml2json in Python

Changed files
+14 -11
+14 -11
yaml2json
···
-
#!/usr/bin/env bash
-
# YAML to JSON conversion script
# Based on https://www.geeksforgeeks.org/convert-yaml-to-json/
#
-
# This script takes a YAML file as the first arg and outputs the
-
# converted JSON to stdout.
-
set -e
-
if ! python -c "import yaml" &>/dev/null; then
-
echo -e "PyYAML is not installed."
-
fi
-
if ! python -c "import yaml, json; print(json.dumps(yaml.load(open('${1}'), Loader=yaml.FullLoader), indent=4))"; then
-
echo "Some kinda error occurred while attempting to convert YAML to JSON"
-
fi
···
+
#!/usr/bin/env python3
+
#
# YAML to JSON conversion script
# Based on https://www.geeksforgeeks.org/convert-yaml-to-json/
#
+
# This script takes a YAML file as the first arg, converts the
+
# YAML content to JSON, and outputs the converted JSON content
+
# to stdout.
+
import json
+
import sys
+
import yaml
+
try:
+
print(json.dumps(yaml.load(open(sys.argv[1]), Loader=yaml.FullLoader), indent=4))
+
except IndexError:
+
print("YAML file must be supplied as first arg")
+
except FileNotFoundError:
+
print("YAML file not found")