mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-28 21:07:59 +00:00
Allow bare lists from the MTProto API to be printed nicely
This commit is contained in:
parent
e7f6e9ec66
commit
f6361cd20f
@ -19,6 +19,7 @@
|
|||||||
from .future_salt import FutureSalt
|
from .future_salt import FutureSalt
|
||||||
from .future_salts import FutureSalts
|
from .future_salts import FutureSalts
|
||||||
from .gzip_packed import GzipPacked
|
from .gzip_packed import GzipPacked
|
||||||
|
from .list import List
|
||||||
from .message import Message
|
from .message import Message
|
||||||
from .msg_container import MsgContainer
|
from .msg_container import MsgContainer
|
||||||
from .object import Object
|
from .object import Object
|
||||||
|
26
pyrogram/api/core/list.py
Normal file
26
pyrogram/api/core/list.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
||||||
|
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
|
||||||
|
#
|
||||||
|
# This file is part of Pyrogram.
|
||||||
|
#
|
||||||
|
# Pyrogram is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Lesser General Public License as published
|
||||||
|
# by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# Pyrogram is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Lesser General Public License for more details.
|
||||||
|
#
|
||||||
|
# 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 .object import Object
|
||||||
|
|
||||||
|
|
||||||
|
class List(list, Object):
|
||||||
|
__slots__ = []
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "pyrogram.api.core.List([{}])".format(",".join(Object.__str__(i) for i in self))
|
@ -17,7 +17,6 @@
|
|||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from datetime import datetime
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from json import dumps
|
from json import dumps
|
||||||
|
|
||||||
@ -36,32 +35,22 @@ class Object:
|
|||||||
def write(self, *args) -> bytes:
|
def write(self, *args) -> bytes:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __eq__(self, other: "Object") -> bool:
|
@staticmethod
|
||||||
for attr in self.__slots__:
|
def default(obj: "Object"):
|
||||||
try:
|
if isinstance(obj, bytes):
|
||||||
if getattr(self, attr) != getattr(other, attr):
|
return repr(obj)
|
||||||
return False
|
|
||||||
except AttributeError:
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
return OrderedDict(
|
||||||
|
[("_", obj.QUALNAME)]
|
||||||
|
+ [
|
||||||
|
(attr, getattr(obj, attr))
|
||||||
|
for attr in obj.__slots__
|
||||||
|
if getattr(obj, attr) is not None
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
def default(obj: Object):
|
return dumps(self, indent=4, default=Object.default, ensure_ascii=False)
|
||||||
try:
|
|
||||||
return OrderedDict(
|
|
||||||
[("_", obj.QUALNAME)]
|
|
||||||
+ [(attr, getattr(obj, attr))
|
|
||||||
for attr in obj.__slots__
|
|
||||||
if getattr(obj, attr) is not None]
|
|
||||||
)
|
|
||||||
except AttributeError:
|
|
||||||
if isinstance(obj, datetime):
|
|
||||||
return obj.strftime("%d-%b-%Y %H:%M:%S")
|
|
||||||
else:
|
|
||||||
return repr(obj)
|
|
||||||
|
|
||||||
return dumps(self, indent=4, default=default, ensure_ascii=False)
|
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return "pyrogram.api.{}({})".format(
|
return "pyrogram.api.{}({})".format(
|
||||||
@ -73,6 +62,16 @@ class Object:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __eq__(self, other: "Object") -> bool:
|
||||||
|
for attr in self.__slots__:
|
||||||
|
try:
|
||||||
|
if getattr(self, attr) != getattr(other, attr):
|
||||||
|
return False
|
||||||
|
except AttributeError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
def __len__(self) -> int:
|
def __len__(self) -> int:
|
||||||
return len(self.write())
|
return len(self.write())
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
from . import Int
|
from . import Int
|
||||||
|
from ..list import List
|
||||||
from ..object import Object
|
from ..object import Object
|
||||||
|
|
||||||
|
|
||||||
@ -37,11 +38,11 @@ class Vector(Object):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def read(b: BytesIO, t: Object = None) -> list:
|
def read(b: BytesIO, t: Object = None) -> list:
|
||||||
return [
|
return List(
|
||||||
t.read(b) if t
|
t.read(b) if t
|
||||||
else Vector._read(b)
|
else Vector._read(b)
|
||||||
for _ in range(Int.read(b))
|
for _ in range(Int.read(b))
|
||||||
]
|
)
|
||||||
|
|
||||||
def __new__(cls, value: list, t: Object = None) -> bytes:
|
def __new__(cls, value: list, t: Object = None) -> bytes:
|
||||||
return b"".join(
|
return b"".join(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user