Merge pull request #21 from surik00/dev-1.x

Minor fix
This commit is contained in:
Alex Root Junior 2018-03-17 22:22:54 +02:00 committed by GitHub
commit f868ca9494
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 58 deletions

View file

@ -86,19 +86,24 @@ class MemoryStorage(BaseStorage):
def has_bucket(self): def has_bucket(self):
return True return True
async def get_bucket(self, *, chat: typing.Union[str, int, None] = None, user: typing.Union[str, int, None] = None, async def get_bucket(self, *,
chat: typing.Union[str, int, None] = None,
user: typing.Union[str, int, None] = None,
default: typing.Optional[dict] = None) -> typing.Dict: default: typing.Optional[dict] = None) -> typing.Dict:
chat, user = self.check_address(chat=chat, user=user) chat, user = self.check_address(chat=chat, user=user)
user = self._get_user(chat, user) user = self._get_user(chat, user)
return user['bucket'] return user['bucket']
async def set_bucket(self, *, chat: typing.Union[str, int, None] = None, user: typing.Union[str, int, None] = None, async def set_bucket(self, *,
chat: typing.Union[str, int, None] = None,
user: typing.Union[str, int, None] = None,
bucket: typing.Dict = None): bucket: typing.Dict = None):
chat, user = self.check_address(chat=chat, user=user) chat, user = self.check_address(chat=chat, user=user)
user = self._get_user(chat, user) user = self._get_user(chat, user)
user['bucket'] = bucket user['bucket'] = bucket
async def update_bucket(self, *, chat: typing.Union[str, int, None] = None, async def update_bucket(self, *,
chat: typing.Union[str, int, None] = None,
user: typing.Union[str, int, None] = None, user: typing.Union[str, int, None] = None,
bucket: typing.Dict = None, **kwargs): bucket: typing.Dict = None, **kwargs):
chat, user = self.check_address(chat=chat, user=user) chat, user = self.check_address(chat=chat, user=user)

View file

@ -72,7 +72,7 @@ class Handler:
context.set_value('handler', handler) context.set_value('handler', handler)
await self.dispatcher.middleware.trigger(f"process_{self.middleware_key}", args) await self.dispatcher.middleware.trigger(f"process_{self.middleware_key}", args)
response = await handler(*args) response = await handler(*args)
if results is not None: if response is not None:
results.append(response) results.append(response)
if self.once: if self.once:
break break

View file

