Skip to content

Commit

Permalink
fix: revert schema validation, this is causing issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
dreulavelle committed Sep 21, 2024
1 parent 0dfccbd commit 12f4a1a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 36 deletions.
9 changes: 1 addition & 8 deletions src/controllers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@ async def get_settings_schema():
"""
Get the JSON schema for the settings.
"""
return settings_manager.schema

@router.get("/ranking")
async def get_ranking_schema():
"""
Get the JSON schema for the ranking.
"""
return SettingsModel.model_json_schema()
return settings_manager.settings.model_json_schema()

@router.get("/load")
async def load_settings():
Expand Down
35 changes: 8 additions & 27 deletions src/program/settings/manager.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import json
import os

from jsonschema import ValidationError as JsonSchemaValidationError
from jsonschema import validate
from loguru import logger
from pydantic import ValidationError

from program.settings.models import AppModel, Observable
from pydantic import ValidationError
from utils import data_dir_path
from loguru import logger


class SettingsManager:
"""Class that handles settings, ensuring they are validated against a Pydantic schema."""

def __init__(self):
self.schema = self.update_schema()
self.observers = []
self.filename = "settings.json"
self.settings_file = data_dir_path / self.filename
Expand All @@ -23,10 +19,9 @@ def __init__(self):

if not self.settings_file.exists():
self.settings = AppModel()
settings_dict = json.loads(self.settings.model_dump_json())
settings_dict = self.check_environment(settings_dict, "RIVEN")
validate(instance=settings_dict, schema=self.schema)
self.settings = AppModel.model_validate(settings_dict)
self.settings = AppModel.model_validate(
self.check_environment(json.loads(self.settings.model_dump_json()), "RIVEN")
)
self.notify_observers()
else:
self.load()
Expand Down Expand Up @@ -63,27 +58,17 @@ def check_environment(self, settings, prefix="", seperator="_"):
return checked_settings

def load(self, settings_dict: dict | None = None):
"""Load settings from file, validating against the JSON Schema."""
"""Load settings from file, validating against the AppModel schema."""
try:
if not settings_dict:
with open(self.settings_file, "r", encoding="utf-8") as file:
settings_dict = json.loads(file.read())
if os.environ.get("RIVEN_FORCE_ENV", "false").lower() == "true":
settings_dict = self.check_environment(settings_dict, "RIVEN")

# Validate against JSON Schema
self.update_schema()
validate(instance=settings_dict, schema=self.schema)
self.settings = AppModel.model_validate(settings_dict)
self.save()
except JsonSchemaValidationError as e:
logger.error(f"JSON Schema validation error: {e}")
raise
except ValidationError as e:
logger.error(f"Pydantic validation error: {e}")
for error in e.errors():
if error['type'] == 'value_error':
logger.error(f"Field '{error['loc'][0]}': {error['msg']}")
logger.error(f"Error validating settings: {e}")
raise
except json.JSONDecodeError as e:
logger.error(f"Error parsing settings file: {e}")
Expand All @@ -97,10 +82,6 @@ def save(self):
"""Save settings to file, using Pydantic model for JSON serialization."""
with open(self.settings_file, "w", encoding="utf-8") as file:
file.write(self.settings.model_dump_json(indent=4))
self.update_schema()

def update_schema(self):
self.schema = AppModel.model_json_schema()


settings_manager = SettingsManager()
settings_manager = SettingsManager()
1 change: 0 additions & 1 deletion src/program/settings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ class TorrentioConfig(Observable):
filter: str = "sort=qualitysize%7Cqualityfilter=480p,scr,cam"
url: str = "http://torrentio.strem.fun"
timeout: int = 30
ratelimit: bool = True


class KnightcrawlerConfig(Observable):
Expand Down

0 comments on commit 12f4a1a

Please sign in to comment.