Created
May 18, 2016 15:03
-
-
Save sharph/250f3c1beecc7b33791a0978ae985938 to your computer and use it in GitHub Desktop.
because writing json by hand is no fun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
from os.path import splitext | |
import yaml | |
import yamlordereddictloader | |
import json | |
import argparse | |
def yml2json(yml): | |
return json.dumps(yaml.load(yml, Loader=yamlordereddictloader.Loader), indent=2) | |
def main(): | |
parser = argparse.ArgumentParser(description='tool to convert yml to json') | |
parser.add_argument('filename', help='Filename of yml file to convert. ' | |
'Use - for STDIN.') | |
args = parser.parse_args() | |
if args.filename != '-': | |
with open(args.filename, 'r') as f: | |
json = yml2json(f.read()) | |
with open(splitext(args.filename)[0] + '.json', 'w') as dst: | |
dst.write(json) | |
else: # pipe mode | |
json = yml2json(sys.stdin) | |
sys.stdout.write(json) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment