From fed8cbf87e8f5921fad437c603f569b7c83d6012 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 11 Jul 2019 19:28:33 +0200 Subject: [PATCH] Add new set_parse_mode utility method --- compiler/docs/compiler.py | 1 + pyrogram/client/client.py | 78 +++++++++++++++++++ pyrogram/client/ext/base_client.py | 3 + .../methods/messages/edit_inline_text.py | 2 +- .../methods/messages/edit_message_text.py | 2 +- .../client/methods/messages/send_animation.py | 2 +- .../client/methods/messages/send_audio.py | 2 +- .../methods/messages/send_cached_media.py | 2 +- .../client/methods/messages/send_document.py | 2 +- .../client/methods/messages/send_message.py | 2 +- .../client/methods/messages/send_photo.py | 2 +- .../client/methods/messages/send_video.py | 2 +- .../client/methods/messages/send_voice.py | 2 +- pyrogram/client/parser/parser.py | 14 +++- .../input_media/input_media_animation.py | 2 +- .../types/input_media/input_media_audio.py | 2 +- .../types/input_media/input_media_document.py | 2 +- .../types/input_media/input_media_photo.py | 2 +- .../types/input_media/input_media_video.py | 2 +- .../input_text_message_content.py | 2 +- 20 files changed, 109 insertions(+), 19 deletions(-) diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index d261623d..c4a0f8a8 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -136,6 +136,7 @@ def pyrogram_api(): remove_handler stop_transmission export_session_string + set_parse_mode """, messages=""" Messages diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index c660ef21..81eef671 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -491,6 +491,84 @@ class Client(Methods, BaseClient): """ raise Client.StopTransmission + def export_session_string(self): + """Export the current authorized session as a serialized string. + + Session strings are useful for storing in-memory authorized sessions in a portable, serialized string. + More detailed information about session strings can be found at the dedicated page of + :doc:`Storage Engines <../../topics/storage-engines>`. + + Has no parameters. + + Returns: + ``str``: The session serialized into a printable, url-safe string. + + Example: + .. code-block:: python + :emphasize-lines: 6 + + from pyrogram import Client + + app = Client("my_account") + + with app: + print(app.export_session_string()) + """ + return self.storage.export_session_string() + + def set_parse_mode(self, parse_mode: Union[str, None] = "combined"): + """Set the parse mode to be used globally by the client. + + When setting the parse mode with this method, all methods having a *parse_mode* parameter will follow the global + value by default. The default value *"combined"* enables both Markdown and HTML styles to be used and combined + together. + + Parameters: + parse_mode (``str``): + The new parse mode, can be any of: *"combined"*, for the default combined mode. *"markdown"* or *"md"* + to force Markdown-only styles. *"html"* to force HTML-only styles. *None* to disable the parser + completely. + + Raises: + ValueError: In case the provided *parse_mode* is not a valid parse mode. + + Example: + .. code-block:: python + :emphasize-lines: 10,14,18,22 + + from pyrogram import Client + + app = Client("my_account") + + with app: + # Default combined mode: Markdown + HTML + app.send_message("haskell", "1. **markdown** and html") + + # Force Markdown-only, HTML is disabled + app.set_parse_mode("markdown") + app.send_message("haskell", "2. **markdown** and html") + + # Force HTML-only, Markdown is disabled + app.set_parse_mode("html") + app.send_message("haskell", "3. **markdown** and html") + + # Disable the parser completely + app.set_parse_mode(None) + app.send_message("haskell", "4. **markdown** and html") + + # Bring back the default combined mode + app.set_parse_mode() + app.send_message("haskell", "5. **markdown** and html") + """ + + if parse_mode not in self.PARSE_MODES: + raise ValueError('parse_mode must be one of {} or None. Not "{}"'.format( + ", ".join('"{}"'.format(m) for m in self.PARSE_MODES[:-1]), + parse_mode + )) + + self.parse_mode = parse_mode + def authorize_bot(self): try: r = self.send( diff --git a/pyrogram/client/ext/base_client.py b/pyrogram/client/ext/base_client.py index b5be089b..bb024250 100644 --- a/pyrogram/client/ext/base_client.py +++ b/pyrogram/client/ext/base_client.py @@ -59,6 +59,8 @@ class BaseClient: WORKDIR = PARENT_DIR CONFIG_FILE = PARENT_DIR / "config.ini" + PARSE_MODES = ["combined", "markdown", "md", "html", None] + MEDIA_TYPE_ID = { 0: "photo_thumbnail", 1: "chat_photo", @@ -93,6 +95,7 @@ class BaseClient: self.rnd_id = MsgId self.parser = Parser(self) + self.parse_mode = "combined" self.session = None self.media_sessions = {} diff --git a/pyrogram/client/methods/messages/edit_inline_text.py b/pyrogram/client/methods/messages/edit_inline_text.py index 0d17b4a4..9b0b34d3 100644 --- a/pyrogram/client/methods/messages/edit_inline_text.py +++ b/pyrogram/client/methods/messages/edit_inline_text.py @@ -28,7 +28,7 @@ class EditInlineText(BaseClient): self, inline_message_id: str, text: str, - parse_mode: Union[str, None] = "", + parse_mode: Union[str, None] = object, disable_web_page_preview: bool = None, reply_markup: "pyrogram.InlineKeyboardMarkup" = None ) -> bool: diff --git a/pyrogram/client/methods/messages/edit_message_text.py b/pyrogram/client/methods/messages/edit_message_text.py index c81139af..063c8c72 100644 --- a/pyrogram/client/methods/messages/edit_message_text.py +++ b/pyrogram/client/methods/messages/edit_message_text.py @@ -29,7 +29,7 @@ class EditMessageText(BaseClient): chat_id: Union[int, str], message_id: int, text: str, - parse_mode: Union[str, None] = "", + parse_mode: Union[str, None] = object, disable_web_page_preview: bool = None, reply_markup: "pyrogram.InlineKeyboardMarkup" = None ) -> "pyrogram.Message": diff --git a/pyrogram/client/methods/messages/send_animation.py b/pyrogram/client/methods/messages/send_animation.py index 34389149..a68984fc 100644 --- a/pyrogram/client/methods/messages/send_animation.py +++ b/pyrogram/client/methods/messages/send_animation.py @@ -32,7 +32,7 @@ class SendAnimation(BaseClient): animation: str, caption: str = "", unsave: bool = False, - parse_mode: Union[str, None] = "", + parse_mode: Union[str, None] = object, duration: int = 0, width: int = 0, height: int = 0, diff --git a/pyrogram/client/methods/messages/send_audio.py b/pyrogram/client/methods/messages/send_audio.py index 43c5a63e..c8ce7368 100644 --- a/pyrogram/client/methods/messages/send_audio.py +++ b/pyrogram/client/methods/messages/send_audio.py @@ -31,7 +31,7 @@ class SendAudio(BaseClient): chat_id: Union[int, str], audio: str, caption: str = "", - parse_mode: Union[str, None] = "", + parse_mode: Union[str, None] = object, duration: int = 0, performer: str = None, title: str = None, diff --git a/pyrogram/client/methods/messages/send_cached_media.py b/pyrogram/client/methods/messages/send_cached_media.py index 0f2e1389..59a24171 100644 --- a/pyrogram/client/methods/messages/send_cached_media.py +++ b/pyrogram/client/methods/messages/send_cached_media.py @@ -29,7 +29,7 @@ class SendCachedMedia(BaseClient): chat_id: Union[int, str], file_id: str, caption: str = "", - parse_mode: Union[str, None] = "", + parse_mode: Union[str, None] = object, disable_notification: bool = None, reply_to_message_id: int = None, reply_markup: Union[ diff --git a/pyrogram/client/methods/messages/send_document.py b/pyrogram/client/methods/messages/send_document.py index fcaf5f51..a3cec395 100644 --- a/pyrogram/client/methods/messages/send_document.py +++ b/pyrogram/client/methods/messages/send_document.py @@ -32,7 +32,7 @@ class SendDocument(BaseClient): document: str, thumb: str = None, caption: str = "", - parse_mode: Union[str, None] = "", + parse_mode: Union[str, None] = object, disable_notification: bool = None, reply_to_message_id: int = None, reply_markup: Union[ diff --git a/pyrogram/client/methods/messages/send_message.py b/pyrogram/client/methods/messages/send_message.py index 15b5f25c..f652f3d9 100644 --- a/pyrogram/client/methods/messages/send_message.py +++ b/pyrogram/client/methods/messages/send_message.py @@ -28,7 +28,7 @@ class SendMessage(BaseClient): self, chat_id: Union[int, str], text: str, - parse_mode: Union[str, None] = "", + parse_mode: Union[str, None] = object, disable_web_page_preview: bool = None, disable_notification: bool = None, reply_to_message_id: int = None, diff --git a/pyrogram/client/methods/messages/send_photo.py b/pyrogram/client/methods/messages/send_photo.py index c43c0139..981b0045 100644 --- a/pyrogram/client/methods/messages/send_photo.py +++ b/pyrogram/client/methods/messages/send_photo.py @@ -31,7 +31,7 @@ class SendPhoto(BaseClient): chat_id: Union[int, str], photo: str, caption: str = "", - parse_mode: Union[str, None] = "", + parse_mode: Union[str, None] = object, ttl_seconds: int = None, disable_notification: bool = None, reply_to_message_id: int = None, diff --git a/pyrogram/client/methods/messages/send_video.py b/pyrogram/client/methods/messages/send_video.py index ba69aafb..602e3b01 100644 --- a/pyrogram/client/methods/messages/send_video.py +++ b/pyrogram/client/methods/messages/send_video.py @@ -31,7 +31,7 @@ class SendVideo(BaseClient): chat_id: Union[int, str], video: str, caption: str = "", - parse_mode: Union[str, None] = "", + parse_mode: Union[str, None] = object, duration: int = 0, width: int = 0, height: int = 0, diff --git a/pyrogram/client/methods/messages/send_voice.py b/pyrogram/client/methods/messages/send_voice.py index 854385d8..9c0b8514 100644 --- a/pyrogram/client/methods/messages/send_voice.py +++ b/pyrogram/client/methods/messages/send_voice.py @@ -31,7 +31,7 @@ class SendVoice(BaseClient): chat_id: Union[int, str], voice: str, caption: str = "", - parse_mode: Union[str, None] = "", + parse_mode: Union[str, None] = object, duration: int = 0, disable_notification: bool = None, reply_to_message_id: int = None, diff --git a/pyrogram/client/parser/parser.py b/pyrogram/client/parser/parser.py index 8fde46bd..e6681b32 100644 --- a/pyrogram/client/parser/parser.py +++ b/pyrogram/client/parser/parser.py @@ -19,7 +19,6 @@ from collections import OrderedDict from typing import Union - import pyrogram from .html import HTML from .markdown import Markdown @@ -27,12 +26,16 @@ from .markdown import Markdown class Parser: def __init__(self, client: Union["pyrogram.BaseClient", None]): + self.client = client self.html = HTML(client) self.markdown = Markdown(client) - def parse(self, text: str, mode: str = ""): + def parse(self, text: str, mode: Union[str, None] = object): text = str(text or "").strip() + if mode == object: + mode = self.client.parse_mode + if mode is None: return OrderedDict([ ("message", text), @@ -41,7 +44,7 @@ class Parser: mode = mode.lower() - if mode == "": + if mode == "combined": return self.markdown.parse(text) if mode in ["markdown", "md"]: @@ -50,6 +53,11 @@ class Parser: if mode == "html": return self.html.parse(text) + raise ValueError('parse_mode must be one of {} or None. Not "{}"'.format( + ", ".join('"{}"'.format(m) for m in self.client.PARSE_MODES[:-1]), + mode + )) + @staticmethod def unparse(text: str, entities: list, is_html: bool): if is_html: diff --git a/pyrogram/client/types/input_media/input_media_animation.py b/pyrogram/client/types/input_media/input_media_animation.py index e157993b..d6c67d56 100644 --- a/pyrogram/client/types/input_media/input_media_animation.py +++ b/pyrogram/client/types/input_media/input_media_animation.py @@ -63,7 +63,7 @@ class InputMediaAnimation(InputMedia): media: str, thumb: str = None, caption: str = "", - parse_mode: Union[str, None] = "", + parse_mode: Union[str, None] = object, width: int = 0, height: int = 0, duration: int = 0 diff --git a/pyrogram/client/types/input_media/input_media_audio.py b/pyrogram/client/types/input_media/input_media_audio.py index 3eb3ea65..f01444a8 100644 --- a/pyrogram/client/types/input_media/input_media_audio.py +++ b/pyrogram/client/types/input_media/input_media_audio.py @@ -65,7 +65,7 @@ class InputMediaAudio(InputMedia): media: str, thumb: str = None, caption: str = "", - parse_mode: Union[str, None] = "", + parse_mode: Union[str, None] = object, duration: int = 0, performer: int = "", title: str = "" diff --git a/pyrogram/client/types/input_media/input_media_document.py b/pyrogram/client/types/input_media/input_media_document.py index 7aca9a31..af549c81 100644 --- a/pyrogram/client/types/input_media/input_media_document.py +++ b/pyrogram/client/types/input_media/input_media_document.py @@ -54,7 +54,7 @@ class InputMediaDocument(InputMedia): media: str, thumb: str = None, caption: str = "", - parse_mode: Union[str, None] = "" + parse_mode: Union[str, None] = object ): super().__init__(media, caption, parse_mode) diff --git a/pyrogram/client/types/input_media/input_media_photo.py b/pyrogram/client/types/input_media/input_media_photo.py index d2f26a88..30c53777 100644 --- a/pyrogram/client/types/input_media/input_media_photo.py +++ b/pyrogram/client/types/input_media/input_media_photo.py @@ -49,6 +49,6 @@ class InputMediaPhoto(InputMedia): self, media: str, caption: str = "", - parse_mode: Union[str, None] = "" + parse_mode: Union[str, None] = object ): super().__init__(media, caption, parse_mode) diff --git a/pyrogram/client/types/input_media/input_media_video.py b/pyrogram/client/types/input_media/input_media_video.py index d2ee851d..3500ff55 100644 --- a/pyrogram/client/types/input_media/input_media_video.py +++ b/pyrogram/client/types/input_media/input_media_video.py @@ -68,7 +68,7 @@ class InputMediaVideo(InputMedia): media: str, thumb: str = None, caption: str = "", - parse_mode: Union[str, None] = "", + parse_mode: Union[str, None] = object, width: int = 0, height: int = 0, duration: int = 0, diff --git a/pyrogram/client/types/input_message_content/input_text_message_content.py b/pyrogram/client/types/input_message_content/input_text_message_content.py index f4b9aefc..f90b7096 100644 --- a/pyrogram/client/types/input_message_content/input_text_message_content.py +++ b/pyrogram/client/types/input_message_content/input_text_message_content.py @@ -43,7 +43,7 @@ class InputTextMessageContent(InputMessageContent): __slots__ = ["message_text", "parse_mode", "disable_web_page_preview"] - def __init__(self, message_text: str, parse_mode: Union[str, None] = "", disable_web_page_preview: bool = None): + def __init__(self, message_text: str, parse_mode: Union[str, None] = object, disable_web_page_preview: bool = None): super().__init__() self.message_text = message_text