def action(self, args): if len(args) == 1 and args[0] == parser.Word("help"): print self.help else: options = {"pretty": True} while len(args) > 0 and isinstance(args[-1], parser.Option): opt = args.pop() if opt.word.text in ["pretty"]: try: options[opt.word.text] = opt.value.value() except TypeError: raise InspectorError("illegal value for {0}".format(opt.word.text)) else: raise InspectorError("option {0} unrecognized".format(opt.word.text)) if not isinstance(options["pretty"], bool): raise InspectorError("pretty must be boolean") if len(args) == 1 and isinstance(args[0], parser.Word): if args[0].text not in self.mode.pfaFiles: raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text)) node = self.mode.pfaFiles[args[0].text].obj else: self.syntaxError() try: node = node[self.name] # "input" or "output" except KeyError: raise InspectorError("PFA document \"{0}\" is missing {1} section')".format(args[0].text, self.name)) if options["pretty"]: print avscToPretty(node) else: t.look(node, inlineDepth=1)
def action(self, args): """Perform the action associated with this command. :type args: list of titus.inspector.parser.Ast :param args: arguments passed to the command :rtype: ``None`` :return: nothing; results must be printed to the screen """ if len(args) == 1 and args[0] == parser.Word("help"): print self.help else: options = {"pretty": True} while len(args) > 0 and isinstance(args[-1], parser.Option): opt = args.pop() if opt.word.text in ["pretty"]: try: options[opt.word.text] = opt.value.value() except TypeError: raise InspectorError("illegal value for {0}".format( opt.word.text)) else: raise InspectorError("option {0} unrecognized".format( opt.word.text)) if not isinstance(options["pretty"], bool): raise InspectorError("pretty must be boolean") if len(args) == 1 and isinstance(args[0], parser.Word): if args[0].text not in self.mode.pfaFiles: raise InspectorError( "no PFA document named \"{0}\" in memory (try 'load <file> as {1}')" .format(args[0].text, args[0].text)) model = self.mode.pfaFiles[args[0].text] node = model.obj else: self.syntaxError() try: node = node[self.name] # "input" or "output" except KeyError: raise InspectorError( "PFA document \"{0}\" is missing {1} section')".format( args[0].text, self.name)) if isinstance(node, basestring): names = model.engine.parser.names.names.keys() if node in names: node = model.engine.parser.getAvroType(node).jsonNode( set()) if options["pretty"]: print avscToPretty(node) else: t.look(node, inlineDepth=1)
def action(self, args): """Perform the action associated with this command. :type args: list of titus.inspector.parser.Ast :param args: arguments passed to the command :rtype: ``None`` :return: nothing; results must be printed to the screen """ if len(args) == 1 and args[0] == parser.Word("help"): print self.help else: options = {"pretty": True} while len(args) > 0 and isinstance(args[-1], parser.Option): opt = args.pop() if opt.word.text in ["pretty"]: try: options[opt.word.text] = opt.value.value() except TypeError: raise InspectorError("illegal value for {0}".format(opt.word.text)) else: raise InspectorError("option {0} unrecognized".format(opt.word.text)) if not isinstance(options["pretty"], bool): raise InspectorError("pretty must be boolean") if len(args) == 1 and isinstance(args[0], parser.Word): if args[0].text not in self.mode.pfaFiles: raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text)) model = self.mode.pfaFiles[args[0].text] node = model.obj else: self.syntaxError() try: node = node[self.name] # "input" or "output" except KeyError: raise InspectorError("PFA document \"{0}\" is missing {1} section')".format(args[0].text, self.name)) if isinstance(node, basestring): names = model.engine.parser.names.names.keys() if node in names: node = model.engine.parser.getAvroType(node).jsonNode(set()) if options["pretty"]: print avscToPretty(node) else: t.look(node, inlineDepth=1)
def action(self, args): """Perform the action associated with this command. :type args: list of titus.inspector.parser.Ast :param args: arguments passed to the command :rtype: ``None`` :return: nothing; results must be printed to the screen """ if len(args) == 1 and args[0] == parser.Word("help"): print(self.help) else: options = {"pretty": True} while len(args) > 0 and isinstance(args[-1], parser.Option): opt = args.pop() if opt.word.text in ["pretty"]: try: options[opt.word.text] = opt.value.value() except TypeError: raise InspectorError("illegal value for {0}".format(opt.word.text)) else: raise InspectorError("option {0} unrecognized".format(opt.word.text)) if not isinstance(options["pretty"], bool): raise InspectorError("pretty must be boolean") if len(args) == 1 and isinstance(args[0], parser.Word): if args[0].text not in self.mode.pfaFiles: raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text)) model = self.mode.pfaFiles[args[0].text] else: self.syntaxError() names = sorted(model.engine.parser.names.names.keys()) for index, name in enumerate(names): node = model.engine.parser.getAvroType(name).jsonNode(set()) print(name + ":") if options["pretty"]: print(avscToPretty(node, 4)) else: t.look(node, inlineDepth=1) if index != len(names) - 1: print() if len(names) == 0: print("PFA document contains no named types")
def action(self, args): if len(args) == 1 and args[0] == parser.Word("help"): print self.help else: options = {"maxDepth": 8, "indexWidth": 30} while len(args) > 0 and isinstance(args[-1], parser.Option): opt = args.pop() if opt.word.text in ["maxDepth", "indexWidth"]: try: options[opt.word.text] = opt.value.value() except TypeError: raise InspectorError("illegal value for {0}".format(opt.word.text)) else: raise InspectorError("option {0} unrecognized".format(opt.word.text)) if not isinstance(options["maxDepth"], (int, long)) or options["maxDepth"] <= 0: raise InspectorError("maxDepth must be a positive integer") if not isinstance(options["indexWidth"], (int, long)) or options["indexWidth"] <= 0: raise InspectorError("indexWidth must be a positive integer") if len(args) == 1 and isinstance(args[0], parser.Word): if args[0].text not in self.mode.pfaFiles: raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text)) node = self.mode.pfaFiles[args[0].text].obj elif len(args) == 1 and isinstance(args[0], parser.Extract): if args[0].text not in self.mode.pfaFiles: raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text)) node = self.mode.pfaFiles[args[0].text].obj items = args[0].items node = extaction(args[0], node, items) else: self.syntaxError() if not depthGreaterThan(node, 0): print json.dumps(node) else: content = StringIO.StringIO() if not depthGreaterThan(node, 1): t.look(node, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=0, stream=content) elif not depthGreaterThan(node, 2): t.look(node, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=1, stream=content) else: t.look(node, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=2, stream=content) content = content.getvalue() if content.count("\n") <= 100: print content else: proc = pipe("less") try: proc.stdin.write(content) except IOError as err: if str(err) != "[Errno 32] Broken pipe": raise pipewait(proc)
def action(self, args): if len(args) == 1 and args[0] == parser.Word("help"): print self.help else: options = {"pretty": True} while len(args) > 0 and isinstance(args[-1], parser.Option): opt = args.pop() if opt.word.text in ["pretty"]: try: options[opt.word.text] = opt.value.value() except TypeError: raise InspectorError("illegal value for {0}".format(opt.word.text)) else: raise InspectorError("option {0} unrecognized".format(opt.word.text)) if not isinstance(options["pretty"], bool): raise InspectorError("pretty must be boolean") if len(args) == 1 and isinstance(args[0], parser.Word): if args[0].text not in self.mode.pfaFiles: raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text)) model = self.mode.pfaFiles[args[0].text] else: self.syntaxError() names = sorted(model.engine.parser.names.names.keys()) for index, name in enumerate(names): node = model.engine.parser.getAvroType(name).jsonNode(set()) print name + ":" if options["pretty"]: print avscToPretty(node, 4) else: t.look(node, inlineDepth=1) if index != len(names) - 1: print if len(names) == 0: print "PFA document contains no named types"
def action(self, args): """Perform the action associated with this command. :type args: list of titus.inspector.parser.Ast :param args: arguments passed to the command :rtype: ``None`` :return: nothing; results must be printed to the screen """ if len(args) == 1 and args[0] == parser.Word("help"): print self.help else: options = {"maxDepth": 3, "indexWidth": 30} while len(args) > 0 and isinstance(args[-1], parser.Option): opt = args.pop() if opt.word.text in ["maxDepth", "indexWidth"]: try: options[opt.word.text] = opt.value.value() except TypeError: raise InspectorError("illegal value for {0}".format(opt.word.text)) else: raise InspectorError("option {1} unrecognized".format(opt.word.text)) if not isinstance(options["maxDepth"], (int, long)) or options["maxDepth"] <= 0: raise InspectorError("maxDepth must be a positive integer") if not isinstance(options["indexWidth"], (int, long)) or options["indexWidth"] <= 0: raise InspectorError("indexWidth must be a positive integer") if len(args) == 2 and isinstance(args[0], parser.Word): if args[0].text not in self.mode.pfaFiles: raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text)) node = self.mode.pfaFiles[args[0].text].obj elif len(args) == 2 and isinstance(args[0], parser.Extract): if args[0].text not in self.mode.pfaFiles: raise InspectorError("no PFA document named \"{0}\" in memory (try 'load <file> as {1}')".format(args[0].text, args[0].text)) node = self.mode.pfaFiles[args[0].text].obj items = args[0].items node = extaction(args[0], node, items) else: self.syntaxError() regex = args[-1].regex() def display(i): if isinstance(i, basestring): if " " in i: return json.dumps(i) else: return i else: return str(i) content = StringIO.StringIO() count = 0 for index in t.indexes(regex, node): content.write("At index [" + ", ".join(display(i) for i in index) + "]:\n") matched = t.get(node, index) if not depthGreaterThan(matched, 0): content.write(json.dumps(matched) + "\n") elif not depthGreaterThan(matched, 1): t.look(matched, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=0, stream=content) elif not depthGreaterThan(matched, 2): t.look(matched, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=1, stream=content) else: t.look(matched, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=2, stream=content) content.write("\n") count += 1 if count == 0: print " (none)" content = content.getvalue() if content.count("\n") <= 100: print content else: proc = pipe("less") try: proc.stdin.write(content) except IOError as err: if str(err) != "[Errno 32] Broken pipe": raise pipewait(proc)
def action(self, args): """Perform the action associated with this command. :type args: list of titus.inspector.parser.Ast :param args: arguments passed to the command :rtype: ``None`` :return: nothing; results must be printed to the screen """ if len(args) == 1 and args[0] == parser.Word("help"): print self.help else: options = {"maxDepth": 8, "indexWidth": 30} while len(args) > 0 and isinstance(args[-1], parser.Option): opt = args.pop() if opt.word.text in ["maxDepth", "indexWidth"]: try: options[opt.word.text] = opt.value.value() except TypeError: raise InspectorError("illegal value for {0}".format( opt.word.text)) else: raise InspectorError("option {0} unrecognized".format( opt.word.text)) if not isinstance(options["maxDepth"], (int, long)) or options["maxDepth"] <= 0: raise InspectorError("maxDepth must be a positive integer") if not isinstance(options["indexWidth"], (int, long)) or options["indexWidth"] <= 0: raise InspectorError("indexWidth must be a positive integer") if len(args) == 1 and isinstance(args[0], parser.Word): if args[0].text not in self.mode.pfaFiles: raise InspectorError( "no PFA document named \"{0}\" in memory (try 'load <file> as {1}')" .format(args[0].text, args[0].text)) node = self.mode.pfaFiles[args[0].text].obj elif len(args) == 1 and isinstance(args[0], parser.Extract): if args[0].text not in self.mode.pfaFiles: raise InspectorError( "no PFA document named \"{0}\" in memory (try 'load <file> as {1}')" .format(args[0].text, args[0].text)) node = self.mode.pfaFiles[args[0].text].obj items = args[0].items node = extaction(args[0], node, items) else: self.syntaxError() if not depthGreaterThan(node, 0): print json.dumps(node) else: content = StringIO.StringIO() if not depthGreaterThan(node, 1): t.look(node, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=0, stream=content) elif not depthGreaterThan(node, 2): t.look(node, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=1, stream=content) else: t.look(node, maxDepth=options["maxDepth"], indexWidth=options["indexWidth"], inlineDepth=2, stream=content) content = content.getvalue() if content.count("\n") <= 100: print content else: proc = pipe("less") try: proc.stdin.write(content) except IOError as err: if str(err) != "[Errno 32] Broken pipe": raise pipewait(proc)