Ejemplo n.º 1
0
def H_merge(ctx, write=True):
    """
    Merge srcfiles into last file. All srcfiles must be same format.

    Defaults to src-to-dest noop, iow. '- -' functions identical to
    'dump'.  """

    if not ctx.opts.args.srcfiles and not ctx.opts.flags.line_input:
        ctx.opts.args.srcfile = '-'
        return H_dump(ctx, write=write)

    if ctx.opts.flags.line_input:
        ctx.opts.args.srcfile = ctx.opts.args.srcfiles.pop(0)
        set_format('input', 'src', ctx.opts)
        inp = open_file(ctx.opts.args.srcfile, 'in', ctx=ctx)
        ctx.opts.args.srcfiles += [ StringIO(line) for line in inp.readlines() ]
    else:
        ctx.opts.args.srcfile = ctx.opts.args.srcfiles[0]
        set_format('input', 'src', ctx.opts)


    if not (ctx.opts.flags.list_union or ctx.opts.flags.list_update):
        if ctx.opts.flags.list_update_nodict:
            ctx.opts.flags.list_update = True
        else:
            ctx.opts.flags.list_union = True

    data = None
    for srcfile in ctx.opts.args.srcfiles:
        mdata = None
        if hasattr(srcfile, 'read'):
            infile = srcfile
        elif isinstance(srcfile, (dict, list)):
            mdata = srcfile
            srcfile = '<inline>'
            infile = None
        else:
            infile = open_file(srcfile, defio='in', ctx=ctx)
        if infile and not mdata:
            mdata = load_data( ctx.opts.flags.input_format, infile, ctx )
        if not data:
            data = type(mdata)()
        elif not isinstance(mdata, type(data)):
            raise ValueError( "Srcsfiles must have same root type. "\
                    "Expected %s, but found %s (%s)" % (
                            type(data), type(mdata), srcfile ) )
        if isinstance(data, dict):
            deep_update([data, mdata], ctx)
        elif isinstance(data, list):
            data = deep_union([data, mdata], ctx)
        else:
            raise ValueError(data)

    if write:
        if not ctx.opts.flags.quiet:
            sys.stderr.write("Writing to %s\n" % ctx.opts.args.destfile)
        outfile = open_file(ctx.opts.args.destfile, mode='w+', ctx=ctx)
        return stdout_data( data, ctx, outf=outfile )
    else:
        return data
Ejemplo n.º 2
0
def H_update(ctx):
    "Update srcfile from stdin. Write to destfile or stdout. "

    if not ctx.opts.args.srcfiles:
        return

    updatefile = get_dest(ctx, 'r')
    data = load_data( ctx.opts.flags.output_format, updatefile, ctx )
    updatefile.close()

    for cp in ctx.opts.flags.clear_paths:
        if cp.startswith('['):
            es = parse_json(cp)
        else:
            es = cp.split('/')
        cl = es.pop()
        v = data
        while es:
            el = es.pop(0)
            v = v[el]
        del v[cl]

    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 )
        deep_update([data, mdata], ctx)

    updatefile = get_dest(ctx, 'w+')
    return stdout_data( data, ctx, outf=updatefile )
Ejemplo n.º 3
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 )
Ejemplo n.º 4
0
def H_update(ctx):
    "Update srcfile from stdin. Write to destfile or stdout. "

    if not ctx.opts.args.srcfiles:
        return

    updatefile = get_dest(ctx, 'r')
    data = load_data( ctx.opts.flags.output_format, updatefile, ctx )
    updatefile.close()

    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 )

        deep_update([data, mdata], ctx)

    updatefile = get_dest(ctx, 'w+')
    return stdout_data( ctx.opts.flags.output_format, data, updatefile, ctx )
Ejemplo n.º 5
0
def H_update_at(ctx):
    """Update object at path, using data read from srcfile(s)"""
    if not ctx.opts.args.srcfiles:
        return
    updatefile = get_dest(ctx, 'r')
    data = o = load_data( ctx.opts.flags.output_format, updatefile, ctx )
    #if ctx.opts.args.pathexpr:
        #o = data_at_path(ctx, None, data)
    if ctx.opts.args.expr:
        q = Tree(data)
        assert q.data
        o = q.execute( ctx.opts.args.expr )
    if isinstance(o, types.GeneratorType):
        r = list(o)
        assert len(r) == 1, r
        o = r[0]
        #r = [ stdout_data( s, ctx, outf=sys.stdout) for s in o ]
        #print(r)
    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 )
        deep_update([o, mdata], ctx)
    updatefile = get_dest(ctx, 'w+')
    return stdout_data( data, ctx, outf=updatefile )