Files
basethon/basethon/async_base_session.py
2025-05-18 13:10:49 +03:00

31 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from pathlib import Path
from typing import AsyncGenerator
from jsoner.async_ import json_read
class AsyncBaseSession:
def __init__(self, base_dir: Path, errors_dir: Path, banned_dir: Path):
self.base_dir = base_dir
self.errors_dir = errors_dir
self.banned_dir = banned_dir
self.json_errors: set[Path] = set()
async def iter_sessions(self) -> AsyncGenerator[tuple[Path, Path, dict], None]:
"""
Поиск сессий в base_dir
Возвращает генератор с кортежами (item, json_path, json_data)
Если json_file не найден, то добавляет его (не существующий) в self.json_errors
Если json_data не найден, то добавляет его (не существующий) в self.json_errors
"""
for item in self.base_dir.glob("*.session"):
json_file = item.with_suffix(".json")
if not json_file.is_file():
self.json_errors.add(json_file)
continue
if not (json_data := await json_read(json_file)):
self.json_errors.add(json_file)
continue
yield item, json_file, json_data