示例#1
0
    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:
            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)

            print "Indexes that match the pattern:"
            count = 0
            for index in t.indexes(regex, node):
                print "    [" + ", ".join(display(i) for i in index) + "]"
                count += 1
            if count == 0:
                print "    (none)"
示例#2
0
    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:
            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)

            print "Indexes that match the pattern:"
            count = 0
            for index in t.indexes(regex, node):
                print "    [" + ", ".join(display(i) for i in index) + "]"
                count += 1
            if count == 0:
                print "    (none)"
示例#3
0
    def action(self, args):
        if len(args) == 1 and args[0] == parser.Word("help"):
            print self.help
        else:
            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)

            print "Indexes that match the pattern:"
            count = 0
            for index in t.indexes(regex, node):
                print "    [" + ", ".join(display(i) for i in index) + "]"
                count += 1
            if count == 0:
                print "    (none)"
示例#4
0
    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)
示例#5
0
    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)