Robot Framework
visitor.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import ast
17 
18 
20 
21  def _find_visitor(self, cls):
22  if cls is ast.AST:
23  return None
24  method = 'visit_' + cls.__name__
25  if hasattr(self, method):
26  return getattr(self, method)
27  for base in cls.__bases__:
28  visitor = self._find_visitor_find_visitor(base)
29  if visitor:
30  return visitor
31  return None
32 
33 
34 
45 class ModelVisitor(ast.NodeVisitor, VisitorFinder):
46 
47  def visit(self, node):
48  visitor = self._find_visitor_find_visitor(type(node)) or self.generic_visit
49  visitor(node)
50 
51 
52 
58 class ModelTransformer(ast.NodeTransformer, VisitorFinder):
59 
60  def visit(self, node):
61  visitor = self._find_visitor_find_visitor(type(node)) or self.generic_visit
62  return visitor(node)
NodeTransformer that supports matching nodes based on their base classes.
Definition: visitor.py:58
NodeVisitor that supports matching nodes based on their base classes.
Definition: visitor.py:45