2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

Update usages of Parser all around the library

This commit is contained in:
Dan 2019-06-26 16:08:24 +02:00
parent e61bf92627
commit f05e79e0f4
13 changed files with 32 additions and 39 deletions

View File

@ -25,7 +25,7 @@ from queue import Queue
from threading import Lock
from pyrogram import __version__
from ..style import Markdown, HTML
from ..parser import Parser
from ...session.internals import MsgId
@ -92,8 +92,7 @@ class BaseClient:
self.rnd_id = MsgId
self.markdown = Markdown(self)
self.html = HTML(self)
self.parser = Parser(self)
self.session = None
self.media_sessions = {}

View File

@ -16,6 +16,8 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Union
import pyrogram
from pyrogram.api import functions
from pyrogram.client.ext import BaseClient, utils
@ -26,7 +28,7 @@ class EditInlineText(BaseClient):
self,
inline_message_id: str,
text: str,
parse_mode: str = "",
parse_mode: Union[str, None] = "",
disable_web_page_preview: bool = None,
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
) -> bool:
@ -55,13 +57,12 @@ class EditInlineText(BaseClient):
Raises:
RPCError: In case of a Telegram RPC error.
"""
style = self.html if parse_mode.lower() == "html" else self.markdown
return self.send(
functions.messages.EditInlineBotMessage(
id=utils.unpack_inline_message_id(inline_message_id),
no_webpage=disable_web_page_preview or None,
reply_markup=reply_markup.write() if reply_markup else None,
**style.parse(text)
**self.parser.parse(text, parse_mode)
)
)

View File

@ -29,7 +29,7 @@ class EditMessageText(BaseClient):
chat_id: Union[int, str],
message_id: int,
text: str,
parse_mode: str = "",
parse_mode: Union[str, None] = "",
disable_web_page_preview: bool = None,
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
) -> "pyrogram.Message":
@ -63,7 +63,6 @@ class EditMessageText(BaseClient):
Raises:
RPCError: In case of a Telegram RPC error.
"""
style = self.html if parse_mode.lower() == "html" else self.markdown
r = self.send(
functions.messages.EditMessage(
@ -71,7 +70,7 @@ class EditMessageText(BaseClient):
id=message_id,
no_webpage=disable_web_page_preview or None,
reply_markup=reply_markup.write() if reply_markup else None,
**style.parse(text)
**self.parser.parse(text, parse_mode)
)
)

View File

@ -32,7 +32,7 @@ class SendAnimation(BaseClient):
animation: str,
caption: str = "",
unsave: bool = False,
parse_mode: str = "",
parse_mode: Union[str, None] = "",
duration: int = 0,
width: int = 0,
height: int = 0,
@ -130,7 +130,6 @@ class SendAnimation(BaseClient):
RPCError: In case of a Telegram RPC error.
"""
file = None
style = self.html if parse_mode.lower() == "html" else self.markdown
try:
if os.path.exists(animation):
@ -168,7 +167,7 @@ class SendAnimation(BaseClient):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
reply_markup=reply_markup.write() if reply_markup else None,
**style.parse(caption)
**self.parser.parse(caption, parse_mode)
)
)
except FilePartMissing as e:

View File

@ -31,7 +31,7 @@ class SendAudio(BaseClient):
chat_id: Union[int, str],
audio: str,
caption: str = "",
parse_mode: str = "",
parse_mode: Union[str, None] = "",
duration: int = 0,
performer: str = None,
title: str = None,
@ -127,7 +127,6 @@ class SendAudio(BaseClient):
RPCError: In case of a Telegram RPC error.
"""
file = None
style = self.html if parse_mode.lower() == "html" else self.markdown
try:
if os.path.exists(audio):
@ -163,7 +162,7 @@ class SendAudio(BaseClient):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
reply_markup=reply_markup.write() if reply_markup else None,
**style.parse(caption)
**self.parser.parse(caption, parse_mode)
)
)
except FilePartMissing as e:

View File

@ -29,7 +29,7 @@ class SendCachedMedia(BaseClient):
chat_id: Union[int, str],
file_id: str,
caption: str = "",
parse_mode: str = "",
parse_mode: Union[str, None] = "",
disable_notification: bool = None,
reply_to_message_id: int = None,
reply_markup: Union[
@ -79,7 +79,6 @@ class SendCachedMedia(BaseClient):
Raises:
RPCError: In case of a Telegram RPC error.
"""
style = self.html if parse_mode.lower() == "html" else self.markdown
r = self.send(
functions.messages.SendMedia(
@ -89,7 +88,7 @@ class SendCachedMedia(BaseClient):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
reply_markup=reply_markup.write() if reply_markup else None,
**style.parse(caption)
**self.parser.parse(caption, parse_mode)
)
)

View File

@ -32,7 +32,7 @@ class SendDocument(BaseClient):
document: str,
thumb: str = None,
caption: str = "",
parse_mode: str = "",
parse_mode: Union[str, None] = "",
disable_notification: bool = None,
reply_to_message_id: int = None,
reply_markup: Union[
@ -113,7 +113,6 @@ class SendDocument(BaseClient):
RPCError: In case of a Telegram RPC error.
"""
file = None
style = self.html if parse_mode.lower() == "html" else self.markdown
try:
if os.path.exists(document):
@ -144,7 +143,7 @@ class SendDocument(BaseClient):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
reply_markup=reply_markup.write() if reply_markup else None,
**style.parse(caption)
**self.parser(caption, parse_mode)
)
)
except FilePartMissing as e:

