From 11fc718c40ed7189c03393c9a6852e0c70ee1b34 Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Thu, 15 Mar 2018 20:40:16 +0300 Subject: [PATCH 1/7] Minor typos fix --- aiogram/types/user.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/aiogram/types/user.py b/aiogram/types/user.py index c8864f8d..aaad3240 100644 --- a/aiogram/types/user.py +++ b/aiogram/types/user.py @@ -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 `_ module + This property requires `Babel `_ module :return: :class:`babel.core.Locale` :raise: ImportError: when babel is not installed. @@ -79,9 +80,9 @@ class User(base.TelegramObject): def __hash__(self): return self.id + \ - hash(self.is_bot) + \ - hash(self.full_name) + \ - (hash(self.username) if self.username else 0) + hash(self.is_bot) + \ + hash(self.full_name) + \ + (hash(self.username) if self.username else 0) def __int__(self): return self.id From b45df4ea2eee49e4ab7f13f345fa5bb532b49bc4 Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Thu, 15 Mar 2018 20:48:17 +0300 Subject: [PATCH 2/7] Fix response None check in handlers --- aiogram/dispatcher/handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiogram/dispatcher/handler.py b/aiogram/dispatcher/handler.py index 8972958c..139c6011 100644 --- a/aiogram/dispatcher/handler.py +++ b/aiogram/dispatcher/handler.py @@ -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 From 7d804759420843b90fbcc62f61c6e862c3e14004 Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Fri, 16 Mar 2018 22:40:14 +0300 Subject: [PATCH 3/7] Improvements in InlineKeyboardMarkup --- aiogram/types/inline_keyboard.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/aiogram/types/inline_keyboard.py b/aiogram/types/inline_keyboard.py index 7c859e6e..003a3556 100644 --- a/aiogram/types/inline_keyboard.py +++ b/aiogram/types/inline_keyboard.py @@ -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: From 82b35acf52edf7fd7e6bf5b24a4c517d976790bd Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Sat, 17 Mar 2018 11:12:53 +0300 Subject: [PATCH 4/7] Add return self to the InlineKeyboardMarkup's insert method --- aiogram/types/inline_keyboard.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aiogram/types/inline_keyboard.py b/aiogram/types/inline_keyboard.py index 003a3556..1b5c8011 100644 --- a/aiogram/types/inline_keyboard.py +++ b/aiogram/types/inline_keyboard.py @@ -66,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): From 9deb95a9ea8b955959ebda70b815794dd7382503 Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Sat, 17 Mar 2018 11:13:43 +0300 Subject: [PATCH 5/7] Minor typography changes --- aiogram/contrib/fsm_storage/memory.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/aiogram/contrib/fsm_storage/memory.py b/aiogram/contrib/fsm_storage/memory.py index ac463d14..f8670ec4 100644 --- a/aiogram/contrib/fsm_storage/memory.py +++ b/aiogram/contrib/fsm_storage/memory.py @@ -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) From 6d14790e1c68e9b49cee0252ea74c6c6e9245407 Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Sat, 17 Mar 2018 11:22:03 +0300 Subject: [PATCH 6/7] Typos fixes --- aiogram/dispatcher/storage.py | 93 ++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/aiogram/dispatcher/storage.py b/aiogram/dispatcher/storage.py index e50b8de7..b7e733d7 100644 --- a/aiogram/dispatcher/storage.py +++ b/aiogram/dispatcher/storage.py @@ -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: From def6fff4a9de5d028cf17135032c636d6fe5e69f Mon Sep 17 00:00:00 2001 From: Suren Khorenyan Date: Sat, 17 Mar 2018 11:29:43 +0300 Subject: [PATCH 7/7] Upgrade ReplyKeyboardMarkup methods to return self --- aiogram/types/reply_keyboard.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/aiogram/types/reply_keyboard.py b/aiogram/types/reply_keyboard.py index 197202c2..e09ae4c4 100644 --- a/aiogram/types/reply_keyboard.py +++ b/aiogram/types/reply_keyboard.py @@ -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)