<commit_before>#!/usr/bin/env python

"""
Command-line tool to validate and pretty-print JSON and XML.

Based on `python -mjson.tool` but without the crap.

Usage::

    $ echo '{"json":"obj"}' | pjson
    {
        "json": "obj"
    }
    $ echo '{ 1.2:3.4}' | pjson
    Expecting property name: line 1 column 2 (char 2)


Author: <NAME> <<EMAIL>>, 2012
"""

__version__ = '0.3'

import json
import sys
if sys.version_info[0] == 2:
    from StringIO import StringIO
else:
    from io import StringIO
from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments.lexers import JSONLexer, XmlLexer
import xml.dom.minidom
import argparse

def format_code(data, is_xml=False):
    """
    Parses data and formats it
    """
    if is_xml:
        return xml.dom.minidom.parseString(data).toprettyxml()
    else:
        obj = json.loads(data)
        output = StringIO()

        json.dump(obj, output, sort_keys=True, indent=2)
        return output.getvalue()

def color_yo_shit(code, lexer):
    """
    Calls pygments.highlight to color yo shit
    """
    return highlight(code, lexer, TerminalFormatter())

def main():
    """
    Main function to excecute everything in order
    """
    parser = argparse.ArgumentParser(description="Command-line tool to validate and pretty-print JSON and XML")
    parser.add_argument("-x", action="store_true", help="Data is XML")
    parser.add_argument("-b", action="store_true", help="Read data in chunks")
    args = parser.parse_args()

    if args.x and args.b:
        sys.stderr.write("-x and -b cannot be used simultaneously\n")
        exit(1)
    elif args.b:
        for line in sys.stdin:
            print(color_yo_shit(format_code(line), JSONLexer()))
    else:
        data = sys.stdin.read()
        if sys.stdout.isatty():
            data = color_yo_shit(format_code(data, args.x), XmlLexer() if args.x else JSONLexer())
        print(data)

if __name__ == '__main__':
    main()
<commit_msg>Handled exception when json not provided<commit_after>#!/usr/bin/env python

"""
Command-line tool to validate and pretty-print JSON and XML.

Based on `python -mjson.tool` but without the crap.

Usage::

    $ echo '{"json":"obj"}' | pjson
    {
        "json": "obj"
    }
    $ echo '{ 1.2:3.4}' | pjson
    Expecting property name: line 1 column 2 (char 2)


Author: <NAME> <<EMAIL>>, 2012
"""

__version__ = '0.3'

import json
import sys
if sys.version_info[0] == 2:
    from StringIO import StringIO
else:
    from io import StringIO
from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments.lexers import JSONLexer, XmlLexer
import xml.dom.minidom
import argparse

def format_code(data, is_xml=False):
    """
    Parses data and formats it
    """
    if is_xml:
        return xml.dom.minidom.parseString(data).toprettyxml()
    else:
        obj = json.loads(data)
        output = StringIO()

        json.dump(obj, output, sort_keys=True, indent=2)
        return output.getvalue()

def color_yo_shit(code, lexer):
    """
    Calls pygments.highlight to color yo shit
    """
    return highlight(code, lexer, TerminalFormatter())

def main():
    """
    Main function to excecute everything in order
    """
    parser = argparse.ArgumentParser(description="Command-line tool to validate and pretty-print JSON and XML")
    parser.add_argument("-x", action="store_true", help="Data is XML")
    parser.add_argument("-b", action="store_true", help="Read data in chunks")
    args = parser.parse_args()

    if args.x and args.b:
        sys.stderr.write("-x and -b cannot be used simultaneously\n")
        exit(1)
    elif args.b:
        for line in sys.stdin:
            print(color_yo_shit(format_code(line), JSONLexer()))
    else:
        data = sys.stdin.read()
        if sys.stdout.isatty():
            try:
                data = color_yo_shit(format_code(data, args.x), XmlLexer() if args.x else JSONLexer())
            except ValueError as e:
                print e
        print(data)

if __name__ == '__main__':
    main()

{'fraction_non_alphanumeric': 0.12021589793915603, 'fraction_numerical': 0.008341511285574092, 'mean_word_length': 2.886558627264061, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6154637', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1294, 'n_words': 378}