Skip to content

Commit 9e64548

Browse files
committed
Add __repr__ to all classes. Closes #2
1 parent 3776b94 commit 9e64548

File tree

6 files changed

+83
-3
lines changed

6 files changed

+83
-3
lines changed

tgbox/api/db.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def __init__(self, aiosql_conn, table_name: str):
6262
self._table_name = table_name
6363
self._aiosql_conn = aiosql_conn
6464

65+
def __repr__(self) -> str:
66+
return f'<class {self.__class__.__name__}(aiosql_conn, "{self._table_name}")>'
67+
6568
async def __aiter__(self) -> tuple:
6669
"""Will yield rows as self.select without ``sql_statement``"""
6770
async for row in self.select():
@@ -163,6 +166,12 @@ def __init__(self, db_path: Union[PathLike, str]):
163166

164167
self._name = self._db_path.name
165168

169+
def __str__(self) -> str:
170+
return f'{self.__class__.__name__}("{str(self._db_path)}") # {self._initialized=}'
171+
172+
def __repr__(self) -> str:
173+
return f'{self.__class__.__name__}("{str(self._db_path)}")'
174+
166175
@property
167176
def name(self) -> str:
168177
"""Returns TgboxDB name"""

tgbox/api/local.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,15 @@ def __init__(
377377
self._initialized = False
378378
self._is_encrypted = True
379379

380+
def __repr__(self) -> str:
381+
return f'{self.__class__.__name__}({repr(self._tgbox_db)}, {repr(self._defaults)})'
382+
383+
def __str__(self) -> str:
384+
box_salt = None if not self._initialized else urlsafe_b64encode(self.box_salt.salt).decode()
385+
return (
386+
f'''{self.__class__.__name__}({repr(self._tgbox_db)}, {repr(self._defaults)}) '''
387+
f'''# {self._initialized=}, {self._box_channel_id=}, {box_salt=}'''
388+
)
380389
def __hash__(self) -> int:
381390
if not self._initialized:
382391
raise NotInitializedError(
@@ -2318,6 +2327,15 @@ def __init__(
23182327
self._secret_metadata, self._minor_version = None, None
23192328
self._efile_path = None
23202329

2330+
def __repr__(self) -> str:
2331+
return (f'{self.__class__.__name__}({self._id}, {repr(self._lb)}, {self._cache_preview})')
2332+
2333+
def __str__(self) -> str:
2334+
file_salt = None if not self._initialized else urlsafe_b64encode(self._file_salt.salt).decode()
2335+
return (
2336+
f'''{self.__class__.__name__}({self._id}, {repr(self._lb)}, {self._cache_preview}) # '''
2337+
f'''{self._initialized=}, {file_salt=}'''
2338+
)
23212339
def __hash__(self) -> int:
23222340
return hash((self._id, 22))
23232341

tgbox/api/remote.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,15 @@ def __init__(self,
307307
DOWNLOAD_PATH = DOWNLOAD_PATH
308308
)
309309

310+
def __repr__(self) -> str:
311+
return f'<class {self.__class__.__name__}({self._box_channel}, {self._tc}, {repr(self._defaults)})>'
312+
313+
def __str__(self) -> str:
314+
box_salt = None if not self._box_salt else urlsafe_b64encode(self._box_salt.salt).decode()
315+
return (
316+
f'''<class {self.__class__.__name__}({self._box_channel}, {self._tc}, {repr(self._defaults)})> '''
317+
f'''# {self._box_name=}, {box_salt=}'''
318+
)
310319
def __hash__(self) -> int:
311320
# Without 22 hash of int wil be equal to object's
312321
return hash((self._box_channel_id, 22))
@@ -1402,6 +1411,18 @@ def __init__(
14021411
logger.debug('ERBF: Found custom defaults, will try to use it')
14031412
self._defaults = defaults
14041413

1414+
def __repr__(self) -> str:
1415+
return (
1416+
f'''{self.__class__.__name__}({self._id}, {repr(self._rb)}, '''
1417+
f'''{self._message}, {self._cache_preview}, {repr(self._defaults)})'''
1418+
)
1419+
def __str__(self) -> str:
1420+
file_salt = None if not self._initialized else urlsafe_b64encode(self._file_salt.salt).decode()
1421+
return (
1422+
f'''{self.__class__.__name__}({self._id}, {repr(self._rb)}, '''
1423+
f'''{self._message}, {self._cache_preview}, {repr(self._defaults)}) # '''
1424+
f'''{self._initialized=}, {file_salt=}, {self._sender=}, {self._imported=}'''
1425+
)
14051426
def __hash__(self) -> int:
14061427
if not self.initialized:
14071428
raise NotInitializedError(

tgbox/api/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ def __init__(self, document: Union[Photo, Document], tc: TelegramClient):
208208

209209
self._downloader = None
210210

211+
def __repr__(self) -> str:
212+
return (
213+
f'''<class {self.__class__.__name__} @ '''
214+
f'''{self.name=}, {self.size=}, {self.mime=}>'''
215+
)
211216
async def get_preview(self, quality: int=1) -> bytes:
212217
if hasattr(self.document,'sizes')\
213218
and not self.document.sizes:
@@ -552,6 +557,12 @@ def __init__(self, tgbox_db: TgboxDB):
552557
self._tgbox_db = tgbox_db
553558
self._initialized = False
554559

560+
def __repr__(self) -> str:
561+
return (f'{self.__class__.__name__}({repr(self._tgbox_db)})')
562+
563+
def __str__(self) -> str:
564+
return (f'{self.__class__.__name__}({repr(self._tgbox_db)}) # {self._initialized=}')
565+
555566
@property
556567
def initialized(self) -> bool:
557568
return self._initialized

tgbox/crypto.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ def __init__(self, iv: Union[bytes, memoryview]):
4545
self.iv = iv if isinstance(iv, bytes) else bytes(iv)
4646

4747
def __repr__(self) -> str:
48-
class_name = self.__class__.__name__
49-
return f'{class_name}({repr(self.iv)}) # at {hex(id(self))}'
48+
return f'{self.__class__.__name__}({repr(self.iv)})'
49+
50+
def __str__(self) -> str:
51+
return f'{self.__class__.__name__}({repr(self.iv)}) # at {hex(id(self))}'
5052

5153
def __add__(self, other):
5254
return self.iv + other
@@ -75,8 +77,11 @@ def __init__(self, salt: Union[bytes, memoryview]):
7577
self.salt = salt if isinstance(salt, bytes) else bytes(salt)
7678

7779
def __repr__(self) -> str:
80+
return f'{self.__class__.__name__}({repr(self.salt)})'
81+
82+
def __str__(self) -> str:
7883
class_name = self.__class__.__name__
79-
return f'{class_name}({repr(self.salt)}) # at {hex(id(self))}'
84+
return f'{self.__class__.__name__}({repr(self.salt)}) # at {hex(id(self))}'
8085

8186
def __add__(self, other):
8287
return self.salt + other
@@ -208,6 +213,12 @@ def __init__(
208213
self.iv = IV(self.iv)
209214
self.__iv_concated = False
210215

216+
def __repr__(self) -> str:
217+
return f'<class {self.__class__.__name__}(<key>, {repr(self.iv)})>'
218+
219+
def __str__(self) -> str:
220+
return f'<class {self.__class__.__name__}(<key>, {repr(self.iv)})> # {self.__mode=}'
221+
211222
def __init_aes_state(self, mode: int) -> None:
212223
if FAST_ENCRYPTION:
213224
self._aes_cbc = Cipher(algorithms.AES(self.key), modes.CBC(self.iv.iv))

tgbox/tools.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,16 @@ def __init__(
325325

326326
self._position = 0
327327

328+
def __repr__(self):
329+
return (
330+
f'''<class {self.__class__.__name__}({self._flo}, {repr(self._aes_state)}, '''
331+
f'''{self._total_size})>'''
332+
)
333+
def __str__(self):
334+
return (
335+
f'''<class {self.__class__.__name__}({self._flo}, {repr(self._aes_state)}, '''
336+
f'''{self._total_size})> # {self._position=}, {len(self._buffered_bytes)=}'''
337+
)
328338
def concat_metadata(self, metadata: bytes) -> None:
329339
"""Concates metadata to the file as (metadata + file)."""
330340
if self._position:

0 commit comments

Comments
 (0)