add files
This commit is contained in:
39
settings_work.py
Normal file
39
settings_work.py
Normal file
@@ -0,0 +1,39 @@
|
||||
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}' не найден."}
|
||||
Reference in New Issue
Block a user