Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: third_party/logilab/logilab/common/visitor.py

Issue 1920403002: [content/test/gpu] Run pylint check of gpu tests in unittest instead of PRESUBMIT (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update path to LICENSE.txt of logilab/README.chromium Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
3 #
4 # This file is part of logilab-common.
5 #
6 # logilab-common is free software: you can redistribute it and/or modify it unde r
7 # the terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation, either version 2.1 of the License, or (at your option) an y
9 # later version.
10 #
11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 # details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License along
17 # with logilab-common. If not, see <http://www.gnu.org/licenses/>.
18 """A generic visitor abstract implementation.
19
20
21
22
23 """
24 __docformat__ = "restructuredtext en"
25
26 def no_filter(_):
27 return 1
28
29 # Iterators ###################################################################
30 class FilteredIterator(object):
31
32 def __init__(self, node, list_func, filter_func=None):
33 self._next = [(node, 0)]
34 if filter_func is None:
35 filter_func = no_filter
36 self._list = list_func(node, filter_func)
37
38 def __next__(self):
39 try:
40 return self._list.pop(0)
41 except :
42 return None
43
44 next = __next__
45
46 # Base Visitor ################################################################
47 class Visitor(object):
48
49 def __init__(self, iterator_class, filter_func=None):
50 self._iter_class = iterator_class
51 self.filter = filter_func
52
53 def visit(self, node, *args, **kargs):
54 """
55 launch the visit on a given node
56
57 call 'open_visit' before the beginning of the visit, with extra args
58 given
59 when all nodes have been visited, call the 'close_visit' method
60 """
61 self.open_visit(node, *args, **kargs)
62 return self.close_visit(self._visit(node))
63
64 def _visit(self, node):
65 iterator = self._get_iterator(node)
66 n = next(iterator)
67 while n:
68 result = n.accept(self)
69 n = next(iterator)
70 return result
71
72 def _get_iterator(self, node):
73 return self._iter_class(node, self.filter)
74
75 def open_visit(self, *args, **kargs):
76 """
77 method called at the beginning of the visit
78 """
79 pass
80
81 def close_visit(self, result):
82 """
83 method called at the end of the visit
84 """
85 return result
86
87 # standard visited mixin ######################################################
88 class VisitedMixIn(object):
89 """
90 Visited interface allow node visitors to use the node
91 """
92 def get_visit_name(self):
93 """
94 return the visit name for the mixed class. When calling 'accept', the
95 method <'visit_' + name returned by this method> will be called on the
96 visitor
97 """
98 try:
99 return self.TYPE.replace('-', '_')
100 except:
101 return self.__class__.__name__.lower()
102
103 def accept(self, visitor, *args, **kwargs):
104 func = getattr(visitor, 'visit_%s' % self.get_visit_name())
105 return func(self, *args, **kwargs)
106
107 def leave(self, visitor, *args, **kwargs):
108 func = getattr(visitor, 'leave_%s' % self.get_visit_name())
109 return func(self, *args, **kwargs)
OLDNEW
« no previous file with comments | « third_party/logilab/logilab/common/vcgutils.py ('k') | third_party/logilab/logilab/common/xmlrpcutils.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698