Skip to content

Commit

Permalink
feat(PAT-9196): manual POMS termination
Browse files Browse the repository at this point in the history
  • Loading branch information
A.Shpak committed Mar 13, 2024
1 parent c3305f0 commit ccbf6e6
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 22 deletions.
8 changes: 4 additions & 4 deletions chatushka/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from chatushka._chatushka import ChatushkaBot
from chatushka._chatushka import Chatushka
from chatushka._errors import ChatushkaError, ChatushkaResponseError
from chatushka._matchers import BaseMatcher, CommandMatcher, RegExMatcher, EventMatcher
from chatushka._models import Chat, Message, Update, User, Events, ChatPermissions
from chatushka._matchers import BaseMatcher, CommandMatcher, EventMatcher, RegExMatcher
from chatushka._models import Chat, ChatPermissions, Events, Message, Update, User
from chatushka._transport import TelegramBotAPI

__all__ = [
"ChatushkaBot",
"Chatushka",
# errors
"ChatushkaError",
"ChatushkaResponseError",
Expand Down
25 changes: 16 additions & 9 deletions chatushka/_chatushka.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
from asyncio import gather
from collections.abc import Callable, Sequence
from contextlib import asynccontextmanager
from typing import final, MutableMapping
from collections.abc import AsyncGenerator, Callable, MutableMapping, Sequence
from contextlib import (
AbstractAsyncContextManager,
_AsyncGeneratorContextManager,
asynccontextmanager,
)
from typing import Any, final

from chatushka._constants import (
HTTP_POOLING_TIMEOUT,
)
from chatushka._matchers import CommandMatcher, Matcher, RegExMatcher, EventMatcher
from chatushka._matchers import CommandMatcher, EventMatcher, Matcher, RegExMatcher
from chatushka._models import Events
from chatushka._transport import TelegramBotAPI


@asynccontextmanager
async def _default_lifespan(
_: "ChatushkaBot",
) -> None:
_: "Chatushka",
) -> AsyncGenerator[None, None]:
yield


@final
class ChatushkaBot:
class Chatushka:
def __init__(
self,
*,
token: str,
cmd_prefixes: str | Sequence[str] = (),
lifespan=None,
lifespan: AbstractAsyncContextManager | None = None,
) -> None:
self._state: MutableMapping = {}
self._lifespan = lifespan or _default_lifespan
self._lifespan: (
AbstractAsyncContextManager[Any]
| Callable[[Chatushka], _AsyncGeneratorContextManager[None]]
) = (lifespan or _default_lifespan)
self._token = token
if isinstance(cmd_prefixes, str):
cmd_prefixes = [cmd_prefixes]
Expand Down
2 changes: 1 addition & 1 deletion chatushka/_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from re import Pattern, compile
from typing import TypeVar

from chatushka._models import Update, Events
from chatushka._models import Events, Update
from chatushka._transport import TelegramBotAPI

Matcher = TypeVar("Matcher", bound="BaseMatcher")
Expand Down
4 changes: 2 additions & 2 deletions chatushka/_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime
from enum import Enum, auto
from typing import Optional, Literal
from enum import Enum
from typing import Literal, Optional

from pydantic import AliasChoices, BaseModel, ConfigDict, Field

Expand Down
7 changes: 3 additions & 4 deletions chatushka/_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
ChatMemberAdministrator,
ChatMemberOwner,
ChatMemberStatuses,
ChatPermissions,
Message,
Update,
ChatPermissions,
)


Expand Down Expand Up @@ -58,7 +58,7 @@ async def _api_request(
) -> list[dict[str, Any]] | dict[str, Any]:
response = await self._client.post(
url=api_method,
data=kwargs,
json=kwargs,
)
return response.json()["result"]

Expand Down Expand Up @@ -141,11 +141,10 @@ async def restrict_chat_member(
permissions: ChatPermissions,
until_date: datetime,
) -> bool:
result = await self._api_request(
return await self._api_request(
"restrictChatMember",
chat_id=chat_id,
user_id=user_id,
permissions=permissions.json(),
until_date=int(until_date.timestamp()),
)
return result
2 changes: 1 addition & 1 deletion example/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import chatushka
from example.constants import MAGIC_EIGHT_BALL_CHOICES

bot = chatushka.ChatushkaBot(
bot = chatushka.Chatushka(
token=os.environ["CHATUSHKA_TOKEN"],
cmd_prefixes=("!", "/"),
)
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
SOURCE_DIR := "chatushka"
TESTS_DIR := "tests"

lint: mypy ruff
lint: ruff mypy

mypy:
poetry run python -m mypy --pretty --package {{ SOURCE_DIR }}
Expand Down

0 comments on commit ccbf6e6

Please sign in to comment.