From d5517f4d5fb13b6a459315865ae3a756f59d2837 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 3 Jun 2019 14:19:50 +0200 Subject: [PATCH] Rename Object to TLObject --- compiler/api/compiler.py | 14 +++++++------- compiler/api/template/mtproto.txt | 2 +- pyrogram/api/__init__.py | 4 ++-- pyrogram/api/core/__init__.py | 2 +- pyrogram/api/core/future_salt.py | 4 ++-- pyrogram/api/core/future_salts.py | 4 ++-- pyrogram/api/core/gzip_packed.py | 8 ++++---- pyrogram/api/core/list.py | 6 +++--- pyrogram/api/core/message.py | 8 ++++---- pyrogram/api/core/msg_container.py | 4 ++-- pyrogram/api/core/primitives/bool.py | 6 +++--- pyrogram/api/core/primitives/bytes.py | 4 ++-- pyrogram/api/core/primitives/double.py | 4 ++-- pyrogram/api/core/primitives/int.py | 4 ++-- pyrogram/api/core/primitives/null.py | 4 ++-- pyrogram/api/core/primitives/vector.py | 12 ++++++------ pyrogram/api/core/{object.py => tl_object.py} | 13 ++++++++----- pyrogram/client/client.py | 4 ++-- pyrogram/session/auth.py | 10 +++++----- pyrogram/session/internals/msg_factory.py | 4 ++-- pyrogram/session/session.py | 6 +++--- 21 files changed, 65 insertions(+), 62 deletions(-) rename pyrogram/api/core/{object.py => tl_object.py} (86%) diff --git a/compiler/api/compiler.py b/compiler/api/compiler.py index 64e88c9d..0ecc9212 100644 --- a/compiler/api/compiler.py +++ b/compiler/api/compiler.py @@ -53,10 +53,10 @@ def get_docstring_arg_type(t: str, is_list: bool = False, is_pyrogram_type: bool return "``{}``".format(t.lower()) elif t == "true": return "``bool``" - elif t == "Object" or t == "X": - return "Any object from :obj:`pyrogram.api.types`" + elif t == "TLObject" or t == "X": + return "Any object from :obj:`~pyrogram.api.types`" elif t == "!X": - return "Any method from :obj:`pyrogram.api.functions`" + return "Any method from :obj:`~pyrogram.api.functions`" elif t.startswith("Vector"): return "List of " + get_docstring_arg_type(t.split("<", 1)[1][:-1], True, is_pyrogram_type) else: @@ -394,7 +394,7 @@ def start(): ) read_types += "\n " - read_types += "{} = Object.read(b{}) if flags & (1 << {}) else []\n ".format( + read_types += "{} = TLObject.read(b{}) if flags & (1 << {}) else []\n ".format( arg_name, ", {}".format(sub_type.title()) if sub_type in core_types else "", index ) else: @@ -403,7 +403,7 @@ def start(): write_types += "b.write(self.{}.write())\n ".format(arg_name) read_types += "\n " - read_types += "{} = Object.read(b) if flags & (1 << {}) else None\n ".format( + read_types += "{} = TLObject.read(b) if flags & (1 << {}) else None\n ".format( arg_name, index ) else: @@ -422,7 +422,7 @@ def start(): ) read_types += "\n " - read_types += "{} = Object.read(b{})\n ".format( + read_types += "{} = TLObject.read(b{})\n ".format( arg_name, ", {}".format(sub_type.title()) if sub_type in core_types else "" ) else: @@ -430,7 +430,7 @@ def start(): write_types += "b.write(self.{}.write())\n ".format(arg_name) read_types += "\n " - read_types += "{} = Object.read(b)\n ".format(arg_name) + read_types += "{} = TLObject.read(b)\n ".format(arg_name) if c.docs: description = c.docs.split("|")[0].split("ยง")[1] diff --git a/compiler/api/template/mtproto.txt b/compiler/api/template/mtproto.txt index c63525d6..d7d3c7b7 100644 --- a/compiler/api/template/mtproto.txt +++ b/compiler/api/template/mtproto.txt @@ -5,7 +5,7 @@ from io import BytesIO from pyrogram.api.core import * -class {class_name}(Object): +class {class_name}(TLObject): """{docstring_args} """ diff --git a/pyrogram/api/__init__.py b/pyrogram/api/__init__.py index e57f0661..8d7831ff 100644 --- a/pyrogram/api/__init__.py +++ b/pyrogram/api/__init__.py @@ -19,8 +19,8 @@ from importlib import import_module from .all import objects -from .core.object import Object +from .core.tl_object import TLObject for k, v in objects.items(): path, name = v.rsplit(".", 1) - Object.all[k] = getattr(import_module(path), name) + TLObject.all[k] = getattr(import_module(path), name) diff --git a/pyrogram/api/core/__init__.py b/pyrogram/api/core/__init__.py index bf596446..f27a26a5 100644 --- a/pyrogram/api/core/__init__.py +++ b/pyrogram/api/core/__init__.py @@ -22,7 +22,7 @@ from .gzip_packed import GzipPacked from .list import List from .message import Message from .msg_container import MsgContainer -from .object import Object +from .tl_object import TLObject from .primitives import ( Bool, BoolTrue, BoolFalse, Bytes, Double, Int, Long, Int128, Int256, Null, String, Vector diff --git a/pyrogram/api/core/future_salt.py b/pyrogram/api/core/future_salt.py index c4ccea3d..699a416f 100644 --- a/pyrogram/api/core/future_salt.py +++ b/pyrogram/api/core/future_salt.py @@ -18,11 +18,11 @@ from io import BytesIO -from .object import Object +from .tl_object import TLObject from .primitives import Int, Long -class FutureSalt(Object): +class FutureSalt(TLObject): ID = 0x0949d9dc __slots__ = ["valid_since", "valid_until", "salt"] diff --git a/pyrogram/api/core/future_salts.py b/pyrogram/api/core/future_salts.py index 91ee7b51..c749c569 100644 --- a/pyrogram/api/core/future_salts.py +++ b/pyrogram/api/core/future_salts.py @@ -19,11 +19,11 @@ from io import BytesIO from . import FutureSalt -from .object import Object +from .tl_object import TLObject from .primitives import Int, Long -class FutureSalts(Object): +class FutureSalts(TLObject): ID = 0xae500895 __slots__ = ["req_msg_id", "now", "salts"] diff --git a/pyrogram/api/core/gzip_packed.py b/pyrogram/api/core/gzip_packed.py index 135c36bf..4b212184 100644 --- a/pyrogram/api/core/gzip_packed.py +++ b/pyrogram/api/core/gzip_packed.py @@ -19,24 +19,24 @@ from gzip import compress, decompress from io import BytesIO -from .object import Object +from .tl_object import TLObject from .primitives import Int, Bytes -class GzipPacked(Object): +class GzipPacked(TLObject): ID = 0x3072cfa1 __slots__ = ["packed_data"] QUALNAME = "GzipPacked" - def __init__(self, packed_data: Object): + def __init__(self, packed_data: TLObject): self.packed_data = packed_data @staticmethod def read(b: BytesIO, *args) -> "GzipPacked": # Return the Object itself instead of a GzipPacked wrapping it - return Object.read( + return TLObject.read( BytesIO( decompress( Bytes.read(b) diff --git a/pyrogram/api/core/list.py b/pyrogram/api/core/list.py index 922ec64d..4b309a6d 100644 --- a/pyrogram/api/core/list.py +++ b/pyrogram/api/core/list.py @@ -16,13 +16,13 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from .object import Object +from .tl_object import TLObject -class List(list, Object): +class List(list, TLObject): __slots__ = [] def __repr__(self): return "pyrogram.api.core.List([{}])".format( - ",".join(Object.__repr__(i) for i in self) + ",".join(TLObject.__repr__(i) for i in self) ) diff --git a/pyrogram/api/core/message.py b/pyrogram/api/core/message.py index 5b2e5b64..db123db3 100644 --- a/pyrogram/api/core/message.py +++ b/pyrogram/api/core/message.py @@ -18,18 +18,18 @@ from io import BytesIO -from .object import Object +from .tl_object import TLObject from .primitives import Int, Long -class Message(Object): +class Message(TLObject): ID = 0x5bb8e511 # hex(crc32(b"message msg_id:long seqno:int bytes:int body:Object = Message")) __slots__ = ["msg_id", "seq_no", "length", "body"] QUALNAME = "Message" - def __init__(self, body: Object, msg_id: int, seq_no: int, length: int): + def __init__(self, body: TLObject, msg_id: int, seq_no: int, length: int): self.msg_id = msg_id self.seq_no = seq_no self.length = length @@ -42,7 +42,7 @@ class Message(Object): length = Int.read(b) body = b.read(length) - return Message(Object.read(BytesIO(body)), msg_id, seq_no, length) + return Message(TLObject.read(BytesIO(body)), msg_id, seq_no, length) def write(self) -> bytes: b = BytesIO() diff --git a/pyrogram/api/core/msg_container.py b/pyrogram/api/core/msg_container.py index bfc41333..7a17087e 100644 --- a/pyrogram/api/core/msg_container.py +++ b/pyrogram/api/core/msg_container.py @@ -19,11 +19,11 @@ from io import BytesIO from .message import Message -from .object import Object +from .tl_object import TLObject from .primitives import Int -class MsgContainer(Object): +class MsgContainer(TLObject): ID = 0x73f1f8dc __slots__ = ["messages"] diff --git a/pyrogram/api/core/primitives/bool.py b/pyrogram/api/core/primitives/bool.py index 117ee7a4..0d3732e0 100644 --- a/pyrogram/api/core/primitives/bool.py +++ b/pyrogram/api/core/primitives/bool.py @@ -18,10 +18,10 @@ from io import BytesIO -from ..object import Object +from ..tl_object import TLObject -class BoolFalse(Object): +class BoolFalse(TLObject): ID = 0xbc799737 value = False @@ -38,7 +38,7 @@ class BoolTrue(BoolFalse): value = True -class Bool(Object): +class Bool(TLObject): @classmethod def read(cls, b: BytesIO) -> bool: return int.from_bytes(b.read(4), "little") == BoolTrue.ID diff --git a/pyrogram/api/core/primitives/bytes.py b/pyrogram/api/core/primitives/bytes.py index 8030b598..f511fef3 100644 --- a/pyrogram/api/core/primitives/bytes.py +++ b/pyrogram/api/core/primitives/bytes.py @@ -18,10 +18,10 @@ from io import BytesIO -from ..object import Object +from ..tl_object import TLObject -class Bytes(Object): +class Bytes(TLObject): @staticmethod def read(b: BytesIO, *args) -> bytes: length = int.from_bytes(b.read(1), "little") diff --git a/pyrogram/api/core/primitives/double.py b/pyrogram/api/core/primitives/double.py index 3dcaa461..067d08bd 100644 --- a/pyrogram/api/core/primitives/double.py +++ b/pyrogram/api/core/primitives/double.py @@ -19,10 +19,10 @@ from io import BytesIO from struct import unpack, pack -from ..object import Object +from ..tl_object import TLObject -class Double(Object): +class Double(TLObject): @staticmethod def read(b: BytesIO, *args) -> float: return unpack("d", b.read(8))[0] diff --git a/pyrogram/api/core/primitives/int.py b/pyrogram/api/core/primitives/int.py index 7833a610..ea43983c 100644 --- a/pyrogram/api/core/primitives/int.py +++ b/pyrogram/api/core/primitives/int.py @@ -18,10 +18,10 @@ from io import BytesIO -from ..object import Object +from ..tl_object import TLObject -class Int(Object): +class Int(TLObject): SIZE = 4 @classmethod diff --git a/pyrogram/api/core/primitives/null.py b/pyrogram/api/core/primitives/null.py index d2d3b1c0..ffddea94 100644 --- a/pyrogram/api/core/primitives/null.py +++ b/pyrogram/api/core/primitives/null.py @@ -18,10 +18,10 @@ from io import BytesIO -from ..object import Object +from ..tl_object import TLObject -class Null(Object): +class Null(TLObject): ID = 0x56730bcc @staticmethod diff --git a/pyrogram/api/core/primitives/vector.py b/pyrogram/api/core/primitives/vector.py index 720486f2..641b33ef 100644 --- a/pyrogram/api/core/primitives/vector.py +++ b/pyrogram/api/core/primitives/vector.py @@ -20,31 +20,31 @@ from io import BytesIO from . import Int from ..list import List -from ..object import Object +from ..tl_object import TLObject -class Vector(Object): +class Vector(TLObject): ID = 0x1cb5c415 # Method added to handle the special case when a query returns a bare Vector (of Ints); # i.e., RpcResult body starts with 0x1cb5c415 (Vector Id) - e.g., messages.GetMessagesViews. @staticmethod - def _read(b: BytesIO) -> Object or int: + def _read(b: BytesIO) -> TLObject or int: try: - return Object.read(b) + return TLObject.read(b) except KeyError: b.seek(-4, 1) return Int.read(b) @staticmethod - def read(b: BytesIO, t: Object = None) -> list: + def read(b: BytesIO, t: TLObject = None) -> list: return List( t.read(b) if t else Vector._read(b) for _ in range(Int.read(b)) ) - def __new__(cls, value: list, t: Object = None) -> bytes: + def __new__(cls, value: list, t: TLObject = None) -> bytes: return b"".join( [Int(cls.ID, False), Int(len(value))] + [ diff --git a/pyrogram/api/core/object.py b/pyrogram/api/core/tl_object.py similarity index 86% rename from pyrogram/api/core/object.py rename to pyrogram/api/core/tl_object.py index 9d1a4852..4b951404 100644 --- a/pyrogram/api/core/object.py +++ b/pyrogram/api/core/tl_object.py @@ -21,7 +21,7 @@ from io import BytesIO from json import dumps -class Object: +class TLObject: all = {} __slots__ = [] @@ -30,13 +30,13 @@ class Object: @staticmethod def read(b: BytesIO, *args): # TODO: Rename b -> data - return Object.all[int.from_bytes(b.read(4), "little")].read(b, *args) + return TLObject.all[int.from_bytes(b.read(4), "little")].read(b, *args) def write(self, *args) -> bytes: pass @staticmethod - def default(obj: "Object"): + def default(obj: "TLObject"): if isinstance(obj, bytes): return repr(obj) @@ -50,7 +50,7 @@ class Object: ) def __str__(self) -> str: - return dumps(self, indent=4, default=Object.default, ensure_ascii=False) + return dumps(self, indent=4, default=TLObject.default, ensure_ascii=False) def __repr__(self) -> str: return "pyrogram.api.{}({})".format( @@ -62,7 +62,7 @@ class Object: ) ) - def __eq__(self, other: "Object") -> bool: + def __eq__(self, other: "TLObject") -> bool: for attr in self.__slots__: try: if getattr(self, attr) != getattr(other, attr): @@ -77,3 +77,6 @@ class Object: def __getitem__(self, item): return getattr(self, item) + + def __setitem__(self, key, value): + setattr(self, key, value) diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 2ba6b8fa..08a3072e 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -37,7 +37,7 @@ from threading import Thread from typing import Union, List from pyrogram.api import functions, types -from pyrogram.api.core import Object +from pyrogram.api.core import TLObject from pyrogram.client.handlers import DisconnectHandler from pyrogram.client.handlers.handler import Handler from pyrogram.client.methods.password.utils import compute_check @@ -998,7 +998,7 @@ class Client(Methods, BaseClient): log.debug("{} stopped".format(name)) - def send(self, data: Object, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT): + def send(self, data: TLObject, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT): """Send raw Telegram queries. This method makes it possible to manually call every single Telegram API method in a low-level manner. diff --git a/pyrogram/session/auth.py b/pyrogram/session/auth.py index 89e5b61f..fb6e7ca3 100644 --- a/pyrogram/session/auth.py +++ b/pyrogram/session/auth.py @@ -23,7 +23,7 @@ from io import BytesIO from os import urandom from pyrogram.api import functions, types -from pyrogram.api.core import Object, Long, Int +from pyrogram.api.core import TLObject, Long, Int from pyrogram.connection import Connection from pyrogram.crypto import AES, RSA, Prime from .internals import MsgId @@ -43,7 +43,7 @@ class Auth: self.connection = None @staticmethod - def pack(data: Object) -> bytes: + def pack(data: TLObject) -> bytes: return ( bytes(8) + Long(MsgId()) @@ -54,9 +54,9 @@ class Auth: @staticmethod def unpack(b: BytesIO): b.seek(20) # Skip auth_key_id (8), message_id (8) and message_length (4) - return Object.read(b) + return TLObject.read(b) - def send(self, data: Object): + def send(self, data: TLObject): data = self.pack(data) self.connection.send(data) response = BytesIO(self.connection.recv()) @@ -158,7 +158,7 @@ class Auth: answer_with_hash = AES.ige256_decrypt(encrypted_answer, tmp_aes_key, tmp_aes_iv) answer = answer_with_hash[20:] - server_dh_inner_data = Object.read(BytesIO(answer)) + server_dh_inner_data = TLObject.read(BytesIO(answer)) log.debug("Done decrypting answer") diff --git a/pyrogram/session/internals/msg_factory.py b/pyrogram/session/internals/msg_factory.py index 7d922ec3..2b833ce8 100644 --- a/pyrogram/session/internals/msg_factory.py +++ b/pyrogram/session/internals/msg_factory.py @@ -16,7 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -from pyrogram.api.core import Message, MsgContainer, Object +from pyrogram.api.core import Message, MsgContainer, TLObject from pyrogram.api.functions import Ping from pyrogram.api.types import MsgsAck, HttpWait from .msg_id import MsgId @@ -29,7 +29,7 @@ class MsgFactory: def __init__(self): self.seq_no = SeqNo() - def __call__(self, body: Object) -> Message: + def __call__(self, body: TLObject) -> Message: return Message( body, MsgId(), diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index e4260be4..8ef5570c 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -30,7 +30,7 @@ import pyrogram from pyrogram import __copyright__, __license__, __version__ from pyrogram.api import functions, types, core from pyrogram.api.all import layer -from pyrogram.api.core import Message, Object, MsgContainer, Long, FutureSalt, Int +from pyrogram.api.core import Message, TLObject, MsgContainer, Long, FutureSalt, Int from pyrogram.connection import Connection from pyrogram.crypto import AES, KDF from pyrogram.errors import RPCError, InternalServerError, AuthKeyDuplicated @@ -391,7 +391,7 @@ class Session: log.debug("RecvThread stopped") - def _send(self, data: Object, wait_response: bool = True, timeout: float = WAIT_TIMEOUT): + def _send(self, data: TLObject, wait_response: bool = True, timeout: float = WAIT_TIMEOUT): message = self.msg_factory(data) msg_id = message.msg_id @@ -422,7 +422,7 @@ class Session: else: return result - def send(self, data: Object, retries: int = MAX_RETRIES, timeout: float = WAIT_TIMEOUT): + def send(self, data: TLObject, retries: int = MAX_RETRIES, timeout: float = WAIT_TIMEOUT): self.is_connected.wait(self.WAIT_TIMEOUT) try: