Files
parser/settings_work.py
Your Name 44cf3d3c21 add files
2026-01-26 15:20:04 +10:00

39 lines
1.4 KiB
Python
Raw Permalink 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 pydantic import BaseModel
from typing import List
import json
# Модель для источника
class Source(BaseModel):
name: str
url: str
prompt: str
# Модель для настроек (список источников)
class Settings(BaseModel):
sources: List[Source]
# Путь к файлу с настройками
SETTINGS_FILE = "config.json"
# Чтение настроек из файла
def read_settings() -> Settings:
try:
with open(SETTINGS_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
return Settings(**data)
except (FileNotFoundError, json.JSONDecodeError):
return Settings(sources=[])
# Запись настроек в файл
def write_settings(settings: Settings):
with open(SETTINGS_FILE, "w", encoding="utf-8") as f:
json.dump(settings.dict(), f, ensure_ascii=False, indent=2)
# Обновление данных по источнику
def update_source(new_source: Source) -> dict:
settings = read_settings()
for i, source in enumerate(settings.sources):
if source.name == new_source.name:
settings.sources[i] = new_source
write_settings(settings)
return {"code": 0, "message": f"Источник '{new_source.name}' успешно обновлен."}
return {"code": 1, "message": f"Источник с именем '{new_source.name}' не найден."}