def parse_import(self, node: ast.Import): for alias_name in node.names: for mod in filter(None, alias_name.name.split('.')): self.ast_tree_obj.line_structure[self.ast_tree_obj.get_lineno( node.lineno)][mod].append( NodeInfo( node_string=alias_name.name, node_type='module', col_offset=node.col_offset, node_id=str(uuid.uuid4()), scope_id=self.ast_tree_obj.scope_id, action=ast. Store, # TODO fix it and pass to standart lib parent_scope_id=self.ast_tree_obj.parent_scope_id, child_scope_id=None, lineno=self.ast_tree_obj.get_lineno(node.lineno))) if alias_name.asname: self.ast_tree_obj.line_structure[self.ast_tree_obj.get_lineno( node.lineno)][alias_name.asname].append( NodeInfo( node_string=alias_name.asname, node_type='module_alias', col_offset=node.col_offset, node_id=str(uuid.uuid4()), action=ast.Store, scope_id=self.ast_tree_obj.scope_id, parent_scope_id=self.ast_tree_obj.parent_scope_id, child_scope_id=None, lineno=self.ast_tree_obj.get_lineno(node.lineno)))
def parse_argument(self, node: ast.arg): self.ast_tree_obj.line_structure[self.ast_tree_obj.get_lineno( node.lineno)][node.arg].append( NodeInfo(node_string=node.arg, node_type='argument', action=ast.Store, col_offset=node.col_offset, node_id=str(uuid.uuid4()), scope_id=self.ast_tree_obj.scope_id, parent_scope_id=self.ast_tree_obj.parent_scope_id, child_scope_id=None, lineno=self.ast_tree_obj.get_lineno(node.lineno)))
def parse_name(self, node: ast.Name): node_info = NodeInfo(node_string=node.id, node_type='name', col_offset=node.col_offset, node_id=str(uuid.uuid4()), action=node.ctx.__class__, scope_id=self.ast_tree_obj.scope_id, parent_scope_id=self.ast_tree_obj.parent_scope_id, child_scope_id=None, lineno=self.ast_tree_obj.get_lineno(node.lineno)) self.ast_tree_obj.line_structure[self.ast_tree_obj.get_lineno( node.lineno)][node.id].append(node_info) return node_info
def parse_call(self, node: ast.Call): if node.func.__class__ is ast.Name: node_info = NodeInfo( node_string=node.func.id, node_type='name', col_offset=node.col_offset, node_id=str(uuid.uuid4()), scope_id=self.ast_tree_obj.scope_id, parent_scope_id=self.ast_tree_obj.parent_scope_id, lineno=self.ast_tree_obj.get_lineno(node.lineno)) self.ast_tree_obj.line_structure[self.ast_tree_obj.get_lineno( node.lineno)][node.func.id].append(node_info) else: node_info = self.parsers[node.func.__class__](node.func) for argument in node.args: self.parsers[argument.__class__](argument) return node_info
def parse_class(self, node): # TODO looks like function def ttry to merge for _, decorator in enumerate(node.decorator_list): self.parsers[decorator.__class__](decorator) with ScopeContextManager(self.ast_tree_obj, node) as scope: for body_obj in node.body: self.parsers[body_obj.__class__](body_obj) node_line = node.lineno + len(node.decorator_list) self.ast_tree_obj.line_structure[self.ast_tree_obj.get_lineno( node_line)][node.name].append( NodeInfo(node_string=node.name, node_type='class', action=ast.Store, col_offset=node.col_offset, node_id=str(uuid.uuid4()), scope_id=self.ast_tree_obj.scope_id, parent_scope_id=self.ast_tree_obj.parent_scope_id, child_scope_id=scope.new_scope_id, lineno=self.ast_tree_obj.get_lineno(node_line)))
def parse_assing(self, node: ast.Assign): value_node_info = self.parsers[node.value.__class__](node.value) for target in node.targets: if target.__class__ == ast.Name: self.ast_tree_obj.line_structure[self.ast_tree_obj.get_lineno( node.lineno)][target.id].append( NodeInfo( node_string=target.id, node_type='name', col_offset=node.col_offset, node_id=str(uuid.uuid4()), action=ast.Store, scope_id=self.ast_tree_obj.scope_id, parent_scope_id=self.ast_tree_obj.parent_scope_id, child_scope_id=None, lineno=self.ast_tree_obj.get_lineno(node.lineno), value=value_node_info)) else: self.parsers[target.__class__](target)
def parse_attribute(self, node: ast.Attribute): value_node_info = self.parsers[node.value.__class__](node.value) node_info = None if value_node_info: node_info = NodeInfo( node_string=node.attr, node_type='attribute', owner=value_node_info, col_offset=node.col_offset, node_id=str(uuid.uuid4()), scope_id=self.ast_tree_obj.scope_id, parent_scope_id=self.ast_tree_obj.parent_scope_id, child_scope_id=None, lineno=self.ast_tree_obj.get_lineno(node.lineno)) self.ast_tree_obj.line_structure[self.ast_tree_obj.get_lineno( node.lineno)][node.attr].append(node_info) node_info = node_info else: # for builtins we will be here pass return node_info
def parse_function(self, node): for _, decorator in enumerate(node.decorator_list): self.parsers[decorator.__class__](decorator) if node.returns: self.parsers[node.returns.__class__](node.returns) with ScopeContextManager(self.ast_tree_obj, node) as scope: for argument in node.args.args: self.parsers[argument.__class__](argument) for body_obj in node.body: self.parsers[body_obj.__class__](body_obj) node_line = node.lineno + len(node.decorator_list) self.ast_tree_obj.line_structure[self.ast_tree_obj.get_lineno( node_line)][node.name].append( NodeInfo(node_string=node.name, node_type='function', action=ast.Store, col_offset=node.col_offset, node_id=str(uuid.uuid4()), scope_id=self.ast_tree_obj.scope_id, parent_scope_id=self.ast_tree_obj.parent_scope_id, child_scope_id=scope.new_scope_id, lineno=self.ast_tree_obj.get_lineno(node_line)))