Skip to content

Commit

Permalink
Изменения из aiogram'а
Browse files Browse the repository at this point in the history
  • Loading branch information
K1rL3s committed Sep 4, 2024
1 parent 4517b25 commit 2428768
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pypi-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
id-token: write
steps:
- name: Download artifacts
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4.1.7
with:
name: dist
path: dist
Expand Down
15 changes: 14 additions & 1 deletion aliceio/client/session/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,26 @@ def _prepare_connector(


class AiohttpSession(BaseSession):
def __init__(self, proxy: Optional[_ProxyType] = None, **kwargs: Any) -> None:
def __init__(
self,
proxy: Optional[_ProxyType] = None,
limit: int = 100,
**kwargs: Any,
) -> None:
"""
Client session based on aiohttp.
:param proxy: The proxy to be used for requests. Default is None.
:param limit: The total number of simultaneous connections. Default is 100.
:param kwargs: Additional keyword arguments.
"""
super().__init__(**kwargs)

self._session: Optional[ClientSession] = None
self._connector_type: Type[TCPConnector] = TCPConnector
self._connector_init: Dict[str, Any] = {
"ssl": ssl.create_default_context(cafile=certifi.where()),
"limit": limit,
"ttl_dns_cache": 3600, # https://github.com/aiogram/aiogram/issues/1500
}
self._should_reset_connector = True # флаг определяет состояние коннектора
self._proxy: Optional[_ProxyType] = None
Expand Down
45 changes: 31 additions & 14 deletions aliceio/fsm/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,13 @@ class StatesGroupMeta(type):
__childs__: "Tuple[Type[StatesGroup], ...]"
__states__: Tuple[State, ...]
__state_names__: Tuple[str, ...]
__all_childs__: Tuple[Type["StatesGroup"], ...]
__all_states__: Tuple[State, ...]
__all_states_names__: Tuple[str, ...]

@no_type_check
def __new__(mcs, name, bases, namespace, **kwargs): # noqa: ANN204, ANN001, ANN003
cls = super(StatesGroupMeta, mcs).__new__( # noqa: UP008
mcs,
name,
bases,
namespace,
)
cls = super().__new__(mcs, name, bases, namespace)

states = []
childs = []
Expand All @@ -89,14 +87,22 @@ def __new__(mcs, name, bases, namespace, **kwargs): # noqa: ANN204, ANN001, ANN
if isinstance(arg, State):
states.append(arg)
elif inspect.isclass(arg) and issubclass(arg, StatesGroup):
childs.append(arg)
arg.__parent__ = cls
child = cls._prepare_child(arg)
childs.append(child)

cls.__parent__ = None
cls.__childs__ = tuple(childs)
cls.__states__ = tuple(states)
cls.__state_names__ = tuple(state.state for state in states)

cls.__all_childs__ = cls._get_all_childs()
cls.__all_states__ = cls._get_all_states()

# In order to ensure performance, we calculate this parameter
# in advance already during the production of the class.
# Depending on the relationship, it should be recalculated
cls.__all_states_names__ = cls._get_all_states_names()

return cls

@property
Expand All @@ -105,22 +111,33 @@ def __full_group_name__(cls) -> str:
return f"{cls.__parent__.__full_group_name__}.{cls.__name__}"
return cls.__name__

@property
def __all_childs__(cls) -> Tuple[Type["StatesGroup"], ...]:
def _prepare_child(cls, child: Type["StatesGroup"]) -> Type["StatesGroup"]:
"""Prepare child.
While adding `cls` for its children, we also need to recalculate
the parameter `__all_states_names__` for each child
`StatesGroup`. Since the child class appears before the
parent, at the time of adding the parent, the child's
`__all_states_names__` is already recorded without taking into
account the name of current parent.
"""
child.__parent__ = cls # type: ignore[assignment]
child.__all_states_names__ = child._get_all_states_names() # noqa: SLF001
return child

def _get_all_childs(cls) -> Tuple[Type["StatesGroup"], ...]:
result = cls.__childs__
for child in cls.__childs__:
result += child.__childs__
return result

@property
def __all_states__(cls) -> Tuple[State, ...]:
def _get_all_states(cls) -> Tuple[State, ...]:
result = cls.__states__
for group in cls.__childs__:
result += group.__all_states__
return result

@property
def __all_states_names__(cls) -> Tuple[str, ...]:
def _get_all_states_names(cls) -> Tuple[str, ...]:
return tuple(state.state for state in cls.__all_states__ if state.state)

def __contains__(cls, item: Any) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ classifiers = [
]
dependencies = [
"aiofiles~=23.2.1",
"aiohttp~=3.9.0",
"aiohttp>=3.9.0,<3.11",
"certifi>=2023.7.22",
"magic-filter>=1.0.12,<1.1",
"pydantic>=2.4.1,<2.9",
Expand Down

0 comments on commit 2428768

Please sign in to comment.