@ -12,13 +12,14 @@ THROTTLE_MANAGER = '$throttle_manager'
class BaseStorage: class BaseStorage:
""" """
In states-storage you can save current user state and data for all steps You are able to save current user's state
and data for all steps in states-storage
""" """
async def close(self): async def close(self):
""" """
Need override this method and use when application is shutdowns. You have to override this method and use when application shutdowns.
You can save data or etc. Perhaps you would like to save data and etc.
:return: :return:
""" """
@ -26,7 +27,7 @@ class BaseStorage:
async def wait_closed(self): async def wait_closed(self):
""" """
You need override this method for all asynchronously storage's like Redis. You have to override this method for all asynchronous storages (e.g., Redis).
:return: :return:
""" """
@ -37,34 +38,33 @@ class BaseStorage:
chat: typing.Union[str, int, None] = None, chat: typing.Union[str, int, None] = None,
user: typing.Union[str, int, None] = None) -> (typing.Union[str, int], typing.Union[str, int]): user: typing.Union[str, int, None] = None) -> (typing.Union[str, int], typing.Union[str, int]):
""" """
In all methods of storage chat or user is always required. In all storage's methods chat or user is always required.
If one of this is not presented, need set the missing value based on the presented. If one of them is not provided, you have to set missing value based on the provided one.
This method performs the above action. This method performs the check described above.
:param chat: :param chat:
:param user: :param user:
:return: :return:
""" """
if chat is not None and user is not None: if chat is None and user is None:
return chat, user raise ValueError('`user` or `chat` parameter is required but no one is provided!')
elif user is None and chat is not None:
if user is None and chat is not None:
user = chat user = chat
return chat, user
elif user is not None and chat is None: elif user is not None and chat is None:
chat = user chat = user
return chat, user return chat, user
raise ValueError('User or chat parameters is required but anyone is not presented!')
async def get_state(self, *, async def get_state(self, *,
chat: typing.Union[str, int, None] = None, chat: typing.Union[str, int, None] = None,
user: typing.Union[str, int, None] = None, user: typing.Union[str, int, None] = None,
default: typing.Optional[str] = None) -> typing.Optional[str]: default: typing.Optional[str] = None) -> typing.Optional[str]:
""" """
Get current state of user in chat. Return value stored in `default` parameter if record is not found. Get current state of user in chat. Return `default` if no record is found.
Chat or user is always required. If one of this is not presented, Chat or user is always required. If one of them is not provided,
need set the missing value based on the presented you have to set missing value based on the provided one.
:param chat: :param chat:
:param user: :param user:
@ -78,10 +78,10 @@ class BaseStorage:
user: typing.Union[str, int, None] = None, user: typing.Union[str, int, None] = None,
default: typing.Optional[typing.Dict] = None) -> typing.Dict: default: typing.Optional[typing.Dict] = None) -> typing.Dict:
""" """
Get state-data for user in chat. Return `default` if data is not presented in storage. Get state-data for user in chat. Return `default` if no data is provided in storage.
Chat or user is always required. If one of this is not presented, Chat or user is always required. If one of them is not provided,
need set the missing value based on the presented you have to set missing value based on the provided one.
:param chat: :param chat:
:param user: :param user:
@ -95,10 +95,10 @@ class BaseStorage:
user: typing.Union[str, int, None] = None, user: typing.Union[str, int, None] = None,
state: typing.Optional[typing.AnyStr] = None): state: typing.Optional[typing.AnyStr] = None):
""" """
Setup new state for user in chat Set new state for user in chat
Chat or user is always required. If one of this is not presented, Chat or user is always required. If one of them is not provided,
need set the missing value based on the presented you have to set missing value based on the provided one.
:param chat: :param chat:
:param user: :param user:
@ -113,8 +113,8 @@ class BaseStorage:
""" """
Set data for user in chat Set data for user in chat
Chat or user is always required. If one of this is not presented, Chat or user is always required. If one of them is not provided,
need set the missing value based on the presented you have to set missing value based on the provided one.
:param chat: :param chat:
:param user: :param user:
@ -132,8 +132,8 @@ class BaseStorage:
You can use data parameter or|and kwargs. You can use data parameter or|and kwargs.
Chat or user is always required. If one of this is not presented, Chat or user is always required. If one of them is not provided,
need set the missing value based on the presented you have to set missing value based on the provided one.
:param data: :param data:
:param chat: :param chat:
@ -147,10 +147,10 @@ class BaseStorage:
chat: typing.Union[str, int, None] = None, chat: typing.Union[str, int, None] = None,
user: typing.Union[str, int, None] = None): user: typing.Union[str, int, None] = None):
""" """
Reset data dor user in chat. Reset data for user in chat.
Chat or user is always required. If one of this is not presented, Chat or user is always required. If one of them is not provided,
need set the missing value based on the presented you have to set missing value based on the provided one.
:param chat: :param chat:
:param user: :param user:
@ -163,10 +163,11 @@ class BaseStorage:
user: typing.Union[str, int, None] = None, user: typing.Union[str, int, None] = None,
with_data: typing.Optional[bool] = True): with_data: typing.Optional[bool] = True):
""" """
Reset state for user in chat. You can use this method for finish conversations. Reset state for user in chat.
You may desire to use this method when finishing conversations.
Chat or user is always required. If one of this is not presented, Chat or user is always required. If one of this is not presented,
need set the missing value based on the presented you have to set missing value based on the provided one.
:param chat: :param chat:
:param user: :param user:
@ -184,8 +185,8 @@ class BaseStorage:
""" """
Finish conversation for user in chat. Finish conversation for user in chat.
Chat or user is always required. If one of this is not presented, Chat or user is always required. If one of them is not provided,
need set the missing value based on the presented you have to set missing value based on the provided one.
:param chat: :param chat:
:param user: :param user:
@ -201,10 +202,10 @@ class BaseStorage:
user: typing.Union[str, int, None] = None, user: typing.Union[str, int, None] = None,
default: typing.Optional[dict] = None) -> typing.Dict: default: typing.Optional[dict] = None) -> typing.Dict:
""" """
Get state-data for user in chat. Return `default` if data is not presented in storage. Get bucket for user in chat. Return `default` if no data is provided in storage.
Chat or user is always required. If one of this is not presented, Chat or user is always required. If one of them is not provided,
need set the missing value based on the presented you have to set missing value based on the provided one.
:param chat: :param chat:
:param user: :param user:
@ -218,10 +219,10 @@ class BaseStorage:
user: typing.Union[str, int, None] = None, user: typing.Union[str, int, None] = None,
bucket: typing.Dict = None): bucket: typing.Dict = None):
""" """
Set data for user in chat Set bucket for user in chat
Chat or user is always required. If one of this is not presented, Chat or user is always required. If one of them is not provided,
need set the missing value based on the presented you have to set missing value based on the provided one.
:param chat: :param chat:
:param user: :param user:
@ -235,12 +236,12 @@ class BaseStorage:
bucket: typing.Dict = None, bucket: typing.Dict = None,
**kwargs): **kwargs):
""" """
Update data for user in chat Update bucket for user in chat
You can use data parameter or|and kwargs. You can use bucket parameter or|and kwargs.
Chat or user is always required. If one of this is not presented, Chat or user is always required. If one of them is not provided,
need set the missing value based on the presented you have to set missing value based on the provided one.
:param bucket: :param bucket:
:param chat: :param chat:
@ -254,10 +255,10 @@ class BaseStorage:
chat: typing.Union[str, int, None] = None, chat: typing.Union[str, int, None] = None,
user: typing.Union[str, int, None] = None): user: typing.Union[str, int, None] = None):
""" """
Reset data dor user in chat. Reset bucket dor user in chat.
Chat or user is always required. If one of this is not presented, Chat or user is always required. If one of them is not provided,
need set the missing value based on the presented you have to set missing value based on the provided one.
:param chat: :param chat:
:param user: :param user:

