Skip to content

Commit 74e9780

Browse files
committed
BaseMiddleware returned to it's original place && I18N middleware is now only in examples
1 parent 9b20f41 commit 74e9780

5 files changed

Lines changed: 50 additions & 27 deletions

File tree

‎examples/asynchronous_telebot/middleware/flooding_middleware.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Just a little example of middleware handlers
22

3-
from telebot.asyncio_middlewares import BaseMiddleware
3+
from telebot.asyncio_handler_backends import BaseMiddleware
44
from telebot.async_telebot import AsyncTeleBot
55
from telebot.async_telebot import CancelUpdate
66
bot = AsyncTeleBot('TOKEN')

‎examples/asynchronous_telebot/middleware/i18n.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
# But this example just to show the work of middlewares.
88

99
import telebot
10-
import telebot.asyncio_middlewares
1110
from telebot.async_telebot import AsyncTeleBot
1211
from telebot import asyncio_handler_backends
1312
import logging
@@ -28,7 +27,7 @@
2827
bot = AsyncTeleBot('TOKEN')
2928

3029

31-
class LanguageMiddleware(telebot.asyncio_middlewares.BaseMiddleware):
30+
class LanguageMiddleware(asyncio_handler_backends.BaseMiddleware):
3231
def __init__(self):
3332
self.update_types = ['message'] # Update types that will be handled by this middleware.
3433
async def pre_process(self, message, data):

telebot/asyncio_middlewares.py renamed to examples/asynchronous_telebot/middleware/i18n_middleware_example/i18n_base_midddleware.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import contextvars
2+
import gettext
3+
import os
4+
5+
from telebot.asyncio_handler_backends import BaseMiddleware
26

37
try:
48
from babel.support import LazyProxy
@@ -7,25 +11,6 @@
711
except ImportError:
812
babel_imported = False
913

10-
from telebot import util
11-
12-
13-
class BaseMiddleware:
14-
"""
15-
Base class for middleware.
16-
17-
Your middlewares should be inherited from this class.
18-
"""
19-
20-
def __init__(self):
21-
pass
22-
23-
async def pre_process(self, message, data):
24-
raise NotImplementedError
25-
26-
async def post_process(self, message, data, exception):
27-
raise NotImplementedError
28-
2914

3015
class I18N(BaseMiddleware):
3116
"""
@@ -41,7 +26,7 @@ def __init__(self, translations_path, domain_name: str):
4126

4227
self.path = translations_path
4328
self.domain = domain_name
44-
self.translations = util.find_translations(self.path, self.domain)
29+
self.translations = self.find_translations()
4530

4631
@property
4732
def available_translations(self):
@@ -107,3 +92,29 @@ async def pre_process(self, message, data):
10792

10893
async def post_process(self, message, data, exception):
10994
pass
95+
96+
def find_translations(self):
97+
"""
98+
Looks for translations with passed 'domain' in passed 'path'
99+
"""
100+
if not os.path.exists(self.path):
101+
raise RuntimeError(f"Translations directory by path: {self.path!r} was not found")
102+
103+
result = {}
104+
105+
for name in os.listdir(self.path):
106+
translations_path = os.path.join(self.path, name, 'LC_MESSAGES')
107+
108+
if not os.path.isdir(translations_path):
109+
continue
110+
111+
po_file = os.path.join(translations_path, self.domain + '.po')
112+
mo_file = po_file[:-2] + 'mo'
113+
114+
if os.path.isfile(po_file) and not os.path.isfile(mo_file):
115+
raise FileNotFoundError(f"Translations for: {name!r} were not compiled!")
116+
117+
with open(mo_file, 'rb') as file:
118+
result[name] = gettext.GNUTranslations(file)
119+
120+
return result

‎examples/asynchronous_telebot/middleware/i18n_middleware_example/main.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
from telebot import types
5757
from telebot.async_telebot import AsyncTeleBot
5858
from telebot.asyncio_filters import TextMatchFilter, TextFilter
59-
from telebot.asyncio_middlewares import I18N
59+
from i18n_base_midddleware import I18N
6060
from telebot.asyncio_storage.memory_storage import StateMemoryStorage
6161

6262

@@ -87,7 +87,7 @@ async def get_user_language(self, obj: Union[types.Message, types.CallbackQuery]
8787

8888

8989
storage = StateMemoryStorage()
90-
bot = AsyncTeleBot("", state_storage=storage)
90+
bot = AsyncTeleBot("1254795383:AAE7gbj1aas4lEDHB1eVuZZhSGPWcH1B5ds", state_storage=storage)
9191

9292
i18n = I18NMiddleware(translations_path='locales', domain_name='messages')
9393
_ = i18n.gettext # for singular translations

‎telebot/asyncio_handler_backends.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
from telebot.asyncio_middlewares import BaseMiddleware
1+
class BaseMiddleware:
2+
"""
3+
Base class for middleware.
4+
Your middlewares should be inherited from this class.
5+
"""
6+
7+
def __init__(self):
8+
pass
9+
10+
async def pre_process(self, message, data):
11+
raise NotImplementedError
12+
13+
async def post_process(self, message, data, exception):
14+
raise NotImplementedError
215

316

417
class State:
@@ -15,4 +28,4 @@ def __init_subclass__(cls) -> None:
1528
for name, value in cls.__dict__.items():
1629
if not name.startswith('__') and not callable(value) and isinstance(value, State):
1730
# change value of that variable
18-
value.name = ':'.join((cls.__name__, name))
31+
value.name = ':'.join((cls.__name__, name))

0 commit comments

Comments
 (0)