Python ast.NodeVisitor() Examples
The following are 30
code examples of ast.NodeVisitor().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ast
, or try the search function
.
Example #1
Source File: setup.py From ubelt with Apache License 2.0 | 6 votes |
def parse_version(fpath): """ Statically parse the version number from a python file """ import ast if not exists(fpath): raise ValueError('fpath={!r} does not exist'.format(fpath)) with open(fpath, 'r') as file_: sourcecode = file_.read() pt = ast.parse(sourcecode) class VersionVisitor(ast.NodeVisitor): def visit_Assign(self, node): for target in node.targets: if getattr(target, 'id', None) == '__version__': self.version = node.value.s visitor = VersionVisitor() visitor.visit(pt) return visitor.version
Example #2
Source File: nodevisitor.py From python-lua with Apache License 2.0 | 6 votes |
def visit_all(self, nodes, inline=False): """Visit all nodes in the given list""" if not inline: last_ctx = self.context.last() last_ctx["locals"].push() visitor = NodeVisitor(context=self.context, config=self.config) if isinstance(nodes, list): for node in nodes: visitor.visit(node) if not inline: self.output.append(visitor.output) else: visitor.visit(nodes) if not inline: self.output.extend(visitor.output) if not inline: last_ctx = self.context.last() last_ctx["locals"].pop() if inline: return " ".join(visitor.output)
Example #3
Source File: PyAstUtil.py From ufora with Apache License 2.0 | 6 votes |
def visit_FunctionDef(self, node): if node.lineno == self.lineNumber: self.funcDefSubnodesAtLineNumber.append(node) ast.NodeVisitor.generic_visit(self, node)
Example #4
Source File: tf_upgrade.py From dlbench with MIT License | 6 votes |
def visit_Attribute(self, node): # pylint: disable=invalid-name """Handle bare Attributes i.e. [tf.foo, tf.bar]. Args: node: Node that is of type ast.Attribute """ full_name = self._get_attribute_full_path(node) if full_name and full_name.startswith("tf."): self._rename_functions(node, full_name) if full_name in self._api_change_spec.change_to_function: if not hasattr(node, "is_function_for_call"): new_text = full_name + "()" self._file_edit.add("Changed %r to %r"%(full_name, new_text), node.lineno, node.col_offset, full_name, new_text) ast.NodeVisitor.generic_visit(self, node)
Example #5
Source File: astparser.py From docassemble with MIT License | 6 votes |
def visit_Subscript(self, node): if hasattr(node.slice, 'value') and hasattr(node.slice.value, 'id'): self.stack.append('[' + str(node.slice.value.id) + ']') self.in_subscript += 1 self.seen_name = False elif hasattr(node.slice, 'value') and hasattr(node.slice.value, 'n'): self.stack.append('[' + str(node.slice.value.n) + ']') self.in_subscript += 1 self.seen_name = False elif hasattr(node.slice, 'value') and hasattr(node.slice.value, 's'): self.stack.append('[' + repr(str(node.slice.value.s)) + ']') self.in_subscript += 1 self.seen_name = False else: self.seen_complexity = 1 ast.NodeVisitor.generic_visit(self, node) if hasattr(node.slice, 'slice') and (hasattr(node.slice.value, 'id') or hasattr(node.slice.value, 'n')): self.in_subscript -= 1
Example #6
Source File: setup.py From ibeis with Apache License 2.0 | 6 votes |
def parse_version(fpath): """ Statically parse the version number from a python file """ import ast if not exists(fpath): raise ValueError('fpath={!r} does not exist'.format(fpath)) with open(fpath, 'r') as file_: sourcecode = file_.read() pt = ast.parse(sourcecode) class VersionVisitor(ast.NodeVisitor): def visit_Assign(self, node): for target in node.targets: if getattr(target, 'id', None) == '__version__': self.version = node.value.s visitor = VersionVisitor() visitor.visit(pt) return visitor.version
Example #7
Source File: super_setup.py From ibeis with Apache License 2.0 | 6 votes |
def parse_version(package): """ Statically parse the version number from __init__.py CommandLine: python -c "import setup; print(setup.parse_version('ovharn'))" """ from os.path import dirname, join import ast init_fpath = join(dirname(__file__), package, '__init__.py') with open(init_fpath) as file_: sourcecode = file_.read() pt = ast.parse(sourcecode) class VersionVisitor(ast.NodeVisitor): def visit_Assign(self, node): for target in node.targets: if target.id == '__version__': self.version = node.value.s visitor = VersionVisitor() visitor.visit(pt) return visitor.version
Example #8
Source File: astparser.py From docassemble with MIT License | 6 votes |
def visit_Assign(self, node): for key, val in ast.iter_fields(node): if key == 'targets': for subnode in val: if type(subnode) is ast.Tuple: for subsubnode in subnode.elts: crawler = myextract() crawler.visit(subsubnode) self.targets[fix_assign.sub(r'\1', ".".join(reversed(crawler.stack)))] = 1 else: crawler = myextract() crawler.visit(subnode) self.targets[fix_assign.sub(r'\1', ".".join(reversed(crawler.stack)))] = 1 self.depth += 1 #ast.NodeVisitor.generic_visit(self, node) self.generic_visit(node) self.depth -= 1
Example #9
Source File: test_warnings.py From GraphicDesignPatternByPython with MIT License | 6 votes |
def visit_Call(self, node): p = ParseCall() p.visit(node.func) ast.NodeVisitor.generic_visit(self, node) if p.ls[-1] == 'simplefilter' or p.ls[-1] == 'filterwarnings': if node.args[0].s == "ignore": self.bad_filters.append( "{}:{}".format(self.__filename, node.lineno)) if p.ls[-1] == 'warn' and ( len(p.ls) == 1 or p.ls[-2] == 'warnings'): if self.__filename == "_lib/tests/test_warnings.py": # This file return # See if stacklevel exists: if len(node.args) == 3: return args = {kw.arg for kw in node.keywords} if "stacklevel" not in args: self.bad_stacklevels.append( "{}:{}".format(self.__filename, node.lineno))
Example #10
Source File: defopt.py From defopt with MIT License | 6 votes |
def _parse_slice(string): slices = [] class SliceVisitor(ast.NodeVisitor): def visit_Slice(self, node): start = ast.literal_eval(node.lower) if node.lower else None stop = ast.literal_eval(node.upper) if node.upper else None step = ast.literal_eval(node.step) if node.step else None slices.append(slice(start, stop, step)) try: SliceVisitor().visit(ast.parse('_[{}]'.format(string))) sl, = slices except (SyntaxError, ValueError): raise ValueError('{} is not a valid slice string'.format(string)) return sl
Example #11
Source File: astparser.py From docassemble with MIT License | 5 votes |
def visit_ExceptHandler(self, node): self.illegal = True ast.NodeVisitor.generic_visit(self, node)
Example #12
Source File: analyzer.py From chalice with Apache License 2.0 | 5 votes |
def visit(self, node): # type: (ast.AST) -> None inferred_type = self._binder.get_type_for_node(node) if isinstance(inferred_type, Boto3ClientMethodCallType): self.api_calls.setdefault(inferred_type.service_name, set()).add( inferred_type.method_name) ast.NodeVisitor.visit(self, node)
Example #13
Source File: test_warnings.py From coffeegrindsize with MIT License | 5 votes |
def visit_Call(self, node): p = ParseCall() p.visit(node.func) ast.NodeVisitor.generic_visit(self, node) if p.ls[-1] == 'simplefilter' or p.ls[-1] == 'filterwarnings': if node.args[0].s == "ignore": raise AssertionError( "ignore filter should not be used; found in " "{} on line {}".format(self.__filename, node.lineno)) if p.ls[-1] == 'warn' and ( len(p.ls) == 1 or p.ls[-2] == 'warnings'): if "testing/tests/test_warnings.py" is self.__filename: # This file return # See if stacklevel exists: if len(node.args) == 3: return args = {kw.arg for kw in node.keywords} if "stacklevel" in args: return raise AssertionError( "warnings should have an appropriate stacklevel; found in " "{} on line {}".format(self.__filename, node.lineno))
Example #14
Source File: analyzer.py From chalice with Apache License 2.0 | 5 votes |
def visit(self, node): # type: (Any) -> None return ast.NodeVisitor.visit(self, node)
Example #15
Source File: astparser.py From docassemble with MIT License | 5 votes |
def visit_ClassDef(self, node): self.illegal = True ast.NodeVisitor.generic_visit(self, node)
Example #16
Source File: astparser.py From docassemble with MIT License | 5 votes |
def visit_Assign(self, node): self.illegal = True ast.NodeVisitor.generic_visit(self, node)
Example #17
Source File: astparser.py From docassemble with MIT License | 5 votes |
def visit_Return(self, node): self.illegal = True ast.NodeVisitor.generic_visit(self, node)
Example #18
Source File: astparser.py From docassemble with MIT License | 5 votes |
def visit_AugAssign(self, node): self.illegal = True ast.NodeVisitor.generic_visit(self, node)
Example #19
Source File: astparser.py From docassemble with MIT License | 5 votes |
def visit_Attribute(self, node): self.stack.append(node.attr) ast.NodeVisitor.generic_visit(self, node)
Example #20
Source File: test_warnings.py From coffeegrindsize with MIT License | 5 votes |
def visit_Attribute(self, node): ast.NodeVisitor.generic_visit(self, node) self.ls.append(node.attr)
Example #21
Source File: tf_upgrade.py From dlbench with MIT License | 5 votes |
def generic_visit(self, node): ast.NodeVisitor.generic_visit(self, node)
Example #22
Source File: Symbolic.py From nionswift with GNU General Public License v3.0 | 5 votes |
def parse_names(cls, expression): """Return the list of identifiers used in the expression.""" names = set() try: ast_node = ast.parse(expression, "ast") class Visitor(ast.NodeVisitor): def visit_Name(self, node): names.add(node.id) Visitor().visit(ast_node) except Exception: pass return names
Example #23
Source File: test_utils.py From vulture with MIT License | 5 votes |
def check_decorator_names(code, expected_names): decorator_names = [] def visit_FunctionDef(node): for decorator in node.decorator_list: decorator_names.append(utils.get_decorator_name(decorator)) node_visitor = ast.NodeVisitor() node_visitor.visit_AsyncFunctionDef = visit_FunctionDef node_visitor.visit_ClassDef = visit_FunctionDef node_visitor.visit_FunctionDef = visit_FunctionDef node_visitor.visit(ast.parse(code)) assert expected_names == decorator_names
Example #24
Source File: test_warnings.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def visit_Call(self, node): p = ParseCall() p.visit(node.func) ast.NodeVisitor.generic_visit(self, node) if p.ls[-1] == 'simplefilter' or p.ls[-1] == 'filterwarnings': if node.args[0].s == "ignore": raise AssertionError( "ignore filter should not be used; found in " "{} on line {}".format(self.__filename, node.lineno)) if p.ls[-1] == 'warn' and ( len(p.ls) == 1 or p.ls[-2] == 'warnings'): if "testing/tests/test_warnings.py" is self.__filename: # This file return # See if stacklevel exists: if len(node.args) == 3: return args = {kw.arg for kw in node.keywords} if "stacklevel" in args: return raise AssertionError( "warnings should have an appropriate stacklevel; found in " "{} on line {}".format(self.__filename, node.lineno))
Example #25
Source File: test_warnings.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def visit_Attribute(self, node): ast.NodeVisitor.generic_visit(self, node) self.ls.append(node.attr)
Example #26
Source File: lookuptable_connector.py From ABXpy with MIT License | 5 votes |
def generic_visit(self, node): # visit all children super(ast.NodeVisitor, self).generic_visit(node) # bubble up code_nodes from the children if any (cleaning up children # at the same time to keep sound asts) node.code_nodes = [] for child in ast.iter_child_nodes(node): node.code_nodes = node.code_nodes + child.code_nodes del(child.code_nodes)
Example #27
Source File: dbfun_compute.py From ABXpy with MIT License | 5 votes |
def __init__(self, *args, **kwargs): ast.NodeVisitor.__init__(self, *args, **kwargs) self.names = []
Example #28
Source File: CodeGenerate_By_Ast.py From ontology-python-compiler with GNU Lesser General Public License v3.0 | 5 votes |
def generic_visit(self, node): self.current_node = node # ast.NodeVisitor.generic_visit(self, node) ast.NodeVisitor.generic_visit(self, node)
Example #29
Source File: CodeGenerate_By_Ast.py From ontology-python-compiler with GNU Lesser General Public License v3.0 | 5 votes |
def generic_visit(self, node): ast.NodeVisitor.generic_visit(self, node)
Example #30
Source File: CodeGenerate_By_Ast.py From ontology-python-compiler with GNU Lesser General Public License v3.0 | 5 votes |
def generic_visit(self, node): self.current_node = node ast.NodeVisitor.generic_visit(self, node)