Example #1
0
def H_append(ctx):
    "Add srcfiles as items to list. Optionally provide pathexpr to list. "
    if not ctx.opts.args.srcfiles:
        return
    appendfile = get_dest(ctx, 'r')
    data = l = load_data( ctx.opts.flags.output_format, appendfile, ctx )
    if ctx.opts.args.pathexpr:
        l = data_at_path(ctx, None, data)
    for src in ctx.opts.args.srcfiles:
        fmt = get_format_for_fileext(src) or ctx.opts.flags.input_format
        mdata = load_data( fmt, open_file( src, 'in', ctx=ctx ), ctx )
        l.append(mdata)
    updatefile = get_dest(ctx, 'w+')
    return stdout_data( data, ctx, outf=updatefile )
Example #2
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))
Example #3
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)
Example #4
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))