Skip to content

Commit

Permalink
Add __repr__ to all classes. Closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
NotStatilko committed Jan 21, 2024
1 parent 3776b94 commit 9e64548
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 3 deletions.
9 changes: 9 additions & 0 deletions tgbox/api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def __init__(self, aiosql_conn, table_name: str):
self._table_name = table_name
self._aiosql_conn = aiosql_conn

def __repr__(self) -> str:
return f'<class {self.__class__.__name__}(aiosql_conn, "{self._table_name}")>'

async def __aiter__(self) -> tuple:
"""Will yield rows as self.select without ``sql_statement``"""
async for row in self.select():
Expand Down Expand Up @@ -163,6 +166,12 @@ def __init__(self, db_path: Union[PathLike, str]):

self._name = self._db_path.name

def __str__(self) -> str:
return f'{self.__class__.__name__}("{str(self._db_path)}") # {self._initialized=}'

def __repr__(self) -> str:
return f'{self.__class__.__name__}("{str(self._db_path)}")'

@property
def name(self) -> str:
"""Returns TgboxDB name"""
Expand Down
18 changes: 18 additions & 0 deletions tgbox/api/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@ def __init__(
self._initialized = False
self._is_encrypted = True

def __repr__(self) -> str:
return f'{self.__class__.__name__}({repr(self._tgbox_db)}, {repr(self._defaults)})'

def __str__(self) -> str:
box_salt = None if not self._initialized else urlsafe_b64encode(self.box_salt.salt).decode()
return (
f'''{self.__class__.__name__}({repr(self._tgbox_db)}, {repr(self._defaults)}) '''
f'''# {self._initialized=}, {self._box_channel_id=}, {box_salt=}'''
)
def __hash__(self) -> int:
if not self._initialized:
raise NotInitializedError(
Expand Down Expand Up @@ -2318,6 +2327,15 @@ def __init__(
self._secret_metadata, self._minor_version = None, None
self._efile_path = None

def __repr__(self) -> str:
return (f'{self.__class__.__name__}({self._id}, {repr(self._lb)}, {self._cache_preview})')

def __str__(self) -> str:
file_salt = None if not self._initialized else urlsafe_b64encode(self._file_salt.salt).decode()
return (
f'''{self.__class__.__name__}({self._id}, {repr(self._lb)}, {self._cache_preview}) # '''
f'''{self._initialized=}, {file_salt=}'''
)
def __hash__(self) -> int:
return hash((self._id, 22))

Expand Down
21 changes: 21 additions & 0 deletions tgbox/api/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ def __init__(self,
DOWNLOAD_PATH = DOWNLOAD_PATH
)

def __repr__(self) -> str:
return f'<class {self.__class__.__name__}({self._box_channel}, {self._tc}, {repr(self._defaults)})>'

def __str__(self) -> str:
box_salt = None if not self._box_salt else urlsafe_b64encode(self._box_salt.salt).decode()
return (
f'''<class {self.__class__.__name__}({self._box_channel}, {self._tc}, {repr(self._defaults)})> '''
f'''# {self._box_name=}, {box_salt=}'''
)
def __hash__(self) -> int:
# Without 22 hash of int wil be equal to object's
return hash((self._box_channel_id, 22))
Expand Down Expand Up @@ -1402,6 +1411,18 @@ def __init__(
logger.debug('ERBF: Found custom defaults, will try to use it')
self._defaults = defaults

def __repr__(self) -> str:
return (
f'''{self.__class__.__name__}({self._id}, {repr(self._rb)}, '''
f'''{self._message}, {self._cache_preview}, {repr(self._defaults)})'''
)
def __str__(self) -> str:
file_salt = None if not self._initialized else urlsafe_b64encode(self._file_salt.salt).decode()
return (
f'''{self.__class__.__name__}({self._id}, {repr(self._rb)}, '''
f'''{self._message}, {self._cache_preview}, {repr(self._defaults)}) # '''
f'''{self._initialized=}, {file_salt=}, {self._sender=}, {self._imported=}'''
)
def __hash__(self) -> int:
if not self.initialized:
raise NotInitializedError(
Expand Down
11 changes: 11 additions & 0 deletions tgbox/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ def __init__(self, document: Union[Photo, Document], tc: TelegramClient):

self._downloader = None

def __repr__(self) -> str:
return (
f'''<class {self.__class__.__name__} @ '''
f'''{self.name=}, {self.size=}, {self.mime=}>'''
)
async def get_preview(self, quality: int=1) -> bytes:
if hasattr(self.document,'sizes')\
and not self.document.sizes:
Expand Down Expand Up @@ -552,6 +557,12 @@ def __init__(self, tgbox_db: TgboxDB):
self._tgbox_db = tgbox_db
self._initialized = False

def __repr__(self) -> str:
return (f'{self.__class__.__name__}({repr(self._tgbox_db)})')

def __str__(self) -> str:
return (f'{self.__class__.__name__}({repr(self._tgbox_db)}) # {self._initialized=}')

@property
def initialized(self) -> bool:
return self._initialized
Expand Down
17 changes: 14 additions & 3 deletions tgbox/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ def __init__(self, iv: Union[bytes, memoryview]):
self.iv = iv if isinstance(iv, bytes) else bytes(iv)

def __repr__(self) -> str:
class_name = self.__class__.__name__
return f'{class_name}({repr(self.iv)}) # at {hex(id(self))}'
return f'{self.__class__.__name__}({repr(self.iv)})'

def __str__(self) -> str:
return f'{self.__class__.__name__}({repr(self.iv)}) # at {hex(id(self))}'

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

def __repr__(self) -> str:
return f'{self.__class__.__name__}({repr(self.salt)})'

def __str__(self) -> str:
class_name = self.__class__.__name__
return f'{class_name}({repr(self.salt)}) # at {hex(id(self))}'
return f'{self.__class__.__name__}({repr(self.salt)}) # at {hex(id(self))}'

def __add__(self, other):
return self.salt + other
Expand Down Expand Up @@ -208,6 +213,12 @@ def __init__(
self.iv = IV(self.iv)
self.__iv_concated = False

def __repr__(self) -> str:
return f'<class {self.__class__.__name__}(<key>, {repr(self.iv)})>'

def __str__(self) -> str:
return f'<class {self.__class__.__name__}(<key>, {repr(self.iv)})> # {self.__mode=}'

def __init_aes_state(self, mode: int) -> None:
if FAST_ENCRYPTION:
self._aes_cbc = Cipher(algorithms.AES(self.key), modes.CBC(self.iv.iv))
Expand Down
10 changes: 10 additions & 0 deletions tgbox/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,16 @@ def __init__(

self._position = 0

def __repr__(self):
return (
f'''<class {self.__class__.__name__}({self._flo}, {repr(self._aes_state)}, '''
f'''{self._total_size})>'''
)
def __str__(self):
return (
f'''<class {self.__class__.__name__}({self._flo}, {repr(self._aes_state)}, '''
f'''{self._total_size})> # {self._position=}, {len(self._buffered_bytes)=}'''
)
def concat_metadata(self, metadata: bytes) -> None:
"""Concates metadata to the file as (metadata + file)."""
if self._position:
Expand Down

0 comments on commit 9e64548

Please sign in to comment.