telegrinder 0.1.dev20__py3-none-any.whl → 0.1.dev159__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of telegrinder might be problematic. Click here for more details.
- telegrinder/__init__.py +129 -22
- telegrinder/api/__init__.py +11 -2
- telegrinder/api/abc.py +25 -9
- telegrinder/api/api.py +47 -28
- telegrinder/api/error.py +14 -4
- telegrinder/api/response.py +11 -7
- telegrinder/bot/__init__.py +68 -7
- telegrinder/bot/bot.py +30 -24
- telegrinder/bot/cute_types/__init__.py +11 -1
- telegrinder/bot/cute_types/base.py +138 -0
- telegrinder/bot/cute_types/callback_query.py +458 -15
- telegrinder/bot/cute_types/inline_query.py +30 -24
- telegrinder/bot/cute_types/message.py +2982 -78
- telegrinder/bot/cute_types/update.py +30 -0
- telegrinder/bot/cute_types/utils.py +794 -0
- telegrinder/bot/dispatch/__init__.py +56 -3
- telegrinder/bot/dispatch/abc.py +9 -7
- telegrinder/bot/dispatch/composition.py +74 -0
- telegrinder/bot/dispatch/context.py +71 -0
- telegrinder/bot/dispatch/dispatch.py +86 -49
- telegrinder/bot/dispatch/handler/__init__.py +3 -0
- telegrinder/bot/dispatch/handler/abc.py +11 -5
- telegrinder/bot/dispatch/handler/func.py +41 -32
- telegrinder/bot/dispatch/handler/message_reply.py +46 -0
- telegrinder/bot/dispatch/middleware/__init__.py +2 -0
- telegrinder/bot/dispatch/middleware/abc.py +10 -4
- telegrinder/bot/dispatch/process.py +53 -49
- telegrinder/bot/dispatch/return_manager/__init__.py +19 -0
- telegrinder/bot/dispatch/return_manager/abc.py +95 -0
- telegrinder/bot/dispatch/return_manager/callback_query.py +19 -0
- telegrinder/bot/dispatch/return_manager/inline_query.py +14 -0
- telegrinder/bot/dispatch/return_manager/message.py +25 -0
- telegrinder/bot/dispatch/view/__init__.py +14 -2
- telegrinder/bot/dispatch/view/abc.py +128 -2
- telegrinder/bot/dispatch/view/box.py +38 -0
- telegrinder/bot/dispatch/view/callback_query.py +13 -39
- telegrinder/bot/dispatch/view/inline_query.py +11 -39
- telegrinder/bot/dispatch/view/message.py +11 -47
- telegrinder/bot/dispatch/waiter_machine/__init__.py +9 -0
- telegrinder/bot/dispatch/waiter_machine/machine.py +116 -0
- telegrinder/bot/dispatch/waiter_machine/middleware.py +76 -0
- telegrinder/bot/dispatch/waiter_machine/short_state.py +37 -0
- telegrinder/bot/polling/__init__.py +2 -0
- telegrinder/bot/polling/abc.py +11 -4
- telegrinder/bot/polling/polling.py +89 -40
- telegrinder/bot/rules/__init__.py +91 -5
- telegrinder/bot/rules/abc.py +81 -63
- telegrinder/bot/rules/adapter/__init__.py +11 -0
- telegrinder/bot/rules/adapter/abc.py +21 -0
- telegrinder/bot/rules/adapter/errors.py +5 -0
- telegrinder/bot/rules/adapter/event.py +49 -0
- telegrinder/bot/rules/adapter/raw_update.py +24 -0
- telegrinder/bot/rules/callback_data.py +159 -38
- telegrinder/bot/rules/command.py +116 -0
- telegrinder/bot/rules/enum_text.py +28 -0
- telegrinder/bot/rules/func.py +17 -17
- telegrinder/bot/rules/fuzzy.py +13 -10
- telegrinder/bot/rules/inline.py +61 -0
- telegrinder/bot/rules/integer.py +12 -7
- telegrinder/bot/rules/is_from.py +148 -7
- telegrinder/bot/rules/markup.py +21 -18
- telegrinder/bot/rules/mention.py +17 -0
- telegrinder/bot/rules/message_entities.py +33 -0
- telegrinder/bot/rules/regex.py +27 -19
- telegrinder/bot/rules/rule_enum.py +74 -0
- telegrinder/bot/rules/start.py +25 -13
- telegrinder/bot/rules/text.py +23 -14
- telegrinder/bot/scenario/__init__.py +2 -0
- telegrinder/bot/scenario/abc.py +12 -5
- telegrinder/bot/scenario/checkbox.py +48 -30
- telegrinder/bot/scenario/choice.py +16 -10
- telegrinder/client/__init__.py +3 -1
- telegrinder/client/abc.py +26 -16
- telegrinder/client/aiohttp.py +54 -32
- telegrinder/model.py +119 -40
- telegrinder/modules.py +189 -21
- telegrinder/msgspec_json.py +14 -0
- telegrinder/msgspec_utils.py +227 -0
- telegrinder/node/__init__.py +31 -0
- telegrinder/node/attachment.py +71 -0
- telegrinder/node/base.py +93 -0
- telegrinder/node/composer.py +71 -0
- telegrinder/node/container.py +22 -0
- telegrinder/node/message.py +18 -0
- telegrinder/node/rule.py +56 -0
- telegrinder/node/source.py +31 -0
- telegrinder/node/text.py +13 -0
- telegrinder/node/tools/__init__.py +3 -0
- telegrinder/node/tools/generator.py +40 -0
- telegrinder/node/update.py +12 -0
- telegrinder/rules.py +1 -1
- telegrinder/tools/__init__.py +138 -4
- telegrinder/tools/buttons.py +89 -51
- telegrinder/tools/error_handler/__init__.py +8 -0
- telegrinder/tools/error_handler/abc.py +30 -0
- telegrinder/tools/error_handler/error_handler.py +156 -0
- telegrinder/tools/formatting/__init__.py +81 -3
- telegrinder/tools/formatting/html.py +283 -37
- telegrinder/tools/formatting/links.py +32 -0
- telegrinder/tools/formatting/spec_html_formats.py +121 -0
- telegrinder/tools/global_context/__init__.py +12 -0
- telegrinder/tools/global_context/abc.py +66 -0
- telegrinder/tools/global_context/global_context.py +451 -0
- telegrinder/tools/global_context/telegrinder_ctx.py +25 -0
- telegrinder/tools/i18n/__init__.py +12 -0
- telegrinder/tools/i18n/base.py +31 -0
- telegrinder/tools/i18n/middleware/__init__.py +3 -0
- telegrinder/tools/i18n/middleware/base.py +26 -0
- telegrinder/tools/i18n/simple.py +48 -0
- telegrinder/tools/kb_set/__init__.py +2 -0
- telegrinder/tools/kb_set/base.py +3 -0
- telegrinder/tools/kb_set/yaml.py +28 -17
- telegrinder/tools/keyboard.py +84 -62
- telegrinder/tools/loop_wrapper/__init__.py +4 -0
- telegrinder/tools/loop_wrapper/abc.py +18 -0
- telegrinder/tools/loop_wrapper/loop_wrapper.py +132 -0
- telegrinder/tools/magic.py +48 -23
- telegrinder/tools/parse_mode.py +1 -2
- telegrinder/types/__init__.py +1 -0
- telegrinder/types/enums.py +653 -0
- telegrinder/types/methods.py +4107 -1279
- telegrinder/types/objects.py +4771 -1745
- {telegrinder-0.1.dev20.dist-info → telegrinder-0.1.dev159.dist-info}/LICENSE +2 -1
- telegrinder-0.1.dev159.dist-info/METADATA +109 -0
- telegrinder-0.1.dev159.dist-info/RECORD +126 -0
- {telegrinder-0.1.dev20.dist-info → telegrinder-0.1.dev159.dist-info}/WHEEL +1 -1
- telegrinder/bot/dispatch/waiter.py +0 -38
- telegrinder/result.py +0 -38
- telegrinder/tools/formatting/abc.py +0 -52
- telegrinder/tools/formatting/markdown.py +0 -57
- telegrinder-0.1.dev20.dist-info/METADATA +0 -22
- telegrinder-0.1.dev20.dist-info/RECORD +0 -71
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import dataclasses
|
|
2
|
+
import typing
|
|
3
|
+
|
|
4
|
+
from fntypes.result import Error, Ok, Result
|
|
5
|
+
|
|
6
|
+
from telegrinder.api import ABCAPI
|
|
7
|
+
from telegrinder.bot.dispatch.context import Context
|
|
8
|
+
from telegrinder.modules import logger
|
|
9
|
+
from telegrinder.tools.magic import magic_bundle
|
|
10
|
+
|
|
11
|
+
from .abc import ABCErrorHandler, EventT, Handler
|
|
12
|
+
|
|
13
|
+
F = typing.TypeVar("F", bound="FuncCatcher")
|
|
14
|
+
ExceptionT = typing.TypeVar("ExceptionT", bound=BaseException, contravariant=True)
|
|
15
|
+
FuncCatcher = typing.Callable[typing.Concatenate[ExceptionT, ...], typing.Awaitable[typing.Any]]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclasses.dataclass(frozen=True)
|
|
19
|
+
class Catcher(typing.Generic[EventT]):
|
|
20
|
+
func: FuncCatcher
|
|
21
|
+
_: dataclasses.KW_ONLY
|
|
22
|
+
exceptions: list[type[BaseException] | BaseException] = dataclasses.field(
|
|
23
|
+
default_factory=lambda: []
|
|
24
|
+
)
|
|
25
|
+
logging: bool = dataclasses.field(default=False)
|
|
26
|
+
raise_exception: bool = dataclasses.field(default=False)
|
|
27
|
+
ignore_errors: bool = dataclasses.field(default=False)
|
|
28
|
+
|
|
29
|
+
def match_exception(self, exception: BaseException) -> bool:
|
|
30
|
+
for exc in self.exceptions:
|
|
31
|
+
if isinstance(exc, type) and type(exception) == exc:
|
|
32
|
+
return True
|
|
33
|
+
if isinstance(exc, object) and type(exception) == type(exc):
|
|
34
|
+
return True if not exc.args else exc.args == exception.args
|
|
35
|
+
return False
|
|
36
|
+
|
|
37
|
+
async def __call__(
|
|
38
|
+
self,
|
|
39
|
+
handler: Handler[EventT],
|
|
40
|
+
event: EventT,
|
|
41
|
+
api: ABCAPI,
|
|
42
|
+
ctx: Context,
|
|
43
|
+
) -> Result[typing.Any, typing.Any]:
|
|
44
|
+
try:
|
|
45
|
+
result = Ok(await handler(event, **magic_bundle(handler, ctx))) # type: ignore
|
|
46
|
+
except BaseException as exc:
|
|
47
|
+
logger.debug(
|
|
48
|
+
"Exception {!r} occurred while running handler {!r}. Matching "
|
|
49
|
+
"exceptions it with exception that can be caught by the catcher {!r}...",
|
|
50
|
+
exc.__class__.__name__,
|
|
51
|
+
handler.__name__,
|
|
52
|
+
self.func.__name__,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
if self.match_exception(exc):
|
|
56
|
+
logger.debug(
|
|
57
|
+
"Catcher {!r} caught an exception in handler {!r}, "
|
|
58
|
+
"running catcher...".format(
|
|
59
|
+
self.func.__name__,
|
|
60
|
+
handler.__name__,
|
|
61
|
+
)
|
|
62
|
+
)
|
|
63
|
+
result = Ok(
|
|
64
|
+
await self.func(
|
|
65
|
+
exc,
|
|
66
|
+
**magic_bundle(
|
|
67
|
+
self.func,
|
|
68
|
+
{"event": event, "api": api} | ctx # type: ignore
|
|
69
|
+
)
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
else:
|
|
73
|
+
logger.debug("Failed to match exception {!r}!", exc.__class__.__name__)
|
|
74
|
+
result = Error(exc)
|
|
75
|
+
|
|
76
|
+
logger.debug(
|
|
77
|
+
"Catcher {!r} {} with result: {!r}",
|
|
78
|
+
self.func.__name__,
|
|
79
|
+
"completed" if result else "failed",
|
|
80
|
+
result,
|
|
81
|
+
)
|
|
82
|
+
return result
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class ErrorHandler(ABCErrorHandler[EventT]):
|
|
86
|
+
def __init__(self, __catcher: Catcher[EventT] | None = None):
|
|
87
|
+
self.catcher = __catcher
|
|
88
|
+
|
|
89
|
+
def __repr__(self) -> str:
|
|
90
|
+
return f"<ErrorHandler: {self.catcher!r}>"
|
|
91
|
+
|
|
92
|
+
def catch(
|
|
93
|
+
self,
|
|
94
|
+
*exceptions: type[BaseException] | BaseException,
|
|
95
|
+
logging: bool = False,
|
|
96
|
+
raise_exception: bool = False,
|
|
97
|
+
ignore_errors: bool = False,
|
|
98
|
+
):
|
|
99
|
+
"""Catch an exception while the handler is running.
|
|
100
|
+
:param logging: Error logging in stderr.
|
|
101
|
+
:param raise_exception: Raise an exception if the catcher hasn't started.
|
|
102
|
+
:param ignore_errors: Ignore errors that may occur in the catcher.
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
def decorator(func: F) -> F:
|
|
106
|
+
if not self.catcher:
|
|
107
|
+
self.catcher = Catcher(
|
|
108
|
+
func,
|
|
109
|
+
exceptions=list(exceptions),
|
|
110
|
+
logging=logging,
|
|
111
|
+
raise_exception=raise_exception,
|
|
112
|
+
ignore_errors=ignore_errors,
|
|
113
|
+
)
|
|
114
|
+
return func
|
|
115
|
+
return decorator
|
|
116
|
+
|
|
117
|
+
async def run(
|
|
118
|
+
self,
|
|
119
|
+
handler: Handler[EventT],
|
|
120
|
+
event: EventT,
|
|
121
|
+
api: ABCAPI,
|
|
122
|
+
ctx: Context,
|
|
123
|
+
) -> Result[typing.Any, typing.Any]:
|
|
124
|
+
if not self.catcher:
|
|
125
|
+
return Ok(await handler(event, **magic_bundle(handler, ctx))) # type: ignore
|
|
126
|
+
|
|
127
|
+
ok_none = Ok(None)
|
|
128
|
+
|
|
129
|
+
try:
|
|
130
|
+
result = await self.catcher(handler, event, api, ctx)
|
|
131
|
+
except BaseException as e:
|
|
132
|
+
error_msg = "Exception {} was occurred during the running catcher {!r}.".format(
|
|
133
|
+
repr(e.__class__.__name__)
|
|
134
|
+
if not e.args
|
|
135
|
+
else f"{e.__class__.__name__!r}: {str(e)!r}",
|
|
136
|
+
self.catcher.func.__name__,
|
|
137
|
+
)
|
|
138
|
+
result = ok_none
|
|
139
|
+
|
|
140
|
+
if not self.catcher.ignore_errors:
|
|
141
|
+
return Error(error_msg)
|
|
142
|
+
if self.catcher.logging:
|
|
143
|
+
logger.error(error_msg)
|
|
144
|
+
|
|
145
|
+
if self.catcher.raise_exception and not result:
|
|
146
|
+
return result
|
|
147
|
+
|
|
148
|
+
if self.catcher.logging and not result:
|
|
149
|
+
logger.error(
|
|
150
|
+
"Catcher {!r} failed with error: {!r}",
|
|
151
|
+
self.catcher.func.__name__,
|
|
152
|
+
result.error,
|
|
153
|
+
)
|
|
154
|
+
return ok_none
|
|
155
|
+
|
|
156
|
+
return result or ok_none
|
|
@@ -1,3 +1,81 @@
|
|
|
1
|
-
from .
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
from .html import (
|
|
2
|
+
FormatString,
|
|
3
|
+
HTMLFormatter,
|
|
4
|
+
block_quote,
|
|
5
|
+
bold,
|
|
6
|
+
channel_boost_link,
|
|
7
|
+
code_inline,
|
|
8
|
+
escape,
|
|
9
|
+
invite_chat_link,
|
|
10
|
+
italic,
|
|
11
|
+
link,
|
|
12
|
+
mention,
|
|
13
|
+
pre_code,
|
|
14
|
+
resolve_domain,
|
|
15
|
+
spoiler,
|
|
16
|
+
start_bot_link,
|
|
17
|
+
start_group_link,
|
|
18
|
+
strike,
|
|
19
|
+
tg_emoji,
|
|
20
|
+
underline,
|
|
21
|
+
)
|
|
22
|
+
from .links import (
|
|
23
|
+
get_channel_boost_link,
|
|
24
|
+
get_invite_chat_link,
|
|
25
|
+
get_mention_link,
|
|
26
|
+
get_resolve_domain_link,
|
|
27
|
+
get_start_bot_link,
|
|
28
|
+
get_start_group_link,
|
|
29
|
+
)
|
|
30
|
+
from .spec_html_formats import (
|
|
31
|
+
BaseSpecFormat,
|
|
32
|
+
ChannelBoostLink,
|
|
33
|
+
InviteChatLink,
|
|
34
|
+
Link,
|
|
35
|
+
Mention,
|
|
36
|
+
PreCode,
|
|
37
|
+
ResolveDomain,
|
|
38
|
+
SpecialFormat,
|
|
39
|
+
StartBotLink,
|
|
40
|
+
StartGroupLink,
|
|
41
|
+
TgEmoji,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
__all__ = (
|
|
45
|
+
"BaseSpecFormat",
|
|
46
|
+
"ChannelBoostLink",
|
|
47
|
+
"FormatString",
|
|
48
|
+
"HTMLFormatter",
|
|
49
|
+
"InviteChatLink",
|
|
50
|
+
"Link",
|
|
51
|
+
"Mention",
|
|
52
|
+
"PreCode",
|
|
53
|
+
"ResolveDomain",
|
|
54
|
+
"SpecialFormat",
|
|
55
|
+
"StartBotLink",
|
|
56
|
+
"StartGroupLink",
|
|
57
|
+
"TgEmoji",
|
|
58
|
+
"block_quote",
|
|
59
|
+
"bold",
|
|
60
|
+
"channel_boost_link",
|
|
61
|
+
"code_inline",
|
|
62
|
+
"escape",
|
|
63
|
+
"get_channel_boost_link",
|
|
64
|
+
"get_invite_chat_link",
|
|
65
|
+
"get_mention_link",
|
|
66
|
+
"get_resolve_domain_link",
|
|
67
|
+
"get_start_bot_link",
|
|
68
|
+
"get_start_group_link",
|
|
69
|
+
"invite_chat_link",
|
|
70
|
+
"italic",
|
|
71
|
+
"link",
|
|
72
|
+
"mention",
|
|
73
|
+
"pre_code",
|
|
74
|
+
"resolve_domain",
|
|
75
|
+
"spoiler",
|
|
76
|
+
"start_bot_link",
|
|
77
|
+
"start_group_link",
|
|
78
|
+
"strike",
|
|
79
|
+
"tg_emoji",
|
|
80
|
+
"underline",
|
|
81
|
+
)
|
|
@@ -1,58 +1,304 @@
|
|
|
1
|
-
|
|
1
|
+
import html
|
|
2
|
+
import string
|
|
2
3
|
import typing
|
|
4
|
+
from contextlib import suppress
|
|
5
|
+
|
|
3
6
|
from telegrinder.tools.parse_mode import ParseMode
|
|
7
|
+
from telegrinder.types.enums import ProgrammingLanguage
|
|
8
|
+
|
|
9
|
+
from .links import (
|
|
10
|
+
get_channel_boost_link,
|
|
11
|
+
get_invite_chat_link,
|
|
12
|
+
get_mention_link,
|
|
13
|
+
get_resolve_domain_link,
|
|
14
|
+
get_start_bot_link,
|
|
15
|
+
get_start_group_link,
|
|
16
|
+
)
|
|
17
|
+
from .spec_html_formats import SpecialFormat, is_spec_format
|
|
4
18
|
|
|
19
|
+
TAG_FORMAT = "<{tag}{data}>{content}</{tag}>"
|
|
5
20
|
QUOT_MARK = '"'
|
|
6
21
|
|
|
7
22
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
for k, v in data.items():
|
|
14
|
-
s += f" {k}={v}"
|
|
15
|
-
s += ">" + content + "</" + tag_name + ">"
|
|
16
|
-
return HTMLFormatter(s)
|
|
23
|
+
class StringFormatter(string.Formatter):
|
|
24
|
+
"""String formatter, using substitutions from args and kwargs.
|
|
25
|
+
The substitutions are identified by braces ('{' and '}') with
|
|
26
|
+
specifiers: `bold`, `italic`, etc.
|
|
27
|
+
"""
|
|
17
28
|
|
|
29
|
+
__formats__: typing.ClassVar[tuple[str, ...]] = (
|
|
30
|
+
"blockquote",
|
|
31
|
+
"bold",
|
|
32
|
+
"code_inline",
|
|
33
|
+
"italic",
|
|
34
|
+
"spoiler",
|
|
35
|
+
"strike",
|
|
36
|
+
"underline",
|
|
37
|
+
)
|
|
18
38
|
|
|
19
|
-
|
|
20
|
-
|
|
39
|
+
def is_html_format(self, value: typing.Any, fmt: str) -> str:
|
|
40
|
+
if not fmt:
|
|
41
|
+
raise ValueError("Formats union should be: format+format.")
|
|
42
|
+
if fmt not in self.__formats__:
|
|
43
|
+
raise ValueError(
|
|
44
|
+
"Unknown format {!r} for object of type {!r}.".format(
|
|
45
|
+
fmt,
|
|
46
|
+
type(value).__name__,
|
|
47
|
+
)
|
|
48
|
+
)
|
|
49
|
+
return fmt
|
|
21
50
|
|
|
22
|
-
def
|
|
23
|
-
return
|
|
24
|
-
|
|
51
|
+
def get_spec_formatter(self, value: SpecialFormat) -> typing.Callable[..., "TagFormat"]:
|
|
52
|
+
return globals()[value.__formatter_name__]
|
|
53
|
+
|
|
54
|
+
def check_formats(self, value: typing.Any, fmts: list[str]) -> "TagFormat":
|
|
55
|
+
if is_spec_format(value):
|
|
56
|
+
value = value.string
|
|
57
|
+
|
|
58
|
+
current_format = globals()[fmts.pop(0)](
|
|
59
|
+
str(value)
|
|
60
|
+
if isinstance(value, TagFormat)
|
|
61
|
+
else escape(FormatString(value) if not isinstance(value, str) else value)
|
|
62
|
+
)
|
|
63
|
+
for fmt in fmts:
|
|
64
|
+
current_format = globals()[fmt](current_format)
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
TagFormat(
|
|
68
|
+
current_format,
|
|
69
|
+
tag=value.tag,
|
|
70
|
+
**value.data,
|
|
71
|
+
)
|
|
72
|
+
if isinstance(value, TagFormat)
|
|
73
|
+
else current_format
|
|
25
74
|
)
|
|
26
75
|
|
|
27
|
-
def
|
|
28
|
-
|
|
76
|
+
def format_field(self, value: typing.Any, fmt: str) -> "HTMLFormatter":
|
|
77
|
+
with suppress(ValueError):
|
|
78
|
+
return HTMLFormatter(
|
|
79
|
+
format(
|
|
80
|
+
value.formatting()
|
|
81
|
+
if isinstance(value, TagFormat)
|
|
82
|
+
else self.get_spec_formatter(value)(**value.__dict__).formatting()
|
|
83
|
+
if is_spec_format(value)
|
|
84
|
+
else value,
|
|
85
|
+
fmt,
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
return self.format_raw_value(value, fmt)
|
|
29
89
|
|
|
30
|
-
def
|
|
31
|
-
|
|
90
|
+
def format_raw_value(self, value: typing.Any, fmt: str) -> "HTMLFormatter":
|
|
91
|
+
fmts = list(map(lambda fmt: self.is_html_format(value, fmt), fmt.split("+")))
|
|
92
|
+
tag_format = self.check_formats(value, fmts)
|
|
32
93
|
|
|
33
|
-
|
|
34
|
-
|
|
94
|
+
if is_spec_format(value):
|
|
95
|
+
value.string = tag_format
|
|
96
|
+
tag_format = self.get_spec_formatter(value)(**value.__dict__)
|
|
35
97
|
|
|
36
|
-
|
|
37
|
-
return wrap_tag("s", self)
|
|
98
|
+
return tag_format.formatting()
|
|
38
99
|
|
|
39
|
-
def
|
|
40
|
-
return
|
|
100
|
+
def format(self, __string: str, *args: object, **kwargs: object) -> "HTMLFormatter":
|
|
101
|
+
return HTMLFormatter(super().format(__string, *args, **kwargs))
|
|
41
102
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
103
|
+
|
|
104
|
+
class FormatString(str):
|
|
105
|
+
string_formatter = StringFormatter()
|
|
106
|
+
|
|
107
|
+
def __new__(cls, string: str) -> typing.Self:
|
|
108
|
+
if isinstance(string, TagFormat):
|
|
109
|
+
return super().__new__(cls, string.formatting())
|
|
110
|
+
return super().__new__(cls, string)
|
|
111
|
+
|
|
112
|
+
def __add__(self, value: str) -> "HTMLFormatter":
|
|
113
|
+
return HTMLFormatter(
|
|
114
|
+
str.__add__(
|
|
115
|
+
escape(self),
|
|
116
|
+
value.formatting() if isinstance(value, TagFormat) else escape(value),
|
|
117
|
+
)
|
|
47
118
|
)
|
|
48
119
|
|
|
49
|
-
def
|
|
50
|
-
|
|
120
|
+
def __radd__(self, value: str) -> "HTMLFormatter":
|
|
121
|
+
"""Return value+self."""
|
|
122
|
+
return HTMLFormatter(FormatString.__add__(FormatString(value), self).as_str())
|
|
123
|
+
|
|
124
|
+
def as_str(self) -> str:
|
|
125
|
+
"""Return self as a standart string."""
|
|
126
|
+
return self.__str__()
|
|
127
|
+
|
|
128
|
+
def format(self, *args: object, **kwargs: object) -> "HTMLFormatter":
|
|
129
|
+
return self.string_formatter.format(self, *args, **kwargs)
|
|
130
|
+
|
|
51
131
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
132
|
+
class EscapedString(FormatString):
|
|
133
|
+
@property
|
|
134
|
+
def former_string(self) -> str:
|
|
135
|
+
return html.unescape(self)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class TagFormat(FormatString):
|
|
139
|
+
tag: str
|
|
140
|
+
data: dict[str, typing.Any]
|
|
141
|
+
|
|
142
|
+
def __new__(
|
|
143
|
+
cls,
|
|
144
|
+
string: str,
|
|
145
|
+
*,
|
|
146
|
+
tag: str,
|
|
147
|
+
**data: typing.Any,
|
|
148
|
+
) -> typing.Self:
|
|
149
|
+
if isinstance(string, TagFormat):
|
|
150
|
+
string = string.formatting()
|
|
151
|
+
elif not isinstance(
|
|
152
|
+
string,
|
|
153
|
+
(
|
|
154
|
+
EscapedString,
|
|
155
|
+
HTMLFormatter,
|
|
156
|
+
),
|
|
157
|
+
):
|
|
158
|
+
string = escape(string)
|
|
159
|
+
obj = super().__new__(cls, string)
|
|
160
|
+
obj.tag = tag
|
|
161
|
+
obj.data = data
|
|
162
|
+
return obj
|
|
163
|
+
|
|
164
|
+
def get_tag_data(self) -> str:
|
|
165
|
+
return "".join(f" {k}={v}" for k, v in self.data.items())
|
|
166
|
+
|
|
167
|
+
def formatting(self) -> "HTMLFormatter":
|
|
168
|
+
return HTMLFormatter(
|
|
169
|
+
TAG_FORMAT.format(
|
|
170
|
+
tag=self.tag,
|
|
171
|
+
data=self.get_tag_data(),
|
|
172
|
+
content=self,
|
|
173
|
+
)
|
|
55
174
|
)
|
|
56
175
|
|
|
57
|
-
|
|
58
|
-
|
|
176
|
+
|
|
177
|
+
class HTMLFormatter(FormatString):
|
|
178
|
+
"""
|
|
179
|
+
>>> HTMLFormatter(bold("Hello, World"))
|
|
180
|
+
'<b>Hello, World</b>'
|
|
181
|
+
HTMLFormatter("Hi, {name:italic}").format(name="Max")
|
|
182
|
+
'Hi, <i>Max</i>'
|
|
183
|
+
"""
|
|
184
|
+
|
|
185
|
+
PARSE_MODE = ParseMode.HTML
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def escape(string: str) -> EscapedString:
|
|
189
|
+
if isinstance(string, EscapedString | HTMLFormatter):
|
|
190
|
+
return EscapedString(string)
|
|
191
|
+
return EscapedString(html.escape(string, quote=False))
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def block_quote(string: str) -> TagFormat:
|
|
195
|
+
return TagFormat(string, tag="blockquote")
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def bold(string: str) -> TagFormat:
|
|
199
|
+
return TagFormat(string, tag="b")
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def channel_boost_link(channel_username: str, string: str | None = None):
|
|
203
|
+
return link(
|
|
204
|
+
get_channel_boost_link(channel_username),
|
|
205
|
+
string or f"t.me/{channel_username}?boost",
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def code_inline(string: str) -> TagFormat:
|
|
210
|
+
return TagFormat(string, tag="code")
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def italic(string: str) -> TagFormat:
|
|
214
|
+
return TagFormat(string, tag="i")
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def link(href: str, string: str | None = None) -> TagFormat:
|
|
218
|
+
return TagFormat(
|
|
219
|
+
string or href,
|
|
220
|
+
tag="a",
|
|
221
|
+
href=QUOT_MARK + href + QUOT_MARK,
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def pre_code(string: str, lang: str | ProgrammingLanguage | None = None) -> TagFormat:
|
|
226
|
+
if lang is None:
|
|
227
|
+
return TagFormat(string, tag="pre")
|
|
228
|
+
lang = lang.value if isinstance(lang, ProgrammingLanguage) else lang
|
|
229
|
+
return pre_code(TagFormat(string, tag="code", **{"class": f"language-{lang}"}))
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def spoiler(string: str) -> TagFormat:
|
|
233
|
+
return TagFormat(string, tag="tg-spoiler")
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def start_bot_link(bot_username: str, data: str, string: str | None = None) -> TagFormat:
|
|
237
|
+
return link(
|
|
238
|
+
get_start_bot_link(bot_username, data),
|
|
239
|
+
string or f"t.me/{bot_username}?start={data}"
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def start_group_link(bot_username: str, data: str, string: str | None = None) -> TagFormat:
|
|
244
|
+
return link(get_start_group_link(bot_username, data), string)
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def strike(string: str) -> TagFormat:
|
|
248
|
+
return TagFormat(string, tag="s")
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def mention(string: str, user_id: int) -> TagFormat:
|
|
252
|
+
return link(get_mention_link(user_id), string)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def tg_emoji(string: str, emoji_id: int) -> TagFormat:
|
|
256
|
+
return TagFormat(string, tag="tg-emoji", **{"emoji-id": emoji_id})
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def invite_chat_link(invite_link: str, string: str | None = None) -> TagFormat:
|
|
260
|
+
return link(
|
|
261
|
+
get_invite_chat_link(invite_link),
|
|
262
|
+
string or f"https://t.me/joinchat/{invite_link}",
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def resolve_domain(username: str, string: str | None = None) -> TagFormat:
|
|
267
|
+
return link(
|
|
268
|
+
get_resolve_domain_link(username),
|
|
269
|
+
string or f"t.me/{username}",
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def underline(string: str) -> TagFormat:
|
|
274
|
+
return TagFormat(string, tag="u")
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
__all__ = (
|
|
278
|
+
"FormatString",
|
|
279
|
+
"HTMLFormatter",
|
|
280
|
+
"SpecialFormat",
|
|
281
|
+
"block_quote",
|
|
282
|
+
"bold",
|
|
283
|
+
"channel_boost_link",
|
|
284
|
+
"code_inline",
|
|
285
|
+
"escape",
|
|
286
|
+
"get_channel_boost_link",
|
|
287
|
+
"get_invite_chat_link",
|
|
288
|
+
"get_mention_link",
|
|
289
|
+
"get_resolve_domain_link",
|
|
290
|
+
"get_start_bot_link",
|
|
291
|
+
"get_start_group_link",
|
|
292
|
+
"invite_chat_link",
|
|
293
|
+
"italic",
|
|
294
|
+
"link",
|
|
295
|
+
"mention",
|
|
296
|
+
"pre_code",
|
|
297
|
+
"resolve_domain",
|
|
298
|
+
"spoiler",
|
|
299
|
+
"start_bot_link",
|
|
300
|
+
"start_group_link",
|
|
301
|
+
"strike",
|
|
302
|
+
"tg_emoji",
|
|
303
|
+
"underline",
|
|
304
|
+
)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
def get_mention_link(user_id: int) -> str:
|
|
2
|
+
return f"tg://user?id={user_id}"
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def get_resolve_domain_link(username: str) -> str:
|
|
6
|
+
return f"tg://resolve?domain={username}"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def get_start_bot_link(bot_username: str, data: str) -> str:
|
|
10
|
+
return get_resolve_domain_link(bot_username) + f"&start={data}"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get_start_group_link(bot_username: str, data: str) -> str:
|
|
14
|
+
return get_resolve_domain_link(bot_username) + f"&startgroup={data}"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def get_channel_boost_link(channel_username: str) -> str:
|
|
18
|
+
return get_resolve_domain_link(channel_username) + "&boost"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def get_invite_chat_link(invite_link: str) -> str:
|
|
22
|
+
return f"tg://join?invite={invite_link}"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
__all__ = (
|
|
26
|
+
"get_channel_boost_link",
|
|
27
|
+
"get_invite_chat_link",
|
|
28
|
+
"get_mention_link",
|
|
29
|
+
"get_resolve_domain_link",
|
|
30
|
+
"get_start_bot_link",
|
|
31
|
+
"get_start_group_link",
|
|
32
|
+
)
|