Esempio n. 1
0
def H_dump(ctx, write=True):
    "Read src and write destfile according to set i/o formats. "
    infile, outfile = get_src_dest_defaults(ctx)
    data = load_data( ctx.opts.flags.input_format, infile, ctx )
    if write:
        return stdout_data( data, ctx, outf=outfile )
    else:
        return data
Esempio n. 2
0
def H_path(ctx):

    """
    Return data at path. Return 1 if path is not found. Use with ``--is-*``
    opts to OR-test for type or exit 2. To check if a path could be inserted,
    use ``--is-new``. This overrules not-found errors, but only if the path
    could be inserted. When any existing
    element does not match a list or object type it also exits non-zero.
    """

    infile, outfile = get_src_dest_defaults(ctx)
    data = None
    try:
        data = data_at_path(ctx, infile)
        infile.close()
    except (Exception) as err:
        if not ctx.opts.flags.is_new:
            if not ctx.opts.flags.quiet:
                tb = traceback.format_exc()
                sys.stderr.write(tb)
                sys.stderr.write("Error: getting %r: %r\n" % (
                    ctx.opts.args.pathexpr, err ))
            return 1

    res = [ ]

    for tp in "new list obj int str bool".split(" "):
        if ctx.opts.flags["is_%s" % tp]:
            # FIXME: print(maptype(tp))
            if tp == "new":
                infile, outfile = get_src_dest_defaults(ctx)
                if not data and data_check_path(ctx, infile):
                    res += [ 0 ]
            elif isinstance(data, maptype(tp)):
                res += [ 0 ]
            else:
                res += [ 1 ]

    if res and min(res) == 0:
        res = [ 0 ]

    if not ctx.opts.flags.quiet:
        res += [ stdout_data( data, ctx, outf=outfile ) ]

    return max(res)
Esempio n. 3
0
def H_objectpath(ctx):
    infile, outfile = get_src_dest_defaults(ctx)
    data = load_data( ctx.opts.flags.input_format, infile, ctx )
    assert data
    q = Tree(data)
    assert q.data
    o = q.execute( ctx.opts.args.expr )
    if isinstance(o, types.GeneratorType):
        for s in o:
            v = stdout_data( s, ctx, outf=outfile )
            if v:
                return v
    else:
        return stdout_data( o, ctx, outf=outfile )
Esempio n. 4
0
def H_keys(ctx):
    "Output list of keys or indices"
    infile, outfile = get_src_dest_defaults(ctx)
    try:
        data = data_at_path(ctx, infile)
    except:
        return 1
    if not data:
        return 1
    if isinstance(data, dict):
        return stdout_data( data.keys(), ctx, outf=outfile )
    elif isinstance(data, list):
        return stdout_data( range(0, len(data)), ctx, outf=outfile )
    else:
        raise ValueError("Unhandled type %s" % type(data))
Esempio n. 5
0
def H_items(ctx):
    "Output for every key or item in object at path"
    infile, outfile = get_src_dest_defaults(ctx)
    try:
        data = data_at_path(ctx, infile)
    except:
        return 1
    if not data:
        return 1
    if isinstance(data, list):
        for item in data:
            stdout_data( item, ctx, outf=outfile )
    elif isinstance(data, dict):
        for key, value in data.items():
            subdata = { key: value }
            stdout_data( subdata, ctx, outf=outfile )
    else:
        raise ValueError("Unhandled type %s" % type(data))