View File

@ -28,7 +28,7 @@ class SendMessage(BaseClient):
self,
chat_id: Union[int, str],
text: str,
parse_mode: str = "",
parse_mode: Union[str, None] = "",
disable_web_page_preview: bool = None,
disable_notification: bool = None,
reply_to_message_id: int = None,
@ -74,8 +74,7 @@ class SendMessage(BaseClient):
Raises:
RPCError: In case of a Telegram RPC error.
"""
style = self.html if parse_mode.lower() == "html" else self.markdown
message, entities = style.parse(text).values()
message, entities = self.parser.parse(text, parse_mode).values()
r = self.send(
functions.messages.SendMessage(

View File

@ -31,7 +31,7 @@ class SendPhoto(BaseClient):
chat_id: Union[int, str],
photo: str,
caption: str = "",
parse_mode: str = "",
parse_mode: Union[str, None] = "",
ttl_seconds: int = None,
disable_notification: bool = None,
reply_to_message_id: int = None,
@ -112,7 +112,6 @@ class SendPhoto(BaseClient):
RPCError: In case of a Telegram RPC error.
"""
file = None
style = self.html if parse_mode.lower() == "html" else self.markdown
try:
if os.path.exists(photo):
@ -139,7 +138,7 @@ class SendPhoto(BaseClient):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
reply_markup=reply_markup.write() if reply_markup else None,
**style.parse(caption)
**self.parser.parse(caption, parse_mode)
)
)
except FilePartMissing as e:

View File

@ -31,7 +31,7 @@ class SendVideo(BaseClient):
chat_id: Union[int, str],
video: str,
caption: str = "",
parse_mode: str = "",
parse_mode: Union[str, None] = "",
duration: int = 0,
width: int = 0,
height: int = 0,
@ -129,7 +129,6 @@ class SendVideo(BaseClient):
RPCError: In case of a Telegram RPC error.
"""
file = None
style = self.html if parse_mode.lower() == "html" else self.markdown
try:
if os.path.exists(video):
@ -166,7 +165,7 @@ class SendVideo(BaseClient):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
reply_markup=reply_markup.write() if reply_markup else None,
**style.parse(caption)
**self.parser.parse(caption, parse_mode)
)
)
except FilePartMissing as e:

View File

@ -31,7 +31,7 @@ class SendVoice(BaseClient):
chat_id: Union[int, str],
voice: str,
caption: str = "",
parse_mode: str = "",
parse_mode: Union[str, None] = "",
duration: int = 0,
disable_notification: bool = None,
reply_to_message_id: int = None,
@ -110,7 +110,6 @@ class SendVoice(BaseClient):
RPCError: In case of a Telegram RPC error.
"""
file = None
style = self.html if parse_mode.lower() == "html" else self.markdown
try:
if os.path.exists(voice):
@ -142,7 +141,7 @@ class SendVoice(BaseClient):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
reply_markup=reply_markup.write() if reply_markup else None,
**style.parse(caption)
**self.parser.parse(caption, parse_mode)
)
)
except FilePartMissing as e:

View File

@ -16,9 +16,11 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Union
from pyrogram.api import types
from .input_message_content import InputMessageContent
from ...style import HTML, Markdown
from ...parser import Parser
class InputTextMessageContent(InputMessageContent):
@ -38,7 +40,7 @@ class InputTextMessageContent(InputMessageContent):
__slots__ = ["message_text", "parse_mode", "disable_web_page_preview"]
def __init__(self, message_text: str, parse_mode: str = "", disable_web_page_preview: bool = None):
def __init__(self, message_text: str, parse_mode: Union[str, None] = "", disable_web_page_preview: bool = None):
super().__init__()
self.message_text = message_text
@ -49,5 +51,5 @@ class InputTextMessageContent(InputMessageContent):
return types.InputBotInlineMessageText(
no_webpage=self.disable_web_page_preview or None,
reply_markup=reply_markup.write() if reply_markup else None,
**(HTML() if self.parse_mode.lower() == "html" else Markdown()).parse(self.message_text)
**(Parser(None)).parse(self.message_text, self.parse_mode)
)

View File

@ -31,7 +31,7 @@ from ..object import Object
from ..update import Update
from ..user_and_chats.chat import Chat
from ..user_and_chats.user import User
from ...style import utils, Markdown, HTML
from ...parser import utils, Parser
class Str(str):
@ -47,11 +47,11 @@ class Str(str):
@property
def markdown(self):
return Markdown.unparse(self, self.entities)
return Parser.unparse(self, self.entities, False)
@property
def html(self):
return HTML.unparse(self, self.entities)
return Parser.unparse(self, self.entities, True)
def __getitem__(self, item):
return utils.remove_surrogates(utils.add_surrogates(self)[item])