| 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 | |
| 46 # Base Visitor ################################################################ | 44 # Base Visitor ################################################################ |
| 47 class Visitor(object): | 45 class Visitor(object): |
| 48 | 46 |
| 49 def __init__(self, iterator_class, filter_func=None): | 47 def __init__(self, iterator_class, filter_func=None): |
| 50 self._iter_class = iterator_class | 48 self._iter_class = iterator_class |
| 51 self.filter = filter_func | 49 self.filter = filter_func |
| 52 | 50 |
| 53 def visit(self, node, *args, **kargs): | 51 def visit(self, node, *args, **kargs): |
| 54 """ | 52 """ |
| 55 launch the visit on a given node | 53 launch the visit on a given node |
| 56 | 54 |
| 57 call 'open_visit' before the beginning of the visit, with extra args | 55 call 'open_visit' before the beginning of the visit, with extra args |
| 58 given | 56 given |
| 59 when all nodes have been visited, call the 'close_visit' method | 57 when all nodes have been visited, call the 'close_visit' method |
| 60 """ | 58 """ |
| 61 self.open_visit(node, *args, **kargs) | 59 self.open_visit(node, *args, **kargs) |
| 62 return self.close_visit(self._visit(node)) | 60 return self.close_visit(self._visit(node)) |
| 63 | 61 |
| 64 def _visit(self, node): | 62 def _visit(self, node): |
| 65 iterator = self._get_iterator(node) | 63 iterator = self._get_iterator(node) |
| 66 n = next(iterator) | 64 n = iterator.next() |
| 67 while n: | 65 while n: |
| 68 result = n.accept(self) | 66 result = n.accept(self) |
| 69 n = next(iterator) | 67 n = iterator.next() |
| 70 return result | 68 return result |
| 71 | 69 |
| 72 def _get_iterator(self, node): | 70 def _get_iterator(self, node): |
| 73 return self._iter_class(node, self.filter) | 71 return self._iter_class(node, self.filter) |
| 74 | 72 |
| 75 def open_visit(self, *args, **kargs): | 73 def open_visit(self, *args, **kargs): |
| 76 """ | 74 """ |
| 77 method called at the beginning of the visit | 75 method called at the beginning of the visit |
| 78 """ | 76 """ |
| 79 pass | 77 pass |
| (...skipping 20 matching lines...) Expand all Loading... |
| 100 except: | 98 except: |
| 101 return self.__class__.__name__.lower() | 99 return self.__class__.__name__.lower() |
| 102 | 100 |
| 103 def accept(self, visitor, *args, **kwargs): | 101 def accept(self, visitor, *args, **kwargs): |
| 104 func = getattr(visitor, 'visit_%s' % self.get_visit_name()) | 102 func = getattr(visitor, 'visit_%s' % self.get_visit_name()) |
| 105 return func(self, *args, **kwargs) | 103 return func(self, *args, **kwargs) |
| 106 | 104 |
| 107 def leave(self, visitor, *args, **kwargs): | 105 def leave(self, visitor, *args, **kwargs): |
| 108 func = getattr(visitor, 'leave_%s' % self.get_visit_name()) | 106 func = getattr(visitor, 'leave_%s' % self.get_visit_name()) |
| 109 return func(self, *args, **kwargs) | 107 return func(self, *args, **kwargs) |
| OLD | NEW |