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):
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:
chat, user = self.check_address(chat=chat, user=user)
user = self._get_user(chat, user)
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):
chat, user = self.check_address(chat=chat, user=user)
user = self._get_user(chat, user)
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,
bucket: typing.Dict = None, **kwargs):
chat, user = self.check_address(chat=chat, user=user)

View file

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

View file

@ -12,13 +12,14 @@ THROTTLE_MANAGER = '$throttle_manager'
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):
"""
Need override this method and use when application is shutdowns.
You can save data or etc.
You have to override this method and use when application shutdowns.
Perhaps you would like to save data and etc.
:return:
"""
@ -26,7 +27,7 @@ class BaseStorage:
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:
"""
@ -37,34 +38,33 @@ class BaseStorage:
chat: typing.Union[str, int, None] = None,
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.
If one of this is not presented, need set the missing value based on the presented.
In all storage's methods chat or user is always required.
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 user:
:return:
"""
if chat is not None and user is not None:
return chat, user
elif user is None and chat is not None:
if chat is None and user is None:
raise ValueError('`user` or `chat` parameter is required but no one is provided!')
if user is None and chat is not None:
user = chat
return chat, user
elif user is not None and chat is None:
chat = user
return chat, user
raise ValueError('User or chat parameters is required but anyone is not presented!')
return chat, user
async def get_state(self, *,
chat: typing.Union[str, int, None] = None,
user: typing.Union[str, int, None] = None,
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,
need set the missing value based on the presented
Chat or user is always required. If one of them is not provided,
you have to set missing value based on the provided one.
:param chat:
:param user:
@ -78,10 +78,10 @@ class BaseStorage:
user: typing.Union[str, int, None] = None,
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,
need set the missing value based on the presented
Chat or user is always required. If one of them is not provided,
you have to set missing value based on the provided one.
:param chat:
:param user:
@ -95,10 +95,10 @@ class BaseStorage:
user: typing.Union[str, int, None] = 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,
need set the missing value based on the presented
Chat or user is always required. If one of them is not provided,
you have to set missing value based on the provided one.
:param chat:
:param user:
@ -113,8 +113,8 @@ class BaseStorage:
"""
Set data for user in chat
Chat or user is always required. If one of this is not presented,
need set the missing value based on the presented
Chat or user is always required. If one of them is not provided,
you have to set missing value based on the provided one.
:param chat:
:param user:
@ -132,8 +132,8 @@ class BaseStorage:
You can use data parameter or|and kwargs.
Chat or user is always required. If one of this is not presented,
need set the missing value based on the presented
Chat or user is always required. If one of them is not provided,
you have to set missing value based on the provided one.
:param data:
:param chat:
@ -147,10 +147,10 @@ class BaseStorage:
chat: 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,
need set the missing value based on the presented
Chat or user is always required. If one of them is not provided,
you have to set missing value based on the provided one.
:param chat:
:param user:
@ -163,10 +163,11 @@ class BaseStorage:
user: typing.Union[str, int, None] = None,
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,
need set the missing value based on the presented
you have to set missing value based on the provided one.
:param chat:
:param user:
@ -184,8 +185,8 @@ class BaseStorage:
"""
Finish conversation for user in chat.
Chat or user is always required. If one of this is not presented,
need set the missing value based on the presented
Chat or user is always required. If one of them is not provided,
you have to set missing value based on the provided one.
:param chat:
:param user:
@ -201,10 +202,10 @@ class BaseStorage:
user: typing.Union[str, int, None] = None,
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,
need set the missing value based on the presented
Chat or user is always required. If one of them is not provided,
you have to set missing value based on the provided one.
:param chat:
:param user:
@ -218,10 +219,10 @@ class BaseStorage:
user: typing.Union[str, int, None] = 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,
need set the missing value based on the presented
Chat or user is always required. If one of them is not provided,
you have to set missing value based on the provided one.
:param chat:
:param user:
@ -235,12 +236,12 @@ class BaseStorage:
bucket: typing.Dict = None,
**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,
need set the missing value based on the presented
Chat or user is always required. If one of them is not provided,
you have to set missing value based on the provided one.
:param bucket:
:param chat:
@ -254,10 +255,10 @@ class BaseStorage:
chat: 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,
need set the missing value based on the presented
Chat or user is always required. If one of them is not provided,
you have to set missing value based on the provided one.
:param chat:
:param user:

View file

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

View file

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

View file

@ -36,7 +36,8 @@ class User(base.TelegramObject):
@property
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
"""
@ -47,7 +48,7 @@ class User(base.TelegramObject):
@property
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`
:raise: ImportError: when babel is not installed.