Skip to content

Commit

Permalink
byml_to_yml: Add option to dump to JSON
Browse files Browse the repository at this point in the history
Useful for web things.
  • Loading branch information
leoetlino committed Jan 21, 2019
1 parent 6e1b5aa commit 21db57c
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion byml/byml_to_yml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import json
import os
import sys
import yaml
Expand All @@ -9,6 +10,7 @@

def main() -> None:
parser = argparse.ArgumentParser(description='Converts a BYML file to YAML.')
parser.add_argument('-j', '--to-json', action='store_true', help='Convert to JSON (warning: one-way conversion; does not preserve type information)')
parser.add_argument('byml', help='Path to a BYML file', nargs='?', default='-')
parser.add_argument('yml', help='Path to destination YAML file', nargs='?', default='-')
args = parser.parse_args()
Expand All @@ -30,7 +32,10 @@ def main() -> None:
sys.exit(1)
output = sys.stdout if args.yml == '-' else open(args.yml, 'w', encoding='utf-8')
with output:
yaml.dump(root, output, Dumper=dumper, allow_unicode=True, encoding='utf-8')
if args.to_json:
json.dump(root, output, ensure_ascii=False)
else:
yaml.dump(root, output, Dumper=dumper, allow_unicode=True, encoding='utf-8')

if __name__ == '__main__':
main()

0 comments on commit 21db57c

Please sign in to comment.