def main(argv=None, stdin=None): parser = argparse.ArgumentParser( description='Convert (parts of) a GCL model file to JSON.') parser.add_argument('file', metavar='FILE', type=str, nargs='?', help='File to parse') parser.add_argument( 'selectors', metavar='SELECTOR', type=str, nargs='*', help= 'Subnodes to convert. The first selector will be treated as the root of the printed output.' ) args = parser.parse_args(argv or sys.argv[1:]) try: if args.file and args.file != '-': model = gcl.load(args.file) else: model = gcl.loads((stdin or sys.stdin).read(), filename='<stdin>') sels = query.GPath(args.selectors) if not sels.everything(): model = sels.select(model).deep() plain = util.to_python(model) sys.stdout.write(json.dumps(plain)) except (gcl.ParseError, RuntimeError) as e: sys.stderr.write(str(e) + '\n') sys.exit(1)
def testPathSelectors(self): sel = query.GPath(['x.y.z', 'x.q.z']) self.assertEquals([ (['x', 'y', 'z'], 3), (['x', 'q', 'z'], 4), ], sel.select(self.model).paths_values())
def print_selectors(model, selectors, printer, **kwargs): sels = query.GPath(selectors) if sels.everything(): printer(model, **kwargs) else: for path, value in sels.select(model).paths_values(): try: print(util.Color.colorize('.'.join(path), 'yellow')) printer(value, **kwargs) print('') except gcl.EvaluationError as e: print(util.Color.colorize(str(e), 'red'))
def testMultipleQueriesWithStar(self): """Check that if we have multiple selectors with * in it, we keep the indexes consistent.""" sel = query.GPath([ 'list.*.item1', 'list.*.item2', ]) # Kinda know it breaks if the orders are different model = gcl.loads(""" list = [ { item2 = 1 }, { item1 = 5 }, ] """) result = sel.select(model).deep() self.assertEquals([{"item2": 1}, {"item1": 5}], result["list"])
def main(argv=None, stdin=None): parser = argparse.ArgumentParser( description='Convert (parts of) a GCL model file to JSON.') parser.add_argument('file', metavar='FILE', type=str, nargs='?', help='File to parse') parser.add_argument('selectors', metavar='SELECTOR', type=str, nargs='*', help='Select nodes to include in the JSON.') parser.add_argument( '--root', '-r', metavar='PATH', type=str, default='', help= 'Use the indicated root path as the root of the output JSON object (like a.b.c but without wildcards)' ) args = parser.parse_args(argv or sys.argv[1:]) try: if args.file and args.file != '-': model = gcl.load(args.file) else: model = gcl.loads((stdin or sys.stdin).read(), filename='<stdin>') sels = query.GPath(args.selectors) if not sels.everything(): model = sels.select(model).deep() plain = util.to_python(model) selectors = args.root.split('.') if args.root else [] selected = select(plain, selectors) sys.stdout.write(json.dumps(selected, indent=2)) except (gcl.ParseError, RuntimeError) as e: sys.stderr.write(str(e) + '\n') sys.exit(1)
def testListIndexDeep(self): sel = query.GPath('scalar_list.[1]') result = sel.select(self.model).deep() self.assertEquals([2], result['scalar_list'])
def testListIndex(self): sel = query.GPath('scalar_list.[1]') result = sel.select(self.model).first() self.assertEquals(2, result)
def testListSelectors(self): sel = query.GPath('obj_list.*') result = sel.select(self.model).deep() self.assertTrue(isinstance(result['obj_list'], list))
def testListSelectorsSub(self): sel = query.GPath('obj_list.*.x') self.assertEquals([1, 2, 3], list(sel.select(self.model).values()))
def testMultiSelectors(self): sel = query.GPath(['x.y.z', 'x.q.z']) self.assertEquals([3, 4], list(sel.select(self.model).values()))
def testMultiParse(self): sel = query.GPath('x.{y,q}.z') self.assertEquals([3, 4], list(sel.select(self.model).values()))
def testStarParse(self): sel = query.GPath('x.*.z') self.assertEquals(set([3, 4]), set(sel.select(self.model).values()))
def testSimpleParseDeep(self): sel = query.GPath('x.y.z') deep = sel.select(self.model).deep() self.assertEquals({'x': {'y': {'z': 3}}}, deep)
def testSimpleParse(self): sel = query.GPath('x.y.z') self.assertEquals(3, sel.select(self.model).first())