From: Dmitry Selyutin Date: Wed, 14 Jun 2023 10:36:50 +0000 (+0300) Subject: all: deprecate core module X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=a0c8bacd988dd814c6dd8ec89bd4714912d476b7;p=mdis.git all: deprecate core module --- diff --git a/README.md b/README.md index 7284b66..b38f8fa 100644 --- a/README.md +++ b/README.md @@ -21,11 +21,11 @@ A walker is just a particular example of dispatcher, where the call yields some The default `mdis.Walker` already incorporates the logic to walk over some builtin Python objects. The example below shows how to override the way the dicts are traversed so that keys and values are swapped. - import mdis.core + import mdis.dispatcher import mdis.walker class CustomWalker(mdis.walker.Walker): - @mdis.core.hook(dict) + @mdis.dispatcher.hook(dict) def dispatch_dict(self, instance): for (key, value) in instance.items(): yield (value, key) @@ -54,25 +54,25 @@ The example below shows how to execute some arbitrary code upon visiting an obje import contextlib - import mdis.core + import mdis.dispatcher import mdis.visitor class CustomVisitor(mdis.visitor.Visitor): - @mdis.core.hook(int) + @mdis.dispatcher.hook(int) @contextlib.contextmanager def dispatch_int(self, instance): print("entering int") yield (instance + 42) print("leaving int") - @mdis.core.hook(str) + @mdis.dispatcher.hook(str) @contextlib.contextmanager def dispatch_str(self, instance): print("entering str") yield f"!!!{instance}!!!" print("leaving str") - @mdis.core.hook(object) + @mdis.dispatcher.hook(object) @contextlib.contextmanager def dispatch_object(self, instance): print("entering object") diff --git a/src/mdis/core.py b/src/mdis/core.py deleted file mode 100644 index 9774cb9..0000000 --- a/src/mdis/core.py +++ /dev/null @@ -1,31 +0,0 @@ -class Hook(object): - def __init__(self, *typeids): - for typeid in typeids: - if not isinstance(typeid, type): - raise ValueError(typeid) - self.__typeids = typeids - return super().__init__() - - def __iter__(self): - yield from self.__typeids - - def __repr__(self): - names = [] - for typeid in self.__typeids: - name = typeid.__qualname__ - module = typeid.__module__ - if module not in ("builtins",): - name = f"{module}.{name}" - names.append(name) - return f"<{', '.join(names)}>" - - def __call__(self, call): - class ConcreteHook(Hook): - def __call__(self, dispatcher, instance): - return call(self=dispatcher, instance=instance) - - return ConcreteHook(*tuple(self)) - - -def hook(*typeids): - return Hook(*typeids) diff --git a/src/mdis/dispatcher.py b/src/mdis/dispatcher.py index f21a292..6f6b9cd 100644 --- a/src/mdis/dispatcher.py +++ b/src/mdis/dispatcher.py @@ -2,7 +2,34 @@ import collections as _collections import inspect as _inspect import types as _types -from . import core as _core + +class Hook(object): + def __init__(self, *typeids): + for typeid in typeids: + if not isinstance(typeid, type): + raise ValueError(typeid) + self.__typeids = typeids + return super().__init__() + + def __iter__(self): + yield from self.__typeids + + def __repr__(self): + names = [] + for typeid in self.__typeids: + name = typeid.__qualname__ + module = typeid.__module__ + if module not in ("builtins",): + name = f"{module}.{name}" + names.append(name) + return f"<{', '.join(names)}>" + + def __call__(self, call): + class ConcreteHook(Hook): + def __call__(self, dispatcher, instance): + return call(self=dispatcher, instance=instance) + + return ConcreteHook(*tuple(self)) class DispatcherMeta(type): @@ -10,7 +37,7 @@ class DispatcherMeta(type): def __new__(metacls, name, bases, ns): hooks = {} - ishook = lambda member: isinstance(member, _core.Hook) + ishook = lambda member: isinstance(member, Hook) for basecls in reversed(bases): members = _inspect.getmembers(basecls, predicate=ishook) @@ -49,6 +76,6 @@ class Dispatcher(metaclass=DispatcherMeta): hook = self.__class__.dispatch() return hook(dispatcher=self, instance=instance) - @_core.hook(object) + @Hook(object) def dispatch_object(self, instance): raise NotImplementedError() diff --git a/src/mdis/visitor.py b/src/mdis/visitor.py index 9b1e02b..f70b5c5 100644 --- a/src/mdis/visitor.py +++ b/src/mdis/visitor.py @@ -1,6 +1,5 @@ import contextlib as _contextlib -from . import core as _core from . import dispatcher as _dispatcher @@ -9,7 +8,7 @@ class VisitorMeta(_dispatcher.DispatcherMeta): class Visitor(_dispatcher.Dispatcher, metaclass=VisitorMeta): + @_dispatcher.Hook(object) @_contextlib.contextmanager - @_core.hook(object) def dispatch_object(self, instance): yield instance diff --git a/src/mdis/walker.py b/src/mdis/walker.py index 507d953..b8fe5ef 100644 --- a/src/mdis/walker.py +++ b/src/mdis/walker.py @@ -1,4 +1,3 @@ -from . import core as _core from . import dispatcher as _dispatcher @@ -7,18 +6,18 @@ class WalkerMeta(_dispatcher.DispatcherMeta): class Walker(_dispatcher.Dispatcher, metaclass=WalkerMeta): - @_core.hook(tuple, list, set, frozenset) + @_dispatcher.Hook(tuple, list, set, frozenset) def dispatch_sequence(self, instance): for item in instance: yield item yield from self(item) - @_core.hook(dict) + @_dispatcher.Hook(dict) def dispatch_mapping(self, instance): for (key, value) in instance.items(): yield (key, value) yield from self((key, value)) - @_core.hook(object) + @_dispatcher.Hook(object) def dispatch_object(self, instance): yield from ()