small refactor

This commit is contained in:
Balakrishnan Balasubramanian 2020-08-07 11:42:57 -04:00
parent 089507c0ac
commit f92da6c8ef

View File

@ -220,22 +220,24 @@ async def process_transactions(mails_list: List[MailEntry]):
await Session.writer().drain() await Session.writer().drain()
async def transaction_stage(deleted_items_path: Path): def get_deleted_items(deleted_items_path: Path):
if deleted_items_path.exists(): if deleted_items_path.exists():
with deleted_items_path.open() as f: with deleted_items_path.open() as f:
deleted_items = set(f.read().splitlines()) return set(f.read().splitlines())
else: return set()
deleted_items = set()
mails_list = [entry for entry in get_mails_list(Session.mails_path / 'new') if entry.uid not in deleted_items]
def save_deleted_items(deleted_items_path: Path, deleted_items: Set):
with deleted_items_path.open(mode="w") as f:
f.writelines(f"{did}\n" for did in deleted_items)
async def transaction_stage(existing_deleted_items: Set):
mails_list = [entry for entry in get_mails_list(Session.mails_path / 'new') if
entry.uid not in existing_deleted_items]
new_deleted_items: Set = await process_transactions(mails_list) new_deleted_items: Set = await process_transactions(mails_list)
return deleted_items.union(new_deleted_items) return new_deleted_items
def delete_messages(delete_ids, deleted_items_path: Path):
with deleted_items_path.open(mode="w") as f:
f.writelines(f"{did}\n" for did in delete_ids)
async def new_session(stream_reader: asyncio.StreamReader, stream_writer: asyncio.StreamWriter): async def new_session(stream_reader: asyncio.StreamReader, stream_writer: asyncio.StreamWriter):
@ -250,10 +252,14 @@ async def new_session(stream_reader: asyncio.StreamReader, stream_writer: asynci
deleted_items_path = Session.mails_path / username deleted_items_path = Session.mails_path / username
logging.info(f"User:{username} logged in successfully") logging.info(f"User:{username} logged in successfully")
delete_ids = await transaction_stage(deleted_items_path) existing_deleted_items: Set = get_deleted_items(deleted_items_path)
logging.info(f"User:{username} completed transactions. Deleted:{delete_ids}")
new_deleted_items: Set = await transaction_stage(existing_deleted_items)
logging.info(f"User:{username} completed transactions. Deleted:{new_deleted_items}")
if new_deleted_items:
save_deleted_items(deleted_items_path, existing_deleted_items.union(new_deleted_items))
delete_messages(delete_ids, deleted_items_path)
logging.info(f"User:{username} Saved deleted items") logging.info(f"User:{username} Saved deleted items")
except ClientError as c: except ClientError as c:
@ -304,4 +310,3 @@ def debug_main():
if __name__ == "__main__": if __name__ == "__main__":
debug_main() debug_main()