From 3863b4f371f99a864c23dba6532ee9671da19f9b Mon Sep 17 00:00:00 2001 From: saschalalala Date: Wed, 14 Jun 2017 00:07:03 +0200 Subject: [PATCH] Rename shortcut functions to snake_case (#661) * Rename shortcut functions to snake_case * More function renaming * Example function rewrite * Add myself to authors.rst * More function renaming * Rename mockbot test functions * Break comment line for flake max line length --- AUTHORS.rst | 1 + examples/paymentbot.py | 14 +++++----- telegram/bot.py | 2 +- telegram/chat.py | 28 ++++++++++---------- telegram/ext/updater.py | 10 ++++---- telegram/inlinequery.py | 4 +-- telegram/message.py | 50 ++++++++++++++++++------------------ telegram/precheckoutquery.py | 5 ++-- telegram/shippingquery.py | 4 +-- telegram/user.py | 4 +-- tests/test_updater.py | 12 ++++----- 11 files changed, 68 insertions(+), 66 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 0828a20dc..040853d53 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -51,6 +51,7 @@ The following wonderful people contributed directly or indirectly to this projec - `Pieter Schutz `_ - `Rahiel Kasim `_ - `Joscha Götzer `_ +- `Sascha `_ - `Shelomentsev D `_ - `sooyhwang `_ - `thodnev `_ diff --git a/examples/paymentbot.py b/examples/paymentbot.py index ddc3774c2..7a0e13419 100644 --- a/examples/paymentbot.py +++ b/examples/paymentbot.py @@ -75,8 +75,8 @@ def shipping_callback(bot, update): # check the payload, is this from your bot? if query.invoice_payload != 'Custom-Payload': # answer False pre_checkout_query - bot.answerShippingQuery(shipping_query_id=query.id, ok=False, - error_message="Something went wrong...") + bot.answer_shipping_query(shipping_query_id=query.id, ok=False, + error_message="Something went wrong...") return else: options = list() @@ -85,8 +85,8 @@ def shipping_callback(bot, update): # an array of LabeledPrice objects price_list = [LabeledPrice('B1', 150), LabeledPrice('B2', 200)] options.append(ShippingOption('2', 'Shipping Option B', price_list)) - bot.answerShippingQuery(shipping_query_id=query.id, ok=True, - shipping_options=options) + bot.answer_shipping_query(shipping_query_id=query.id, ok=True, + shipping_options=options) # after (optional) shipping, it's the pre-checkout @@ -95,10 +95,10 @@ def precheckout_callback(bot, update): # check the payload, is this from your bot? if query.invoice_payload != 'Custom-Payload': # answer False pre_checkout_query - bot.answerPreCheckoutQuery(pre_checkout_query_id=query.id, ok=False, - error_message="Something went wrong...") + bot.answer_pre_checkout_query(pre_checkout_query_id=query.id, ok=False, + error_message="Something went wrong...") else: - bot.answerPreCheckoutQuery(pre_checkout_query_id=query.id, ok=True) + bot.answer_pre_checkout_query(pre_checkout_query_id=query.id, ok=True) # finally, after contacting to the payment provider... diff --git a/telegram/bot.py b/telegram/bot.py index 7fd57c54b..05b617405 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -85,7 +85,7 @@ class Bot(TelegramObject): @functools.wraps(func) def decorator(self, *args, **kwargs): if not self.bot: - self.getMe() + self.get_me() result = func(self, *args, **kwargs) return result diff --git a/telegram/chat.py b/telegram/chat.py index ba291fa1d..a1efa528d 100644 --- a/telegram/chat.py +++ b/telegram/chat.py @@ -89,29 +89,29 @@ class Chat(TelegramObject): return Chat(bot=bot, **data) def send_action(self, *args, **kwargs): - """Shortcut for ``bot.sendChatAction(update.message.chat.id, *args, **kwargs)``""" - return self.bot.sendChatAction(self.id, *args, **kwargs) + """Shortcut for ``bot.send_chat_action(update.message.chat.id, *args, **kwargs)``""" + return self.bot.send_chat_action(self.id, *args, **kwargs) def leave(self, *args, **kwargs): - """Shortcut for ``bot.leaveChat(update.message.chat.id, *args, **kwargs)``""" - return self.bot.leaveChat(self.id, *args, **kwargs) + """Shortcut for ``bot.leave_chat(update.message.chat.id, *args, **kwargs)``""" + return self.bot.leave_chat(self.id, *args, **kwargs) def get_administrators(self, *args, **kwargs): - """Shortcut for ``bot.getChatAdministrators(update.message.chat.id, *args, **kwargs)``""" - return self.bot.getChatAdministrators(self.id, *args, **kwargs) + """Shortcut for ``bot.get_chat_administrators(update.message.chat.id, *args, **kwargs)``""" + return self.bot.get_chat_administrators(self.id, *args, **kwargs) def get_members_count(self, *args, **kwargs): - """Shortcut for ``bot.getChatMembersCount(update.message.chat.id, *args, **kwargs)``""" - return self.bot.getChatMembersCount(self.id, *args, **kwargs) + """Shortcut for ``bot.get_chat_members_count(update.message.chat.id, *args, **kwargs)``""" + return self.bot.get_chat_members_count(self.id, *args, **kwargs) def get_member(self, *args, **kwargs): - """Shortcut for ``bot.getChatMember(update.message.chat.id, *args, **kwargs)``""" - return self.bot.getChatMember(self.id, *args, **kwargs) + """Shortcut for ``bot.get_chat_member(update.message.chat.id, *args, **kwargs)``""" + return self.bot.get_chat_member(self.id, *args, **kwargs) def kick_member(self, *args, **kwargs): - """Shortcut for ``bot.kickChatMember(update.message.chat.id, *args, **kwargs)``""" - return self.bot.kickChatMember(self.id, *args, **kwargs) + """Shortcut for ``bot.kick_chat_member(update.message.chat.id, *args, **kwargs)``""" + return self.bot.kick_chat_member(self.id, *args, **kwargs) def unban_member(self, *args, **kwargs): - """Shortcut for ``bot.unbanChatMember(update.message.chat.id, *args, **kwargs)``""" - return self.bot.unbanChatMember(self.id, *args, **kwargs) + """Shortcut for ``bot.unban_chat_member(update.message.chat.id, *args, **kwargs)``""" + return self.bot.unban_chat_member(self.id, *args, **kwargs) diff --git a/telegram/ext/updater.py b/telegram/ext/updater.py index 04b85663b..013e447d4 100644 --- a/telegram/ext/updater.py +++ b/telegram/ext/updater.py @@ -264,7 +264,7 @@ class Updater(object): while self.running: try: - updates = self.bot.getUpdates( + updates = self.bot.get_updates( self.last_update_id, timeout=timeout, read_latency=read_latency, @@ -367,11 +367,11 @@ class Updater(object): try: if clean: # Disable webhook for cleaning - self.bot.deleteWebhook() + self.bot.delete_webhook() self._clean_updates() sleep(1) - self.bot.setWebhook( + self.bot.set_webhook( url=webhook_url, certificate=cert, allowed_updates=allowed_updates) except (Unauthorized, InvalidToken): raise @@ -390,9 +390,9 @@ class Updater(object): def _clean_updates(self): self.logger.debug('Cleaning updates from Telegram server') - updates = self.bot.getUpdates() + updates = self.bot.get_updates() while updates: - updates = self.bot.getUpdates(updates[-1].update_id + 1) + updates = self.bot.get_updates(updates[-1].update_id + 1) def stop(self): """ diff --git a/telegram/inlinequery.py b/telegram/inlinequery.py index 23bdd20f4..d8c7025b4 100644 --- a/telegram/inlinequery.py +++ b/telegram/inlinequery.py @@ -91,5 +91,5 @@ class InlineQuery(TelegramObject): return data def answer(self, *args, **kwargs): - """Shortcut for ``bot.answerInlineQuery(update.inline_query.id, *args, **kwargs)``""" - return self.bot.answerInlineQuery(self.id, *args, **kwargs) + """Shortcut for ``bot.answer_inline_query(update.inline_query.id, *args, **kwargs)``""" + return self.bot.answer_inline_query(self.id, *args, **kwargs) diff --git a/telegram/message.py b/telegram/message.py index 006fac967..73273e39b 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -328,7 +328,7 @@ class Message(TelegramObject): def reply_text(self, *args, **kwargs): """ - Shortcut for ``bot.sendMessage(update.message.chat_id, *args, **kwargs)`` + Shortcut for ``bot.send_message(update.message.chat_id, *args, **kwargs)`` Keyword Args: quote (Optional[bool]): If set to ``True``, the message is sent as an actual reply to @@ -337,11 +337,11 @@ class Message(TelegramObject): """ self._quote(kwargs) - return self.bot.sendMessage(self.chat_id, *args, **kwargs) + return self.bot.send_message(self.chat_id, *args, **kwargs) def reply_photo(self, *args, **kwargs): """ - Shortcut for ``bot.sendPhoto(update.message.chat_id, *args, **kwargs)`` + Shortcut for ``bot.send_photo(update.message.chat_id, *args, **kwargs)`` Keyword Args: quote (Optional[bool]): If set to ``True``, the photo is sent as an actual reply to @@ -354,11 +354,11 @@ class Message(TelegramObject): """ self._quote(kwargs) - return self.bot.sendPhoto(self.chat_id, *args, **kwargs) + return self.bot.send_photo(self.chat_id, *args, **kwargs) def reply_audio(self, *args, **kwargs): """ - Shortcut for ``bot.sendAudio(update.message.chat_id, *args, **kwargs)`` + Shortcut for ``bot.send_audio(update.message.chat_id, *args, **kwargs)`` Keyword Args: quote (Optional[bool]): If set to ``True``, the audio is sent as an actual reply to @@ -371,11 +371,11 @@ class Message(TelegramObject): """ self._quote(kwargs) - return self.bot.sendAudio(self.chat_id, *args, **kwargs) + return self.bot.send_audio(self.chat_id, *args, **kwargs) def reply_document(self, *args, **kwargs): """ - Shortcut for ``bot.sendDocument(update.message.chat_id, *args, **kwargs)`` + Shortcut for ``bot.send_document(update.message.chat_id, *args, **kwargs)`` Keyword Args: quote (Optional[bool]): If set to ``True``, the document is sent as an actual reply to @@ -388,11 +388,11 @@ class Message(TelegramObject): """ self._quote(kwargs) - return self.bot.sendDocument(self.chat_id, *args, **kwargs) + return self.bot.send_document(self.chat_id, *args, **kwargs) def reply_sticker(self, *args, **kwargs): """ - Shortcut for ``bot.sendSticker(update.message.chat_id, *args, **kwargs)`` + Shortcut for ``bot.send_sticker(update.message.chat_id, *args, **kwargs)`` Keyword Args: quote (Optional[bool]): If set to ``True``, the sticker is sent as an actual reply to @@ -405,11 +405,11 @@ class Message(TelegramObject): """ self._quote(kwargs) - return self.bot.sendSticker(self.chat_id, *args, **kwargs) + return self.bot.send_sticker(self.chat_id, *args, **kwargs) def reply_video(self, *args, **kwargs): """ - Shortcut for ``bot.sendVideo(update.message.chat_id, *args, **kwargs)`` + Shortcut for ``bot.send_video(update.message.chat_id, *args, **kwargs)`` Keyword Args: quote (Optional[bool]): If set to ``True``, the video is sent as an actual reply to @@ -422,7 +422,7 @@ class Message(TelegramObject): """ self._quote(kwargs) - return self.bot.sendVideo(self.chat_id, *args, **kwargs) + return self.bot.send_video(self.chat_id, *args, **kwargs) def reply_video_note(self, *args, **kwargs): """ @@ -443,7 +443,7 @@ class Message(TelegramObject): def reply_voice(self, *args, **kwargs): """ - Shortcut for ``bot.sendVoice(update.message.chat_id, *args, **kwargs)`` + Shortcut for ``bot.send_voice(update.message.chat_id, *args, **kwargs)`` Keyword Args: quote (Optional[bool]): If set to ``True``, the voice is sent as an actual reply to @@ -456,11 +456,11 @@ class Message(TelegramObject): """ self._quote(kwargs) - return self.bot.sendVoice(self.chat_id, *args, **kwargs) + return self.bot.send_voice(self.chat_id, *args, **kwargs) def reply_location(self, *args, **kwargs): """ - Shortcut for ``bot.sendLocation(update.message.chat_id, *args, **kwargs)`` + Shortcut for ``bot.send_location(update.message.chat_id, *args, **kwargs)`` Keyword Args: quote (Optional[bool]): If set to ``True``, the location is sent as an actual reply to @@ -473,11 +473,11 @@ class Message(TelegramObject): """ self._quote(kwargs) - return self.bot.sendLocation(self.chat_id, *args, **kwargs) + return self.bot.send_location(self.chat_id, *args, **kwargs) def reply_venue(self, *args, **kwargs): """ - Shortcut for ``bot.sendVenue(update.message.chat_id, *args, **kwargs)`` + Shortcut for ``bot.send_venue(update.message.chat_id, *args, **kwargs)`` Keyword Args: quote (Optional[bool]): If set to ``True``, the venue is sent as an actual reply to @@ -490,11 +490,11 @@ class Message(TelegramObject): """ self._quote(kwargs) - return self.bot.sendVenue(self.chat_id, *args, **kwargs) + return self.bot.send_venue(self.chat_id, *args, **kwargs) def reply_contact(self, *args, **kwargs): """ - Shortcut for ``bot.sendContact(update.message.chat_id, *args, **kwargs)`` + Shortcut for ``bot.send_contact(update.message.chat_id, *args, **kwargs)`` Keyword Args: quote (Optional[bool]): If set to ``True``, the contact is sent as an actual reply to @@ -507,12 +507,12 @@ class Message(TelegramObject): """ self._quote(kwargs) - return self.bot.sendContact(self.chat_id, *args, **kwargs) + return self.bot.send_contact(self.chat_id, *args, **kwargs) def forward(self, chat_id, disable_notification=False): """Shortcut for - >>> bot.forwardMessage(chat_id=chat_id, + >>> bot.forward_message(chat_id=chat_id, ... from_chat_id=update.message.chat_id, ... disable_notification=disable_notification, ... message_id=update.message.message_id) @@ -521,7 +521,7 @@ class Message(TelegramObject): :class:`telegram.Message`: On success, instance representing the message forwarded. """ - return self.bot.forwardMessage( + return self.bot.forward_message( chat_id=chat_id, from_chat_id=self.chat_id, disable_notification=disable_notification, @@ -531,7 +531,7 @@ class Message(TelegramObject): """ Shortcut for - >>> bot.editMessageText(chat_id=message.chat_id, + >>> bot.edit_message_text(chat_id=message.chat_id, ... message_id=message.message_id, ... *args, **kwargs) @@ -548,7 +548,7 @@ class Message(TelegramObject): """ Shortcut for - >>> bot.editMessageCaption(chat_id=message.chat_id, + >>> bot.edit_message_caption(chat_id=message.chat_id, ... message_id=message.message_id, ... *args, **kwargs) @@ -564,7 +564,7 @@ class Message(TelegramObject): """ Shortcut for - >>> bot.editReplyMarkup(chat_id=message.chat_id, + >>> bot.edit_message_reply_markup(chat_id=message.chat_id, ... message_id=message.message_id, ... *args, **kwargs) diff --git a/telegram/precheckoutquery.py b/telegram/precheckoutquery.py index 327449a92..6b0732488 100644 --- a/telegram/precheckoutquery.py +++ b/telegram/precheckoutquery.py @@ -95,6 +95,7 @@ class PreCheckoutQuery(TelegramObject): def answer(self, *args, **kwargs): """ - Shortcut for ``bot.answerPreCheckoutQuery(update.pre_checkout_query.id, *args, **kwargs)`` + Shortcut for + ``bot.answer_pre_checkout_query(update.pre_checkout_query.id, *args, **kwargs)`` """ - return self.bot.answerPreCheckoutQuery(self.id, *args, **kwargs) + return self.bot.answer_pre_checkout_query(self.id, *args, **kwargs) diff --git a/telegram/shippingquery.py b/telegram/shippingquery.py index b267b2f25..4c955a2bc 100644 --- a/telegram/shippingquery.py +++ b/telegram/shippingquery.py @@ -79,5 +79,5 @@ class ShippingQuery(TelegramObject): return data def answer(self, *args, **kwargs): - """Shortcut for ``bot.answerShippingQuery(update.shipping_query.id, *args, **kwargs)``""" - return self.bot.answerShippingQuery(self.id, *args, **kwargs) + """Shortcut for ``bot.answer_shipping_query(update.shipping_query.id, *args, **kwargs)``""" + return self.bot.answer_shipping_query(self.id, *args, **kwargs) diff --git a/telegram/user.py b/telegram/user.py index 321544058..0a0a1e225 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -96,9 +96,9 @@ class User(TelegramObject): def get_profile_photos(self, *args, **kwargs): """ - Shortcut for ``bot.getUserProfilePhotos(update.message.from_user.id, *args, **kwargs)`` + Shortcut for ``bot.get_user_profile_photos(update.message.from_user.id, *args, **kwargs)`` """ - return self.bot.getUserProfilePhotos(self.id, *args, **kwargs) + return self.bot.get_user_profile_photos(self.id, *args, **kwargs) @staticmethod def de_list(data, bot): diff --git a/tests/test_updater.py b/tests/test_updater.py index e6ca53ccc..0eed0678e 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -944,7 +944,7 @@ class MockBot(object): self.edited = edited self.username = "MockBot" - def mockUpdate(self, text): + def mock_update(self, text): message = Message(0, User(0, 'Testuser'), None, Chat(0, Chat.GROUP), bot=self) message.text = text update = Update(0) @@ -956,7 +956,7 @@ class MockBot(object): return update - def setWebhook(self, url=None, certificate=None, allowed_updates=None): + def set_webhook(self, url=None, certificate=None, allowed_updates=None): if self.bootstrap_retries is None: return @@ -964,7 +964,7 @@ class MockBot(object): self.bootstrap_attempts += 1 raise self.bootstrap_err - def deleteWebhook(self): + def delete_webhook(self): if self.bootstrap_retries is None: return @@ -972,7 +972,7 @@ class MockBot(object): self.bootstrap_attempts += 1 raise self.bootstrap_err - def getUpdates(self, + def get_updates(self, offset=None, limit=100, timeout=0, @@ -984,10 +984,10 @@ class MockBot(object): raise TelegramError('Test Error 2') elif self.send_messages >= 2: self.send_messages -= 2 - return self.mockUpdate(self.text), self.mockUpdate(self.text) + return self.mock_update(self.text), self.mock_update(self.text) elif self.send_messages == 1: self.send_messages -= 1 - return self.mockUpdate(self.text), + return self.mock_update(self.text), else: return []