Skip to content

Commit f6361cd

Browse files
committed
Allow bare lists from the MTProto API to be printed nicely
1 parent e7f6e9e commit f6361cd

File tree

4 files changed

+53
-26
lines changed

4 files changed

+53
-26
lines changed

pyrogram/api/core/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .future_salt import FutureSalt
2020
from .future_salts import FutureSalts
2121
from .gzip_packed import GzipPacked
22+
from .list import List
2223
from .message import Message
2324
from .msg_container import MsgContainer
2425
from .object import Object

pyrogram/api/core/list.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from .object import Object
20+
21+
22+
class List(list, Object):
23+
__slots__ = []
24+
25+
def __repr__(self):
26+
return "pyrogram.api.core.List([{}])".format(",".join(Object.__str__(i) for i in self))

pyrogram/api/core/object.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from collections import OrderedDict
20-
from datetime import datetime
2120
from io import BytesIO
2221
from json import dumps
2322

@@ -36,32 +35,22 @@ def read(b: BytesIO, *args): # TODO: Rename b -> data
3635
def write(self, *args) -> bytes:
3736
pass
3837

39-
def __eq__(self, other: "Object") -> bool:
40-
for attr in self.__slots__:
41-
try:
42-
if getattr(self, attr) != getattr(other, attr):
43-
return False
44-
except AttributeError:
45-
return False
38+
@staticmethod
39+
def default(obj: "Object"):
40+
if isinstance(obj, bytes):
41+
return repr(obj)
4642

47-
return True
43+
return OrderedDict(
44+
[("_", obj.QUALNAME)]
45+
+ [
46+
(attr, getattr(obj, attr))
47+
for attr in obj.__slots__
48+
if getattr(obj, attr) is not None
49+
]
50+
)
4851

4952
def __str__(self) -> str:
50-
def default(obj: Object):
51-
try:
52-
return OrderedDict(
53-
[("_", obj.QUALNAME)]
54-
+ [(attr, getattr(obj, attr))
55-
for attr in obj.__slots__
56-
if getattr(obj, attr) is not None]
57-
)
58-
except AttributeError:
59-
if isinstance(obj, datetime):
60-
return obj.strftime("%d-%b-%Y %H:%M:%S")
61-
else:
62-
return repr(obj)
63-
64-
return dumps(self, indent=4, default=default, ensure_ascii=False)
53+
return dumps(self, indent=4, default=Object.default, ensure_ascii=False)
6554

6655
def __repr__(self) -> str:
6756
return "pyrogram.api.{}({})".format(
@@ -73,6 +62,16 @@ def __repr__(self) -> str:
7362
)
7463
)
7564

65+
def __eq__(self, other: "Object") -> bool:
66+
for attr in self.__slots__:
67+
try:
68+
if getattr(self, attr) != getattr(other, attr):
69+
return False
70+
except AttributeError:
71+
return False
72+
73+
return True
74+
7675
def __len__(self) -> int:
7776
return len(self.write())
7877

pyrogram/api/core/primitives/vector.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from io import BytesIO
2020

2121
from . import Int
22+
from ..list import List
2223
from ..object import Object
2324

2425

@@ -37,11 +38,11 @@ def _read(b: BytesIO) -> Object or int:
3738

3839
@staticmethod
3940
def read(b: BytesIO, t: Object = None) -> list:
40-
return [
41+
return List(
4142
t.read(b) if t
4243
else Vector._read(b)
4344
for _ in range(Int.read(b))
44-
]
45+
)
4546

4647
def __new__(cls, value: list, t: Object = None) -> bytes:
4748
return b"".join(

0 commit comments

Comments
 (0)