예제 #1
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)
예제 #2
0
def info(input, output_format, verbose, flat, **kwargs):
    """
    Provides info about the input.
    """
    if not input:
        input = "-"
    with click.open_file(input, mode="rb") as f:
        data = f.read()
        d = {}
        d.update({"content": txt_utils.get_info(data)})
        # if verbose:
        #     d['_object'] = {
        #         'type': type(data),
        #         'members': sorted(varsdict(data).keys())
        #     }
        if flat:
            d = dict_utils.flatten(d)

        if not output_format:
            output_format = "json"
        if output_format == "pydict":
            s = pformat(d)
        elif output_format == "json":
            s = json.dumps(d, indent=2, sort_keys=True)
        # elif output_format == 'xml':
        else:
            raise NotImplementedError("Unsupported output format: %s" % output_format)
        click.echo(s)