-
-
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.
- Loading branch information
Showing
15 changed files
with
181 additions
and
136 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
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 +1 @@ | ||
__version__ = "0.2.5" | ||
__version__ = "0.2.6" |
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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import Dict, Iterator, List, Optional | ||
|
||
from .mailer import Mailer | ||
|
||
|
||
class MailerContainer: | ||
_mailers: Dict[int, Mailer] | ||
|
||
def __init__(self, *mailers: Mailer) -> None: | ||
self._mailers = {mailer.id: mailer for mailer in mailers} | ||
|
||
def __repr__(self) -> str: | ||
return f"{type(self).__name__}(total_mailers={len(self._mailers)})" | ||
|
||
def __str__(self) -> str: | ||
mailers = ", ".join(map(repr, self)) | ||
return f"{type(self).__name__}[{mailers}]" | ||
|
||
def __contains__(self, item: int) -> bool: | ||
return item in self._mailers | ||
|
||
def __getitem__(self, item: int) -> Mailer: | ||
return self._mailers[item] | ||
|
||
def __iter__(self) -> Iterator[Mailer]: | ||
return iter(self._mailers.copy().values()) | ||
|
||
def __len__(self) -> int: | ||
return len(self._mailers) | ||
|
||
@property | ||
def mailers(self) -> Dict[int, Mailer]: | ||
return self._mailers.copy() | ||
|
||
def get_mailer(self, mailer_id: int) -> Optional[Mailer]: | ||
return self._mailers.get(mailer_id) | ||
|
||
def get_mailers(self) -> List[Mailer]: | ||
return list(self._mailers.values()) |
45 changes: 16 additions & 29 deletions
45
aiogram_broadcaster/mailer/multiple.py → aiogram_broadcaster/mailer/group.py
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,63 +1,50 @@ | ||
from asyncio import gather, wait | ||
from typing import Any, Coroutine, Dict, Iterable, Iterator, List, Tuple | ||
from typing import Any, Coroutine, Dict, Iterable, List | ||
|
||
from aiogram_broadcaster.logger import logger | ||
|
||
from .container import MailerContainer | ||
from .mailer import Mailer | ||
|
||
|
||
class MultipleMailers: | ||
mailers: Tuple[Mailer, ...] | ||
|
||
def __init__(self, mailers: Iterable[Mailer]) -> None: | ||
self.mailers = tuple(mailers) | ||
|
||
def __iter__(self) -> Iterator[Mailer]: | ||
return iter(self.mailers) | ||
|
||
def __len__(self) -> int: | ||
return len(self.mailers) | ||
|
||
def __repr__(self) -> str: | ||
return f"MultipleMailers(total_mailers={len(self.mailers)})" | ||
|
||
def __str__(self) -> str: | ||
mailers = ", ".join(map(repr, self.mailers)) | ||
return f"MultipleMailers[{mailers}]" | ||
|
||
class MailerGroup(MailerContainer): | ||
def start(self, **kwargs: Any) -> None: | ||
for mailer in self.mailers: | ||
for mailer in self._mailers.values(): | ||
try: | ||
mailer.start(**kwargs) | ||
except RuntimeError: # noqa: PERF203 | ||
logger.exception("A start error occurred") | ||
|
||
async def wait(self) -> None: | ||
futures = [mailer.wait() for mailer in self.mailers] | ||
futures = [mailer.wait() for mailer in self._mailers.values()] | ||
await wait(futures) | ||
|
||
async def run(self, **kwargs: Any) -> Dict[Mailer, Any]: | ||
futures = [mailer.run(**kwargs) for mailer in self.mailers] | ||
futures = [mailer.run(**kwargs) for mailer in self._mailers.values()] | ||
return await self._gather_futures(futures=futures) | ||
|
||
async def stop(self) -> Dict[Mailer, Any]: | ||
futures = [mailer.stop() for mailer in self.mailers] | ||
futures = [mailer.stop() for mailer in self._mailers.values()] | ||
return await self._gather_futures(futures=futures) | ||
|
||
async def destroy(self) -> Dict[Mailer, Any]: | ||
futures = [mailer.destroy() for mailer in self.mailers] | ||
futures = [mailer.destroy() for mailer in self._mailers.values()] | ||
return await self._gather_futures(futures=futures) | ||
|
||
async def add_chats(self, chats: Iterable[int]) -> Dict[Mailer, bool]: | ||
futures = [mailer.add_chats(chats=chats) for mailer in self.mailers] | ||
futures = [mailer.add_chats(chats=chats) for mailer in self._mailers.values()] | ||
return await self._gather_futures(futures=futures) | ||
|
||
async def send_content(self, chat_id: int) -> Dict[Mailer, Any]: | ||
futures = [mailer.send_content(chat_id=chat_id) for mailer in self.mailers] | ||
async def reset_chats(self) -> None: | ||
futures = [mailer.reset_chats() for mailer in self._mailers.values()] | ||
await self._gather_futures(futures=futures) | ||
|
||
async def send(self, chat_id: int) -> Dict[Mailer, Any]: | ||
futures = [mailer.send(chat_id=chat_id) for mailer in self._mailers.values()] | ||
return await self._gather_futures(futures=futures) | ||
|
||
async def _gather_futures(self, futures: List[Coroutine[Any, Any, Any]]) -> Dict[Mailer, Any]: | ||
if not futures: | ||
return {} | ||
results = await gather(*futures, return_exceptions=True) | ||
return dict(zip(self.mailers, results)) | ||
return dict(zip(self._mailers.values(), results)) |
Oops, something went wrong.