31 lines
1.2 KiB
Python
31 lines
1.2 KiB
Python
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
|