def test_data_check_path( self, testnr, pathexpr, expected ): """ data-check-path should evalue path expression and return data """ infile = StringIO('{"foo":[{"bar":null}]}') ctx = confparse.Values(dict( opts=util.get_opts(jsotk.__doc__, argv=['path', '', pathexpr]) )) self.assertEquals( ctx.opts.args.pathexpr, pathexpr ) is_new = jsotk_lib.data_check_path( ctx, infile ) self.assert_( is_new == expected, testnr )
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)