reyaml 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python
  2. # PYTHON_ARGCOMPLETE_OK
  3. import sys
  4. import yaml
  5. def reyaml(input_path, output_path):
  6. if input_path:
  7. with open(input_path, 'r') as input_file:
  8. input_yaml = input_file.read()
  9. else:
  10. input_yaml = sys.stdin.read()
  11. output_yaml = yaml.dump(
  12. yaml.load(input_yaml),
  13. default_flow_style = False,
  14. )
  15. if output_path:
  16. with open(output_path, 'w') as output_file:
  17. output_file.write(output_yaml)
  18. else:
  19. sys.stdout.write(output_yaml)
  20. def _init_argparser():
  21. import argparse
  22. argparser = argparse.ArgumentParser(description = None)
  23. argparser.add_argument('-i', dest = 'input_path')
  24. argparser.add_argument('-o', dest = 'output_path')
  25. return argparser
  26. def main(argv):
  27. argparser = _init_argparser()
  28. try:
  29. import argcomplete
  30. argcomplete.autocomplete(argparser)
  31. except ImportError:
  32. pass
  33. args = argparser.parse_args(argv)
  34. reyaml(**vars(args))
  35. return 0
  36. if __name__ == "__main__":
  37. sys.exit(main(sys.argv[1:]))