View file

@ -34,7 +34,8 @@ class InlineKeyboardMarkup(base.TelegramObject):
Add buttons Add buttons
:param args: :param args:
:return: :return: self
:rtype: :obj:`types.InlineKeyboardMarkup`
""" """
row = [] row = []
for index, button in enumerate(args, start=1): for index, button in enumerate(args, start=1):
@ -44,13 +45,15 @@ class InlineKeyboardMarkup(base.TelegramObject):
row = [] row = []
if len(row) > 0: if len(row) > 0:
self.inline_keyboard.append(row) self.inline_keyboard.append(row)
return self
def row(self, *args): def row(self, *args):
""" """
Add row Add row
:param args: :param args:
:return: :return: self
:rtype: :obj:`types.InlineKeyboardMarkup`
""" """
btn_array = [] btn_array = []
for button in args: for button in args:
@ -63,11 +66,14 @@ class InlineKeyboardMarkup(base.TelegramObject):
Insert button to last row Insert button to last row
:param button: :param button:
:return: self
:rtype: :obj:`types.InlineKeyboardMarkup`
""" """
if self.inline_keyboard and len(self.inline_keyboard[-1] < self.row_width): if self.inline_keyboard and len(self.inline_keyboard[-1] < self.row_width):
self.inline_keyboard[-1].append(button) self.inline_keyboard[-1].append(button)
else: else:
self.add(button) self.add(button)
return self
class InlineKeyboardButton(base.TelegramObject): class InlineKeyboardButton(base.TelegramObject):

View file

@ -37,7 +37,8 @@ class ReplyKeyboardMarkup(base.TelegramObject):
Add buttons Add buttons
:param args: :param args:
:return: :return: self
:rtype: :obj:`types.ReplyKeyboardMarkup`
""" """
row = [] row = []
for index, button in enumerate(args): for index, button in enumerate(args):
@ -47,13 +48,15 @@ class ReplyKeyboardMarkup(base.TelegramObject):
row = [] row = []
if len(row) > 0: if len(row) > 0:
self.keyboard.append(row) self.keyboard.append(row)
return self
def row(self, *args): def row(self, *args):
""" """
Add row Add row
:param args: :param args:
:return: :return: self
:rtype: :obj:`types.ReplyKeyboardMarkup`
""" """
btn_array = [] btn_array = []
for button in args: for button in args:
@ -66,11 +69,14 @@ class ReplyKeyboardMarkup(base.TelegramObject):
Insert button to last row Insert button to last row
:param button: :param button:
:return: self
:rtype: :obj:`types.ReplyKeyboardMarkup`
""" """
if self.keyboard and len(self.keyboard[-1]) < self.row_width: if self.keyboard and len(self.keyboard[-1]) < self.row_width:
self.keyboard[-1].append(button) self.keyboard[-1].append(button)
else: else:
self.add(button) self.add(button)
return self
class KeyboardButton(base.TelegramObject): class KeyboardButton(base.TelegramObject):
@ -84,8 +90,11 @@ class KeyboardButton(base.TelegramObject):
request_contact: base.Boolean = fields.Field() request_contact: base.Boolean = fields.Field()
request_location: base.Boolean = fields.Field() request_location: base.Boolean = fields.Field()
def __init__(self, text: base.String, request_contact: base.Boolean = None, request_location: base.Boolean = None): def __init__(self, text: base.String,
super(KeyboardButton, self).__init__(text=text, request_contact=request_contact, request_contact: base.Boolean = None,
request_location: base.Boolean = None):
super(KeyboardButton, self).__init__(text=text,
request_contact=request_contact,
request_location=request_location) request_location=request_location)

View file

@ -36,7 +36,8 @@ class User(base.TelegramObject):
@property @property
def mention(self): def mention(self):
""" """
You can get mention to user (If user have username, otherwise return full name) You can get user's username to mention him
Full name will be returned if user has no username
:return: str :return: str
""" """
@ -47,7 +48,7 @@ class User(base.TelegramObject):
@property @property
def locale(self) -> 'babel.core.Locale' or None: def locale(self) -> 'babel.core.Locale' or None:
""" """
This property require `Babel <https://pypi.python.org/pypi/Babel>`_ module This property requires `Babel <https://pypi.python.org/pypi/Babel>`_ module
:return: :class:`babel.core.Locale` :return: :class:`babel.core.Locale`
:raise: ImportError: when babel is not installed. :raise: ImportError: when babel is not installed.