2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 21:07:59 +00:00

Make all parse methods protected to hide them from the lib user

This commit is contained in:
Dan 2018-12-19 13:00:33 +01:00
parent b79f395d37
commit e8fbae3166
26 changed files with 87 additions and 87 deletions

View File

@ -62,17 +62,17 @@ class Dispatcher:
self.update_parsers = {
Dispatcher.MESSAGE_UPDATES:
lambda upd, usr, cht: (pyrogram.Message.parse(self.client, upd.message, usr, cht), MessageHandler),
lambda upd, usr, cht: (pyrogram.Message._parse(self.client, upd.message, usr, cht), MessageHandler),
Dispatcher.DELETE_MESSAGES_UPDATES:
lambda upd, usr, cht: (pyrogram.Messages.parse_deleted(self.client, upd), DeletedMessagesHandler),
lambda upd, usr, cht: (pyrogram.Messages._parse_deleted(self.client, upd), DeletedMessagesHandler),
Dispatcher.CALLBACK_QUERY_UPDATES:
lambda upd, usr, cht: (pyrogram.CallbackQuery.parse(self.client, upd, usr), CallbackQueryHandler),
lambda upd, usr, cht: (pyrogram.CallbackQuery._parse(self.client, upd, usr), CallbackQueryHandler),
(types.UpdateUserStatus,):
lambda upd, usr, cht: (
pyrogram.UserStatus.parse(self.client, upd.status, upd.user_id), UserStatusHandler
pyrogram.UserStatus._parse(self.client, upd.status, upd.user_id), UserStatusHandler
)
}

View File

