Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kesha1225 committed Apr 12, 2020
2 parents 028f003 + a264fe3 commit 8540531
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
14 changes: 12 additions & 2 deletions vkwave/bots/core/dispatching/cast/caster.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
from abc import ABC
from abc import abstractmethod as abm
from typing import Any, Callable, Generic, Type, TypeVar
from typing import Any, Callable, Optional, Generic, Type, TypeVar

IT = TypeVar("IT") # input type
T = TypeVar("T")


class AbstractCaster(ABC, Generic[IT]):
@abm
def from_casters(self, something: Any) -> Optional[IT]:
...

def cast(self, something: Any) -> IT:
result = self.from_casters(something) or self.default(something)
if result is None:
raise NotImplementedError("There is not cast for this object")
return result

@abm
def default(self, something: Any) -> Optional[IT]:
...

@abm
def add_caster(self, typeof: Type[T], handler: Callable[[Any], IT]):
def add_caster(self, handler: Callable[[Any], IT]):
...
22 changes: 10 additions & 12 deletions vkwave/bots/core/dispatching/cast/default.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, Dict, TypeVar, cast
from typing import Optional, Any, Callable, List, TypeVar, cast

from .caster import AbstractCaster

Expand All @@ -8,17 +8,15 @@

class DefaultCaster(AbstractCaster[IT]):
def __init__(self):
self._user_casts: Dict[T, Callable[[Any], IT]] = {}
self._user_casts: List[Callable[[Any], IT]] = []

def add_caster(self, typeof: T, handler: Callable[[Any], IT]):
self._user_casts[typeof] = handler
def add_caster(self, handler: Callable[[Any], IT]):
self._user_casts.append(handler)

def cast(self, something: Any) -> IT: # type: ignore
cb: IT
def from_casters(self, something: Any) -> Optional[IT]:
for handler in self._user_casts:
result = handler(something)
if result is not None:
return result
return None

typeof = type(something)
av_cast = self._user_casts.get(typeof)
if av_cast:
av_cast = cast(Callable[[Any], IT], av_cast)
cb = av_cast(something)
return cb
20 changes: 7 additions & 13 deletions vkwave/bots/core/dispatching/filters/cast.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
from asyncio import iscoroutinefunction
from inspect import isawaitable, isfunction
from typing import Any
from typing import Any, Optional

from vkwave.bots.core.dispatching.cast.default import DefaultCaster

from .base import AsyncFuncFilter, BaseFilter, SyncFuncFilter


class FilterCaster(DefaultCaster[BaseFilter]):
def cast(self, something: Any):

av_cast = super().cast(something)
if av_cast:
return av_cast

filter: BaseFilter
def default(self, something: Any) -> Optional[BaseFilter]:
filter: Optional[BaseFilter]
if iscoroutinefunction(something) or isawaitable(something):
filter = AsyncFuncFilter(something)
return filter
if isfunction(something):
elif isfunction(something):
filter = SyncFuncFilter(something)
return filter

raise NotImplementedError("There is not caster for this type")
else:
filter = None

return filter

caster = FilterCaster()
23 changes: 9 additions & 14 deletions vkwave/bots/core/dispatching/handler/cast.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
from asyncio import iscoroutinefunction
from inspect import isawaitable, isfunction
from typing import Any
from typing import Any, Optional

from vkwave.bots.core.dispatching.cast.default import DefaultCaster

from .callback import AsyncFuncCallback, BaseCallback, SyncFuncCallback


class CallbackCaster(DefaultCaster[BaseCallback]):
def cast(self, something: Any) -> BaseCallback:
av_cast = super().cast(something)
if av_cast:
return av_cast

cb: BaseCallback
def default(self, something: Any) -> Optional[BaseCallback]:
cb: Optional[BaseCallback]
if iscoroutinefunction(something) or isawaitable(something):
cb = AsyncFuncCallback(something)
return cb
if isfunction(something):
elif isfunction(something):
cb = SyncFuncCallback(something)
return cb
if isinstance(something, str):
return SyncFuncCallback(lambda event: something)

raise NotImplementedError("There is no cast for this type")
elif isinstance(something, str):
cb = SyncFuncCallback(lambda event: something)
else:
cb = None

return cb

caster = CallbackCaster()

0 comments on commit 8540531

Please sign in to comment.