logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

tool.py (3182B)


  1. r"""Command-line tool to validate and pretty-print JSON
  2. Usage::
  3. $ echo '{"json":"obj"}' | python -m json.tool
  4. {
  5. "json": "obj"
  6. }
  7. $ echo '{ 1.2:3.4}' | python -m json.tool
  8. Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
  9. """
  10. import argparse
  11. import json
  12. import sys
  13. def main():
  14. prog = 'python -m json.tool'
  15. description = ('A simple command line interface for json module '
  16. 'to validate and pretty-print JSON objects.')
  17. parser = argparse.ArgumentParser(prog=prog, description=description)
  18. parser.add_argument('infile', nargs='?',
  19. type=argparse.FileType(encoding="utf-8"),
  20. help='a JSON file to be validated or pretty-printed',
  21. default=sys.stdin)
  22. parser.add_argument('outfile', nargs='?',
  23. type=argparse.FileType('w', encoding="utf-8"),
  24. help='write the output of infile to outfile',
  25. default=sys.stdout)
  26. parser.add_argument('--sort-keys', action='store_true', default=False,
  27. help='sort the output of dictionaries alphabetically by key')
  28. parser.add_argument('--no-ensure-ascii', dest='ensure_ascii', action='store_false',
  29. help='disable escaping of non-ASCII characters')
  30. parser.add_argument('--json-lines', action='store_true', default=False,
  31. help='parse input using the JSON Lines format. '
  32. 'Use with --no-indent or --compact to produce valid JSON Lines output.')
  33. group = parser.add_mutually_exclusive_group()
  34. group.add_argument('--indent', default=4, type=int,
  35. help='separate items with newlines and use this number '
  36. 'of spaces for indentation')
  37. group.add_argument('--tab', action='store_const', dest='indent',
  38. const='\t', help='separate items with newlines and use '
  39. 'tabs for indentation')
  40. group.add_argument('--no-indent', action='store_const', dest='indent',
  41. const=None,
  42. help='separate items with spaces rather than newlines')
  43. group.add_argument('--compact', action='store_true',
  44. help='suppress all whitespace separation (most compact)')
  45. options = parser.parse_args()
  46. dump_args = {
  47. 'sort_keys': options.sort_keys,
  48. 'indent': options.indent,
  49. 'ensure_ascii': options.ensure_ascii,
  50. }
  51. if options.compact:
  52. dump_args['indent'] = None
  53. dump_args['separators'] = ',', ':'
  54. with options.infile as infile, options.outfile as outfile:
  55. try:
  56. if options.json_lines:
  57. objs = (json.loads(line) for line in infile)
  58. else:
  59. objs = (json.load(infile), )
  60. for obj in objs:
  61. json.dump(obj, outfile, **dump_args)
  62. outfile.write('\n')
  63. except ValueError as e:
  64. raise SystemExit(e)
  65. if __name__ == '__main__':
  66. try:
  67. main()
  68. except BrokenPipeError as exc:
  69. sys.exit(exc.errno)