1
Fork 0
mirror of https://github.com/Steffo99/async-chain.git synced 2024-11-22 04:54:21 +00:00
async-chain/async_chain/chain.py

128 lines
3.3 KiB
Python
Raw Normal View History

2021-06-14 02:32:00 +00:00
from __future__ import annotations
2021-06-13 18:42:02 +00:00
import inspect
import typing as t
import abc
class Chain(metaclass=abc.ABCMeta):
2021-06-14 02:32:00 +00:00
__slots__ = []
2021-06-13 18:42:02 +00:00
2021-06-14 02:32:00 +00:00
def __getattr__(self, item) -> ChainGetAttr:
2021-06-13 18:42:02 +00:00
return ChainGetAttr(previous=self, item=item)
2021-06-14 02:32:00 +00:00
def __getitem__(self, item) -> ChainGetItem:
2021-06-13 18:42:02 +00:00
return ChainGetItem(previous=self, item=item)
2021-06-14 02:32:00 +00:00
def __call__(self, *args, **kwargs) -> ChainCall:
2021-06-13 18:42:02 +00:00
return ChainCall(previous=self, args=args, kwargs=kwargs)
def __await__(self):
return self.__evaluate__().__await__()
@abc.abstractmethod
2021-06-14 02:32:00 +00:00
def __repr__(self) -> str:
2021-06-13 18:42:02 +00:00
raise NotImplementedError()
@abc.abstractmethod
2021-06-14 02:32:00 +00:00
def __display__(self) -> str:
2021-06-13 18:42:02 +00:00
raise NotImplementedError()
@abc.abstractmethod
2021-06-14 02:32:00 +00:00
async def __evaluate__(self) -> t.Any:
2021-06-13 18:42:02 +00:00
raise NotImplementedError()
class ChainStart(Chain):
2021-06-14 02:32:00 +00:00
__slots__ = ("__start__",)
2021-06-13 18:42:02 +00:00
def __init__(self, start: t.Any):
super().__init__()
self.__start__: t.Any = start
def __repr__(self):
return f"<Chain {self.__display__()}>"
def __display__(self):
return f"{self.__start__!r}"
async def __evaluate__(self):
return self.__start__
class ChainNode(Chain, metaclass=abc.ABCMeta):
2021-06-14 02:32:00 +00:00
__slots__ = ("__previous__",)
2021-06-13 18:42:02 +00:00
def __init__(self, previous: Chain):
super().__init__()
self.__previous__: Chain = previous
def __repr__(self):
return f"<Chain {self.__display__()}>"
class ChainGetAttr(ChainNode):
2021-06-14 02:32:00 +00:00
__slots__ = ("__item__",)
2021-06-13 18:42:02 +00:00
def __init__(self, previous: Chain, item: str):
super().__init__(previous=previous)
2021-06-14 02:32:00 +00:00
self.__item__: str = item
2021-06-13 18:42:02 +00:00
def __display__(self):
2021-06-14 02:32:00 +00:00
return f"{self.__previous__.__display__()}.{self.__item__!s}"
2021-06-13 18:42:02 +00:00
async def __evaluate__(self):
previous = await self.__previous__.__evaluate__()
2021-06-14 02:32:00 +00:00
current = getattr(previous, self.__item__)
2021-06-13 18:42:02 +00:00
if inspect.isawaitable(current):
return await current
else:
return current
class ChainGetItem(ChainNode):
2021-06-14 02:32:00 +00:00
__slots__ = ("__item__",)
2021-06-13 18:42:02 +00:00
def __init__(self, previous: Chain, item: t.Any):
super().__init__(previous=previous)
2021-06-14 02:32:00 +00:00
self.__item__: t.Any = item
2021-06-13 18:42:02 +00:00
def __display__(self):
2021-06-14 02:32:00 +00:00
return f"{self.__previous__.__display__()}[{self.__item__!r}]"
2021-06-13 18:42:02 +00:00
async def __evaluate__(self):
previous = await self.__previous__.__evaluate__()
2021-06-14 02:32:00 +00:00
current = previous[self.__item__]
2021-06-13 18:42:02 +00:00
if inspect.isawaitable(current):
return await current
else:
return current
class ChainCall(ChainNode):
2021-06-14 02:32:00 +00:00
__slots__ = ("__args__", "__kwargs__",)
2021-06-13 18:42:02 +00:00
def __init__(self, previous: Chain, args, kwargs):
super().__init__(previous=previous)
2021-06-14 02:32:00 +00:00
self.__args__ = args
self.__kwargs__ = kwargs
2021-06-13 18:42:02 +00:00
def __display__(self):
2021-06-14 02:32:00 +00:00
args = map(lambda a: f"{a!r}", self.__args__)
kwargs = map(lambda k, v: f"{k!s}={v!r}", self.__kwargs__)
2021-06-13 18:42:02 +00:00
allargs = ", ".join([*args, *kwargs])
return f"{self.__previous__.__display__()}({allargs})"
async def __evaluate__(self):
previous = await self.__previous__.__evaluate__()
2021-06-14 02:32:00 +00:00
current = previous(*self.__args__, **self.__kwargs__)
2021-06-13 18:42:02 +00:00
if inspect.isawaitable(current):
return await current
else:
return current