Compare commits

...

2 Commits

Author SHA1 Message Date
5d59adb7d2 Renamed encryption/other_accounts into encryption/accounts 2023-01-01 00:21:53 +03:00
281c4a262b _export2 no longer blocks event loop during decryption
Removed sorting in get_all_accounts
Removed decrypt_multiple function because it is no longer used
Now running decrytion of accounts in ProcessPoolExecutor
2023-01-01 00:18:57 +03:00
4 changed files with 21 additions and 22 deletions

View File

@ -1,5 +1,7 @@
import asyncio
import functools
import gc
from concurrent.futures import ProcessPoolExecutor
import telebot
from sqlalchemy.future import Engine
@ -305,7 +307,7 @@ async def _add_account5(
password=data["passwd"],
)
encrypted_account = encryption.other_accounts.encrypt(
encrypted_account = encryption.accounts.encrypt(
account,
text,
)
@ -391,7 +393,7 @@ async def _get_account3(
)
account = database.get.get_account_info(engine, mes.from_user.id, name)
account = encryption.other_accounts.decrypt(
account = encryption.accounts.decrypt(
account,
text,
)
@ -534,7 +536,18 @@ async def _export2(
)
accounts = database.get.get_all_accounts(engine, mes.from_user.id)
accounts = encryption.other_accounts.decrypt_multiple(accounts, text)
with ProcessPoolExecutor() as pool:
loop = asyncio.get_running_loop()
tasks = []
for account in accounts:
function = functools.partial(
encryption.accounts.decrypt,
account,
text,
)
tasks.append(loop.run_in_executor(pool, function))
accounts = await asyncio.gather(*tasks)
accounts.sort(key=lambda account: account.name)
json_io = accounts_to_json(accounts)
await bot.send_document(
mes.chat.id,
@ -638,7 +651,7 @@ async def _import3(
if not check_account(account):
failed.append(account.name)
continue
account = encryption.other_accounts.encrypt(
account = encryption.accounts.encrypt(
account,
text,
)

View File

@ -36,13 +36,9 @@ def get_accounts(
def get_all_accounts(engine: Engine, user_id: int) -> list[models.Account]:
"""Returns a list of accounts of a user"""
statement = (
sqlmodel.select(models.Account)
.where(
statement = sqlmodel.select(models.Account).where(
models.Account.user_id == user_id,
)
.order_by(models.Account.name)
)
with sqlmodel.Session(engine) as session:
result = session.exec(statement).fetchall()
return result

View File

@ -1,3 +1,3 @@
from . import master_pass, other_accounts
from . import accounts, master_pass
__all__ = ["master_pass", "other_accounts"]
__all__ = ["master_pass", "accounts"]

View File

@ -1,6 +1,5 @@
import base64
import os
from typing import Iterable, Iterator
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
@ -71,12 +70,3 @@ def decrypt(
login=login,
password=password,
)
def decrypt_multiple(
accounts: Iterable[Account], master_pass: str
) -> Iterator[DecryptedAccount]:
"""Decrypts an iterable of accounts using master_pass and
returns an Iterator of decrypted accounts"""
for account in accounts:
yield decrypt(account, master_pass)