예제 #1
0
def strip(input, prune_null, style, **kwargs):
    """
    Removes specified portions of data from the input. Requires valid input.

    Examples:

        \b
        Example: Remove all elements with value=null:
        $ echo '{"a":{"b":null,"c":"null","d":"","e":{"f":null},"g":{},"h":[]}}' | python -mclifunzone.jsontool strip -n
        {"a": {"c": "null", "d": "", "e": {}, "g": {}, "h": []}}
    """
    if style == "compact":
        dumps_separators = (",", ":")
        dumps_indent = None
    elif style == "pretty":
        dumps_separators = None
        dumps_indent = 2
    elif style == "flat":
        dumps_separators = (",", ": ")
        dumps_indent = 0
    else:
        dumps_separators = None
        dumps_indent = None

    if not input:
        input = "-"
    with click.open_file(input, mode="rb") as f:
        data = json_utils.load_ordered(f)
        if prune_null:
            data = filter_none_values(data, recursive=True)
        s = json.dumps(data, indent=dumps_indent, separators=dumps_separators)
        click.echo(s)
예제 #2
0
def flattencommand(input, separator, sort_keys, style, **kwargs):
    """
    Flattens JSON input with nested or hierarchical structure into a flat (depth 1) hierarchy. Requires valid input.

    Examples:

        \b
        Example: Basic usage:
        $ echo '{"a":{"b":null,"c":"null","d":"","e":{"f":null},"g":{},"h":[]}}' | python -mclifunzone.jsontool flatten -c
        {"a__b":null,"a__c":"null","a__d":"","a__e__f":null,"a__h":[]}
    """
    if style == "compact":
        dumps_separators = (",", ":")
        dumps_indent = None
    elif style == "pretty":
        dumps_separators = None
        dumps_indent = 2
    elif style == "flat":
        dumps_separators = (",", ": ")
        dumps_indent = 0
    else:
        dumps_separators = None
        dumps_indent = None

    if not input:
        input = "-"
    if separator is None:
        separator = "__"
    with click.open_file(input, mode="rb") as f:
        data = json_utils.load_ordered(f)
        data = flatten(data, separator)
        s = json.dumps(data, indent=dumps_indent, separators=dumps_separators, sort_keys=sort_keys)
        click.echo(s)
예제 #3
0
def reprcommand(input, **kwargs):
    """
    Provides the python repr() representation of the parsed input. Requires valid input.
    """
    if not input:
        input = "-"
    with click.open_file(input, mode="rb") as f:
        data = json_utils.load_ordered(f)
        s = repr(data)
        click.echo(s)
예제 #4
0
def parse(input, pyformat, sort_keys, **kwargs):
    """
    Converts the input into a structured object hierarchy. Requires valid input.
    """
    if not input:
        input = "-"
    with click.open_file(input, mode="rb") as f:
        d = json_utils.load_ordered(f)
        if pyformat:
            s = pformat(d)
        else:
            s = json.dumps(d, indent=2, sort_keys=sort_keys)
        click.echo(s)
예제 #5
0
def info(input, verbose, pyformat, **kwargs):
    """
    Provides info about the input. Requires valid input.
    """
    if not input:
        input = "-"
    with click.open_file(input, mode="rb") as f:
        data = json_utils.load_ordered(f)
        d = {"length": len(data), "keys": sorted(data.keys())}
        if verbose:
            d["_object"] = {
                "type": type(data),
                # 'repr': repr(data),
                # 'vars': sorted(vars(data)),
                # 'dir': sorted(dir(data)),
                "members": sorted(varsdict(data).keys()),
            }
        # click.echo(d)
        # click.echo(sorted(d.items()))
        if pyformat:
            s = pformat(d)
        else:
            s = json.dumps(d, indent=2, sort_keys=True)
        click.echo(s)
예제 #6
0
def formatcommand(
    input,
    style,
    indent,
    skip_keys,
    sort_keys,
    ensure_ascii,
    check_circular,
    allow_nan,
    item_separator,
    dict_separator,
    **kwargs
):
    """
    (Re)formats the JSON input. Requires valid input.

    Supports several predefined, named format 'styles' (e.g. -p, -c, -f, -l),
    as well as various 'low-level' formatting options/parameters.

    Many common formatting scenarios can be satisfied by applying a single 'named style' option.
    However, usage scenarios with atypical and/or inflexible formatting requirements can instead
    use the low-level formatting options to achieve a wide variety of customized formats.

    Note that the 'named' format styles are mutually exclusive (with the other named styles
    and in many cases also with the various low-level formatting options).
    Usually, only a single 'named style' option will be used (i.e. with no other options),
    or else 1+ 'low-level' formatting options will be used (i.e. with no 'named style' options).
    When 'conflicting' options are used simultaneously, the tool will usually ignore the conflicts and do its best.
    E.g. If you use both the 'pretty' and 'compact' options simultaneously, it will ignore one of those two options.

    See the docs of the json.dumps() function (in python's built-in json module) for additional details
    about the various low-level formatting options.

    Examples:

        \b
        Example: Compact format:
        $ echo '{"a": {"b": null, "c": "null", "d": "", "e": {"f": {}}}}' | python -mclifunzone.jsontool format -c
        {"a":{"b":null,"c":"null","d":"","e":{"f":{}}}}

        \b
        Example: Pretty format:
        $ echo '{"a": {"b": null, "c": "null", "d": "", "e": {"f": []}}}' | python -mclifunzone.jsontool format -p
        {
          "a": {
            "b": null,
            "c": "null",
            "d": "",
            "e": {
              "f": []
            }
          }
        }
    """
    if style == "compact":
        separators = (",", ":")
        indent = None
    elif style == "pretty":
        # separators = (',', ':')
        separators = None
        indent = 2
    elif style == "flat":
        separators = (",", ": ")
        indent = 0
    elif item_separator or dict_separator:
        if not item_separator:
            item_separator = ", "
        if not dict_separator:
            dict_separator = ": "
        separators = (item_separator, dict_separator)
    else:
        separators = None

    if not input:
        input = "-"
    with click.open_file(input, mode="rb") as f:
        # s = f.read()
        # json_utils.loads_ordered(s)
        data = json_utils.load_ordered(f)
        if style == "lines":
            if len(data) != 1:
                raise ValueError('"lines" style requires that the input must contain a single root element.')
            root = data.keys()[0]
            header = '{"%s": [\n' % root
            child_line_separator = "\n,\n"
            footer = "]}"  # '\n]}'
            click.echo(header)
            lines = [json.dumps(child, indent=None, separators=(",", ":")) for child in data[root]]
            click.echo(child_line_separator.join(lines))
            click.echo(footer)
        else:
            s = json.dumps(
                data,
                skipkeys=skip_keys,
                sort_keys=sort_keys,
                ensure_ascii=ensure_ascii,
                check_circular=check_circular,
                allow_nan=allow_nan,
                indent=indent,
                separators=separators,
            )
            click.echo(s)