diff options
Diffstat (limited to 'logilab/common/visitor.py')
-rw-r--r-- | logilab/common/visitor.py | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/logilab/common/visitor.py b/logilab/common/visitor.py index 0698bae..8d80d54 100644 --- a/logilab/common/visitor.py +++ b/logilab/common/visitor.py @@ -23,15 +23,16 @@ """ from typing import Any, Callable, Optional, Union from logilab.common.types import Node, HTMLWriter, TextWriter + __docformat__ = "restructuredtext en" def no_filter(_: Node) -> int: return 1 + # Iterators ################################################################### class FilteredIterator(object): - def __init__(self, node: Node, list_func: Callable, filter_func: Optional[Any] = None) -> None: self._next = [(node, 0)] if filter_func is None: @@ -41,14 +42,14 @@ class FilteredIterator(object): def __next__(self) -> Optional[Node]: try: return self._list.pop(0) - except : + except: return None next = __next__ + # Base Visitor ################################################################ class Visitor(object): - def __init__(self, iterator_class, filter_func=None): self._iter_class = iterator_class self.filter = filter_func @@ -87,11 +88,13 @@ class Visitor(object): """ return result + # standard visited mixin ###################################################### class VisitedMixIn(object): """ Visited interface allow node visitors to use the node """ + def get_visit_name(self) -> str: """ return the visit name for the mixed class. When calling 'accept', the @@ -101,14 +104,16 @@ class VisitedMixIn(object): try: # mypy: "VisitedMixIn" has no attribute "TYPE" # dynamic attribute - return self.TYPE.replace('-', '_') # type: ignore + return self.TYPE.replace("-", "_") # type: ignore except: return self.__class__.__name__.lower() - def accept(self, visitor: Union[HTMLWriter, TextWriter], *args: Any, **kwargs: Any) -> Optional[Any]: - func = getattr(visitor, 'visit_%s' % self.get_visit_name()) + def accept( + self, visitor: Union[HTMLWriter, TextWriter], *args: Any, **kwargs: Any + ) -> Optional[Any]: + func = getattr(visitor, "visit_%s" % self.get_visit_name()) return func(self, *args, **kwargs) def leave(self, visitor, *args, **kwargs): - func = getattr(visitor, 'leave_%s' % self.get_visit_name()) + func = getattr(visitor, "leave_%s" % self.get_visit_name()) return func(self, *args, **kwargs) |