Allow bare lists from the MTProto API to be printed nicely

This commit is contained in:
Dan 2019-06-01 13:17:04 +02:00
parent e7f6e9ec66
commit f6361cd20f
4 changed files with 53 additions and 26 deletions

View File

@ -19,6 +19,7 @@
from .future_salt import FutureSalt
from .future_salts import FutureSalts
from .gzip_packed import GzipPacked
from .list import List
from .message import Message
from .msg_container import MsgContainer
from .object import Object

26
pyrogram/api/core/list.py Normal file
View 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))

View File

@ -17,7 +17,6 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from collections import OrderedDict
from datetime import datetime
from io import BytesIO
from json import dumps
@ -36,32 +35,22 @@ class Object:
def write(self, *args) -> bytes:
pass
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
@staticmethod
def default(obj: "Object"):
if isinstance(obj, bytes):
return repr(obj)
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 default(obj: Object):
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)
return dumps(self, indent=4, default=Object.default, ensure_ascii=False)
def __repr__(self) -> str:
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:
return len(self.write())

View File

@ -19,6 +19,7 @@
from io import BytesIO
from . import Int
from ..list import List
from ..object import Object
@ -37,11 +38,11 @@ class Vector(Object):
@staticmethod
def read(b: BytesIO, t: Object = None) -> list:
return [
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:
return b"".join(