@ -77,7 +77,7 @@ class CallbackQuery(PyrogramType):
self.game_short_name = game_short_name
@staticmethod
def parse(client, callback_query, users) -> "CallbackQuery":
def _parse(client, callback_query, users) -> "CallbackQuery":
message = None
inline_message_id = None
@ -105,7 +105,7 @@ class CallbackQuery(PyrogramType):
return CallbackQuery(
id=str(callback_query.query_id),
from_user=User.parse(client, users[callback_query.user_id]),
from_user=User._parse(client, users[callback_query.user_id]),
message=message,
inline_message_id=inline_message_id,
chat_instance=str(callback_query.chat_instance),

View File

@ -81,7 +81,7 @@ class Animation(PyrogramType):
self.duration = duration
@staticmethod
def parse(client, animation: types.Document, video_attributes: types.DocumentAttributeVideo,
def _parse(client, animation: types.Document, video_attributes: types.DocumentAttributeVideo,
file_name: str) -> "Animation":
return Animation(
file_id=encode(
@ -96,7 +96,7 @@ class Animation(PyrogramType):
width=getattr(video_attributes, "w", 0),
height=getattr(video_attributes, "h", 0),
duration=getattr(video_attributes, "duration", 0),
thumb=PhotoSize.parse(client, animation.thumb),
thumb=PhotoSize._parse(client, animation.thumb),
mime_type=animation.mime_type,
file_size=animation.size,
file_name=file_name,

View File

@ -81,7 +81,7 @@ class Audio(PyrogramType):
self.title = title
@staticmethod
def parse(client, audio: types.Document, audio_attributes: types.DocumentAttributeAudio, file_name: str) -> "Audio":
def _parse(client, audio: types.Document, audio_attributes: types.DocumentAttributeAudio, file_name: str) -> "Audio":
return Audio(
file_id=encode(
pack(
@ -97,7 +97,7 @@ class Audio(PyrogramType):
title=audio_attributes.title,
mime_type=audio.mime_type,
file_size=audio.size,
thumb=PhotoSize.parse(client, audio.thumb),
thumb=PhotoSize._parse(client, audio.thumb),
file_name=file_name,
date=audio.date,
client=client

View File

@ -57,7 +57,7 @@ class Contact(PyrogramType):
self.vcard = vcard
@staticmethod
def parse(client, contact: types.MessageMediaContact) -> "Contact":
def _parse(client, contact: types.MessageMediaContact) -> "Contact":
return Contact(
phone_number=contact.phone_number,
first_name=contact.first_name,

View File

@ -66,7 +66,7 @@ class Document(PyrogramType):
self.date = date
@staticmethod
def parse(client, document: types.Document, file_name: str) -> "Document":
def _parse(client, document: types.Document, file_name: str) -> "Document":
return Document(
file_id=encode(
pack(
@ -77,7 +77,7 @@ class Document(PyrogramType):
document.access_hash
)
),
thumb=PhotoSize.parse(client, document.thumb),
thumb=PhotoSize._parse(client, document.thumb),
file_name=file_name,
mime_type=document.mime_type,
file_size=document.size,

View File

@ -38,7 +38,7 @@ class Location(PyrogramType):
self.latitude = latitude
@staticmethod
def parse(client, geo_point: types.GeoPoint) -> "Location":
def _parse(client, geo_point: types.GeoPoint) -> "Location":
if isinstance(geo_point, types.GeoPoint):
return Location(
longitude=geo_point.long,

View File

@ -336,7 +336,7 @@ class Message(PyrogramType):
self.reply_markup = reply_markup
@staticmethod
def parse(client, message: types.Message or types.MessageService or types.MessageEmpty, users: dict, chats: dict,
def _parse(client, message: types.Message or types.MessageService or types.MessageEmpty, users: dict, chats: dict,
replies: int = 1):
if isinstance(message, types.MessageEmpty):
return Message(message_id=message.id, empty=True, client=client)
@ -355,11 +355,11 @@ class Message(PyrogramType):
new_chat_photo = None
if isinstance(action, types.MessageActionChatAddUser):
new_chat_members = [User.parse(client, users[i]) for i in action.users]
new_chat_members = [User._parse(client, users[i]) for i in action.users]
elif isinstance(action, types.MessageActionChatJoinedByLink):
new_chat_members = [User.parse(client, users[message.from_id])]
new_chat_members = [User._parse(client, users[message.from_id])]
elif isinstance(action, types.MessageActionChatDeleteUser):
left_chat_member = User.parse(client, users[action.user_id])
left_chat_member = User._parse(client, users[action.user_id])
elif isinstance(action, types.MessageActionChatEditTitle):
new_chat_title = action.title
elif isinstance(action, types.MessageActionChatDeletePhoto):
@ -373,13 +373,13 @@ class Message(PyrogramType):
elif isinstance(action, types.MessageActionChannelCreate):
channel_chat_created = True
elif isinstance(action, types.MessageActionChatEditPhoto):
new_chat_photo = Photo.parse(client, action.photo)
new_chat_photo = Photo._parse(client, action.photo)
parsed_message = Message(
message_id=message.id,
date=message.date,
chat=Chat.parse(client, message, users, chats),
from_user=User.parse(client, users.get(message.from_id, None)),
chat=Chat._parse(client, message, users, chats),
from_user=User._parse(client, users.get(message.from_id, None)),
service=True,
new_chat_members=new_chat_members,
left_chat_member=left_chat_member,
@ -407,7 +407,7 @@ class Message(PyrogramType):
return parsed_message
if isinstance(message, types.Message):
entities = [MessageEntity.parse(client, entity, users) for entity in message.entities]
entities = [MessageEntity._parse(client, entity, users) for entity in message.entities]
entities = list(filter(lambda x: x is not None, entities))
forward_from = None
@ -422,9 +422,9 @@ class Message(PyrogramType):
forward_date = forward_header.date
if forward_header.from_id:
forward_from = User.parse(client, users[forward_header.from_id])
forward_from = User._parse(client, users[forward_header.from_id])
else:
forward_from_chat = Chat.parse_channel_chat(client, chats[forward_header.channel_id])
forward_from_chat = Chat._parse_channel_chat(client, chats[forward_header.channel_id])
forward_from_message_id = forward_header.channel_post
forward_signature = forward_header.post_author
@ -445,13 +445,13 @@ class Message(PyrogramType):
if media:
if isinstance(media, types.MessageMediaPhoto):
photo = Photo.parse(client, media.photo)
photo = Photo._parse(client, media.photo)
elif isinstance(media, types.MessageMediaGeo):
location = Location.parse(client, media.geo)
location = Location._parse(client, media.geo)
elif isinstance(media, types.MessageMediaContact):
contact = Contact.parse(client, media)
contact = Contact._parse(client, media)
elif isinstance(media, types.MessageMediaVenue):
venue = pyrogram.Venue.parse(client, media)
venue = pyrogram.Venue._parse(client, media)
elif isinstance(media, types.MessageMediaDocument):
doc = media.document
@ -468,20 +468,20 @@ class Message(PyrogramType):
audio_attributes = attributes[types.DocumentAttributeAudio]
if audio_attributes.voice:
voice = pyrogram.Voice.parse(client, doc, audio_attributes)
voice = pyrogram.Voice._parse(client, doc, audio_attributes)
else:
audio = pyrogram.Audio.parse(client, doc, audio_attributes, file_name)
audio = pyrogram.Audio._parse(client, doc, audio_attributes, file_name)
elif types.DocumentAttributeAnimated in attributes:
video_attributes = attributes.get(types.DocumentAttributeVideo, None)
animation = pyrogram.Animation.parse(client, doc, video_attributes, file_name)
animation = pyrogram.Animation._parse(client, doc, video_attributes, file_name)
elif types.DocumentAttributeVideo in attributes:
video_attributes = attributes[types.DocumentAttributeVideo]
if video_attributes.round_message:
video_note = pyrogram.VideoNote.parse(client, doc, video_attributes)
video_note = pyrogram.VideoNote._parse(client, doc, video_attributes)
else:
video = pyrogram.Video.parse(client, doc, video_attributes, file_name)
video = pyrogram.Video._parse(client, doc, video_attributes, file_name)
elif types.DocumentAttributeSticker in attributes:
sticker = pyrogram.Sticker.parse(
client, doc,
@ -490,7 +490,7 @@ class Message(PyrogramType):
file_name
)
else:
document = pyrogram.Document.parse(client, doc, file_name)
document = pyrogram.Document._parse(client, doc, file_name)
elif isinstance(media, types.MessageMediaWebPage):
web_page = True
media = None
@ -514,8 +514,8 @@ class Message(PyrogramType):
parsed_message = Message(
message_id=message.id,
date=message.date,
chat=Chat.parse(client, message, users, chats),
from_user=User.parse(client, users.get(message.from_id, None)),
chat=Chat._parse(client, message, users, chats),
from_user=User._parse(client, users.get(message.from_id, None)),
text=Str(message.message).init(client, entities) or None if media is None else None,
caption=Str(message.message).init(client, entities) or None if media is not None else None,
entities=entities or None if media is None else None,
@ -543,7 +543,7 @@ class Message(PyrogramType):
document=document,
web_page=web_page,
views=message.views,
via_bot=User.parse(client, users.get(message.via_bot_id, None)),
via_bot=User._parse(client, users.get(message.via_bot_id, None)),
outgoing=message.out,
reply_markup=reply_markup,
client=client

View File

@ -78,7 +78,7 @@ class MessageEntity(PyrogramType):
self.user = user
@staticmethod
def parse(client, entity, users: dict) -> "MessageEntity" or None:
def _parse(client, entity, users: dict) -> "MessageEntity" or None:
type = MessageEntity.ENTITIES.get(entity.ID, None)
if type is None:
@ -89,6 +89,6 @@ class MessageEntity(PyrogramType):
offset=entity.offset,
length=entity.length,
url=getattr(entity, "url", None),
user=User.parse(client, users.get(getattr(entity, "user_id", None), None)),
user=User._parse(client, users.get(getattr(entity, "user_id", None), None)),
client=client
)

View File

@ -44,18 +44,18 @@ class Messages(PyrogramType):
self.messages = messages
@staticmethod
def parse(client, messages: types.messages.Messages) -> "Messages":
def _parse(client, messages: types.messages.Messages) -> "Messages":
users = {i.id: i for i in messages.users}
chats = {i.id: i for i in messages.chats}
return Messages(
total_count=getattr(messages, "count", len(messages.messages)),
messages=[Message.parse(client, message, users, chats) for message in messages.messages],
messages=[Message._parse(client, message, users, chats) for message in messages.messages],
client=client
)
@staticmethod
def parse_deleted(client, update) -> "Messages":
def _parse_deleted(client, update) -> "Messages":
messages = update.messages
channel_id = getattr(update, "channel_id", None)

View File

@ -52,7 +52,7 @@ class Photo(PyrogramType):
self.sizes = sizes
@staticmethod
def parse(client, photo: types.Photo):
def _parse(client, photo: types.Photo):
if isinstance(photo, types.Photo):
raw_sizes = photo.sizes
sizes = []

View File

@ -55,7 +55,7 @@ class PhotoSize(PyrogramType):
self.file_size = file_size
@staticmethod
def parse(client, photo_size: types.PhotoSize or types.PhotoCachedSize):
def _parse(client, photo_size: types.PhotoSize or types.PhotoCachedSize):
if isinstance(photo_size, (types.PhotoSize, types.PhotoCachedSize)):
if isinstance(photo_size, types.PhotoSize):

View File

@ -104,7 +104,7 @@ class Sticker(PyrogramType):
return None
@staticmethod
def parse(client, sticker: types.Document, image_size_attributes: types.DocumentAttributeImageSize,
def _parse(client, sticker: types.Document, image_size_attributes: types.DocumentAttributeImageSize,
sticker_attributes: types.DocumentAttributeSticker, file_name: str) -> "Sticker":
sticker_set = sticker_attributes.stickerset
@ -126,7 +126,7 @@ class Sticker(PyrogramType):
),
width=image_size_attributes.w if image_size_attributes else 0,
height=image_size_attributes.h if image_size_attributes else 0,
thumb=PhotoSize.parse(client, sticker.thumb),
thumb=PhotoSize._parse(client, sticker.thumb),
# TODO: mask_position
set_name=set_name,
emoji=sticker_attributes.alt or None,

View File

@ -42,9 +42,9 @@ class UserProfilePhotos(PyrogramType):
self.photos = photos
@staticmethod
def parse(client, photos) -> "UserProfilePhotos":
def _parse(client, photos) -> "UserProfilePhotos":
return UserProfilePhotos(
total_count=getattr(photos, "count", len(photos.photos)),
photos=[Photo.parse(client, photo) for photo in photos.photos],
photos=[Photo._parse(client, photo) for photo in photos.photos],
client=client
)

View File

@ -60,9 +60,9 @@ class Venue(PyrogramType):
self.foursquare_type = foursquare_type
@staticmethod
def parse(client, venue: types.MessageMediaVenue):
def _parse(client, venue: types.MessageMediaVenue):
return Venue(
location=Location.parse(client, venue.geo),
location=Location._parse(client, venue.geo),
title=venue.title,
address=venue.address,
foursquare_id=venue.venue_id or None,

View File

@ -81,7 +81,7 @@ class Video(PyrogramType):
self.duration = duration
@staticmethod
def parse(client, video: types.Document, video_attributes: types.DocumentAttributeVideo, file_name: str) -> "Video":
def _parse(client, video: types.Document, video_attributes: types.DocumentAttributeVideo, file_name: str) -> "Video":
return Video(
file_id=encode(
pack(
@ -95,7 +95,7 @@ class Video(PyrogramType):
width=video_attributes.w,
height=video_attributes.h,
duration=video_attributes.duration,
thumb=PhotoSize.parse(client, video.thumb),
thumb=PhotoSize._parse(client, video.thumb),
mime_type=video.mime_type,
file_size=video.size,
file_name=file_name,

View File

@ -71,7 +71,7 @@ class VideoNote(PyrogramType):
self.duration = duration
@staticmethod
def parse(client, video_note: types.Document, video_attributes: types.DocumentAttributeVideo) -> "VideoNote":
def _parse(client, video_note: types.Document, video_attributes: types.DocumentAttributeVideo) -> "VideoNote":
return VideoNote(
file_id=encode(
pack(
@ -84,7 +84,7 @@ class VideoNote(PyrogramType):
),
length=video_attributes.w,
duration=video_attributes.duration,
thumb=PhotoSize.parse(client, video_note.thumb),
thumb=PhotoSize._parse(client, video_note.thumb),
file_size=video_note.size,
mime_type=video_note.mime_type,
date=video_note.date,

View File

@ -65,7 +65,7 @@ class Voice(PyrogramType):
self.date = date
@staticmethod
def parse(client, voice: types.Document, attributes: types.DocumentAttributeAudio) -> "Voice":
def _parse(client, voice: types.Document, attributes: types.DocumentAttributeAudio) -> "Voice":
return Voice(
file_id=encode(
pack(

View File

@ -113,20 +113,20 @@ class Chat(PyrogramType):
self.restriction_reason = restriction_reason
@staticmethod
def parse_user_chat(client, user: types.User) -> "Chat":
def _parse_user_chat(client, user: types.User) -> "Chat":
return Chat(
id=user.id,
type="private",
username=user.username,
first_name=user.first_name,
last_name=user.last_name,
photo=ChatPhoto.parse(client, user.photo),
photo=ChatPhoto._parse(client, user.photo),
restriction_reason=user.restriction_reason,
client=client
)
@staticmethod
def parse_chat_chat(client, chat: types.Chat) -> "Chat":
def _parse_chat_chat(client, chat: types.Chat) -> "Chat":
admins_enabled = getattr(chat, "admins_enabled", None)
if admins_enabled is not None:
@ -137,45 +137,45 @@ class Chat(PyrogramType):
type="group",
title=chat.title,
all_members_are_administrators=admins_enabled,
photo=ChatPhoto.parse(client, getattr(chat, "photo", None)),
photo=ChatPhoto._parse(client, getattr(chat, "photo", None)),
client=client
)
@staticmethod
def parse_channel_chat(client, channel: types.Channel) -> "Chat":
def _parse_channel_chat(client, channel: types.Channel) -> "Chat":
return Chat(
id=int("-100" + str(channel.id)),
type="supergroup" if channel.megagroup else "channel",
title=channel.title,
username=getattr(channel, "username", None),
photo=ChatPhoto.parse(client, getattr(channel, "photo", None)),
photo=ChatPhoto._parse(client, getattr(channel, "photo", None)),
restriction_reason=getattr(channel, "restriction_reason", None),
client=client
)
@staticmethod
def parse(client, message: types.Message or types.MessageService, users: dict, chats: dict) -> "Chat":
def _parse(client, message: types.Message or types.MessageService, users: dict, chats: dict) -> "Chat":
if isinstance(message.to_id, types.PeerUser):
return Chat.parse_user_chat(client, users[message.to_id.user_id if message.out else message.from_id])
return Chat._parse_user_chat(client, users[message.to_id.user_id if message.out else message.from_id])
if isinstance(message.to_id, types.PeerChat):
return Chat.parse_chat_chat(client, chats[message.to_id.chat_id])
return Chat._parse_chat_chat(client, chats[message.to_id.chat_id])
return Chat.parse_channel_chat(client, chats[message.to_id.channel_id])
return Chat._parse_channel_chat(client, chats[message.to_id.channel_id])
@staticmethod
def parse_dialog(client, peer, users: dict, chats: dict):
def _parse_dialog(client, peer, users: dict, chats: dict):
if isinstance(peer, types.PeerUser):
return Chat.parse_user_chat(client, users[peer.user_id])
return Chat._parse_user_chat(client, users[peer.user_id])
elif isinstance(peer, types.PeerChat):
return Chat.parse_chat_chat(client, chats[peer.chat_id])
return Chat._parse_chat_chat(client, chats[peer.chat_id])
else:
return Chat.parse_channel_chat(client, chats[peer.channel_id])
return Chat._parse_channel_chat(client, chats[peer.channel_id])
@staticmethod
def parse_full(client, chat_full: types.messages.ChatFull or types.UserFull) -> "Chat":
def _parse_full(client, chat_full: types.messages.ChatFull or types.UserFull) -> "Chat":
if isinstance(chat_full, types.UserFull):
parsed_chat = Chat.parse_user_chat(client, chat_full.user)
_parsed_chat = Chat.parse_user_chat(client, chat_full.user)
parsed_chat.description = chat_full.about
else:
full_chat = chat_full.full_chat
@ -186,12 +186,12 @@ class Chat(PyrogramType):
chat = i
if isinstance(full_chat, types.ChatFull):
parsed_chat = Chat.parse_chat_chat(client, chat)
_parsed_chat = Chat.parse_chat_chat(client, chat)
if isinstance(full_chat.participants, types.ChatParticipants):
parsed_chat.members_count = len(full_chat.participants.participants)
else:
parsed_chat = Chat.parse_channel_chat(client, chat)
_parsed_chat = Chat.parse_channel_chat(client, chat)
parsed_chat.members_count = full_chat.participants_count
parsed_chat.description = full_chat.about or None
# TODO: Add StickerSet type

View File

@ -118,7 +118,7 @@ class ChatMember(PyrogramType):
self.can_add_web_page_previews = can_add_web_page_previews
@staticmethod
def parse(client, member, user) -> "ChatMember":
def _parse(client, member, user) -> "ChatMember":
if isinstance(member, (types.ChannelParticipant, types.ChannelParticipantSelf, types.ChatParticipant)):
return ChatMember(user=user, status="member", client=client)

View File

@ -44,7 +44,7 @@ class ChatMembers(PyrogramType):
self.chat_members = chat_members
@staticmethod
def parse(client, members):
def _parse(client, members):
users = {i.id: i for i in members.users}
chat_members = []
@ -56,8 +56,8 @@ class ChatMembers(PyrogramType):
total_count = len(members)
for member in members:
user = User.parse(client, users[member.user_id])
chat_members.append(ChatMember.parse(client, member, user))
user = User._parse(client, users[member.user_id])
chat_members.append(ChatMember._parse(client, member, user))
return ChatMembers(
total_count=total_count,

View File

@ -45,7 +45,7 @@ class ChatPhoto(PyrogramType):
self.big_file_id = big_file_id
@staticmethod
def parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto):
def _parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto):
if not isinstance(chat_photo, (types.UserProfilePhoto, types.ChatPhoto)):
return None

View File

@ -63,7 +63,7 @@ class Dialog(PyrogramType):
self.is_pinned = is_pinned
@staticmethod
def parse(client, dialog, messages, users, chats) -> "Dialog":
def _parse(client, dialog, messages, users, chats) -> "Dialog":
chat_id = dialog.peer
if isinstance(chat_id, types.PeerUser):
@ -74,7 +74,7 @@ class Dialog(PyrogramType):
chat_id = int("-100" + str(chat_id.channel_id))
return Dialog(
chat=Chat.parse_dialog(client, dialog.peer, users, chats),
chat=Chat._parse_dialog(client, dialog.peer, users, chats),
top_message=messages.get(chat_id),
unread_messages_count=dialog.unread_count,
unread_mentions_count=dialog.unread_mentions_count,

View File

@ -44,7 +44,7 @@ class Dialogs(PyrogramType):
self.dialogs = dialogs
@staticmethod
def parse(client, dialogs) -> "Dialogs":
def _parse(client, dialogs) -> "Dialogs":
users = {i.id: i for i in dialogs.users}
chats = {i.id: i for i in dialogs.chats}
@ -63,10 +63,10 @@ class Dialogs(PyrogramType):
else:
chat_id = int("-100" + str(to_id.channel_id))
messages[chat_id] = Message.parse(client, message, users, chats)
messages[chat_id] = Message._parse(client, message, users, chats)
return Dialogs(
total_count=getattr(dialogs, "count", len(dialogs.dialogs)),
dialogs=[Dialog.parse(client, dialog, messages, users, chats) for dialog in dialogs.dialogs],
dialogs=[Dialog._parse(client, dialog, messages, users, chats) for dialog in dialogs.dialogs],
client=client
)

View File

@ -104,7 +104,7 @@ class User(PyrogramType):
self.restriction_reason = restriction_reason
@staticmethod
def parse(client, user: types.User) -> "User" or None:
def _parse(client, user: types.User) -> "User" or None:
if user is None:
return None
@ -117,11 +117,11 @@ class User(PyrogramType):
is_bot=user.bot,
first_name=user.first_name,
last_name=user.last_name,
status=UserStatus.parse(client, user.status, user.id, user.bot),
status=UserStatus._parse(client, user.status, user.id, user.bot),
username=user.username,
language_code=user.lang_code,
phone_number=user.phone,
photo=ChatPhoto.parse(client, user.photo),
photo=ChatPhoto._parse(client, user.photo),
restriction_reason=user.restriction_reason,
client=client
)

View File

@ -85,7 +85,7 @@ class UserStatus(PyrogramType):
self.long_time_ago = long_time_ago
@staticmethod
def parse(client, user_status, user_id: int, is_bot: bool = False):
def _parse(client, user_status, user_id: int, is_bot: bool = False):
if is_bot:
return None