OLD | NEW |
1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # | 3 # |
4 # This file is part of logilab-common. | 4 # This file is part of logilab-common. |
5 # | 5 # |
6 # logilab-common is free software: you can redistribute it and/or modify it unde
r | 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 | 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 | 8 # Software Foundation, either version 2.1 of the License, or (at your option) an
y |
9 # later version. | 9 # later version. |
10 # | 10 # |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 # Iterators ################################################################### | 29 # Iterators ################################################################### |
30 class FilteredIterator(object): | 30 class FilteredIterator(object): |
31 | 31 |
32 def __init__(self, node, list_func, filter_func=None): | 32 def __init__(self, node, list_func, filter_func=None): |
33 self._next = [(node, 0)] | 33 self._next = [(node, 0)] |
34 if filter_func is None: | 34 if filter_func is None: |
35 filter_func = no_filter | 35 filter_func = no_filter |
36 self._list = list_func(node, filter_func) | 36 self._list = list_func(node, filter_func) |
37 | 37 |
38 def next(self): | 38 def __next__(self): |
39 try: | 39 try: |
40 return self._list.pop(0) | 40 return self._list.pop(0) |
41 except : | 41 except : |
42 return None | 42 return None |
43 | 43 |
| 44 next = __next__ |
| 45 |
44 # Base Visitor ################################################################ | 46 # Base Visitor ################################################################ |
45 class Visitor(object): | 47 class Visitor(object): |
46 | 48 |
47 def __init__(self, iterator_class, filter_func=None): | 49 def __init__(self, iterator_class, filter_func=None): |
48 self._iter_class = iterator_class | 50 self._iter_class = iterator_class |
49 self.filter = filter_func | 51 self.filter = filter_func |
50 | 52 |
51 def visit(self, node, *args, **kargs): | 53 def visit(self, node, *args, **kargs): |
52 """ | 54 """ |
53 launch the visit on a given node | 55 launch the visit on a given node |
54 | 56 |
55 call 'open_visit' before the beginning of the visit, with extra args | 57 call 'open_visit' before the beginning of the visit, with extra args |
56 given | 58 given |
57 when all nodes have been visited, call the 'close_visit' method | 59 when all nodes have been visited, call the 'close_visit' method |
58 """ | 60 """ |
59 self.open_visit(node, *args, **kargs) | 61 self.open_visit(node, *args, **kargs) |
60 return self.close_visit(self._visit(node)) | 62 return self.close_visit(self._visit(node)) |
61 | 63 |
62 def _visit(self, node): | 64 def _visit(self, node): |
63 iterator = self._get_iterator(node) | 65 iterator = self._get_iterator(node) |
64 n = iterator.next() | 66 n = next(iterator) |
65 while n: | 67 while n: |
66 result = n.accept(self) | 68 result = n.accept(self) |
67 n = iterator.next() | 69 n = next(iterator) |
68 return result | 70 return result |
69 | 71 |
70 def _get_iterator(self, node): | 72 def _get_iterator(self, node): |
71 return self._iter_class(node, self.filter) | 73 return self._iter_class(node, self.filter) |
72 | 74 |
73 def open_visit(self, *args, **kargs): | 75 def open_visit(self, *args, **kargs): |
74 """ | 76 """ |
75 method called at the beginning of the visit | 77 method called at the beginning of the visit |
76 """ | 78 """ |
77 pass | 79 pass |
(...skipping 20 matching lines...) Expand all Loading... |
98 except: | 100 except: |
99 return self.__class__.__name__.lower() | 101 return self.__class__.__name__.lower() |
100 | 102 |
101 def accept(self, visitor, *args, **kwargs): | 103 def accept(self, visitor, *args, **kwargs): |
102 func = getattr(visitor, 'visit_%s' % self.get_visit_name()) | 104 func = getattr(visitor, 'visit_%s' % self.get_visit_name()) |
103 return func(self, *args, **kwargs) | 105 return func(self, *args, **kwargs) |
104 | 106 |
105 def leave(self, visitor, *args, **kwargs): | 107 def leave(self, visitor, *args, **kwargs): |
106 func = getattr(visitor, 'leave_%s' % self.get_visit_name()) | 108 func = getattr(visitor, 'leave_%s' % self.get_visit_name()) |
107 return func(self, *args, **kwargs) | 109 return func(self, *args, **kwargs) |
OLD | NEW |