Пример #1
0
    def process(self, code, raw=False):
        tree = ast.parse(code) if isinstance(code, str) else code

        nodes = []
        for node in ast.walk(tree):
            if isinstance(node, type(self.pattern)) and is_ast_equal(
                    node, self.pattern):
                if not raw:
                    nodes.append(convert_node(node))
                else:
                    nodes.append(node)

        return nodes
Пример #2
0
    def __init__(self, file_name, nodes):

        ssg = Path.cwd() / "ssg"
        ext = ssg / "extensions"

        self.data = {
            "success": True,
            "full_path": "",
            "message": "",
            "start_pos": 0,
            "nodes": nodes,
        }

        if file_name is not None:
            path = lambda root, fn: root / "{}.py".format(fn)
            if file_name == "menu" or file_name == "stats":
                full_path = path(ext, file_name)
            elif file_name == "ssg":
                full_path = path(Path.cwd(), file_name)
            else:
                full_path = path(ssg, file_name)

            grammar = parso.load_grammar()
            module = grammar.parse(path=full_path)
            self.data["success"] = len(grammar.iter_errors(module)) == 0

        if self.data["success"]:
            self.data["message"] = "Syntax: valid"
            if file_name is not None:
                self.data["nodes"] = convert_node(
                    ast.parse(full_path.read_text()))
                self.data["code"] = full_path.read_text()

        else:
            self.data["message"] = grammar.iter_errors(module)[0].message
            self.data["start_pos"] = grammar.iter_errors(
                module)[0].start_pos[0]
Пример #3
0
 def query_raw(self, pattern):
     nodes = Template(pattern).process(self.code, True)
     if isinstance(nodes, list) and len(nodes) == 1:
         nodes = [flatten(convert_node(node)) for node in nodes[0].body]
     return Parser(None, nodes)