forked from prostomarkeloff/vkwave
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/prostomarkeloff/vkwave
- Loading branch information
Showing
4 changed files
with
38 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]): | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |