-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
33 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,44 @@ | ||
import json | ||
import logging | ||
from pathlib import Path | ||
from typing import Any, Dict, Optional, Tuple | ||
from typing import Optional | ||
|
||
from appdirs import user_data_dir | ||
from pydantic import BaseModel, BaseSettings, ValidationError | ||
from pydantic.env_settings import SettingsSourceCallable | ||
from pydantic import BaseModel, ValidationError | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
DATA_FILE = Path(user_data_dir("philipstv", "cyran.dev")) / "data.json" | ||
|
||
|
||
def json_data_source(settings: BaseSettings) -> Dict[str, Any]: | ||
try: | ||
_LOGGER.debug("Trying to load application data from %s", DATA_FILE) | ||
return json.loads(DATA_FILE.read_text()) # type: ignore [no-any-return] | ||
except FileNotFoundError: | ||
_LOGGER.debug("Data file not found") | ||
return {} | ||
finally: | ||
_LOGGER.debug("Application data loaded successfully") | ||
|
||
|
||
class HostData(BaseModel): | ||
host: str | ||
id: str | ||
key: str | ||
|
||
|
||
class PhilipsTVData(BaseSettings): | ||
class PhilipsTVData(BaseModel): | ||
last_host: HostData | ||
|
||
@classmethod | ||
def load(cls) -> Optional["PhilipsTVData"]: | ||
try: | ||
return cls() | ||
_LOGGER.debug("Trying to load application data from %s", DATA_FILE) | ||
return cls.model_validate(json.loads(DATA_FILE.read_text())) | ||
except FileNotFoundError: | ||
_LOGGER.debug("Data file not found") | ||
return None | ||
except ValidationError: | ||
return None | ||
finally: | ||
_LOGGER.debug("Application data loaded successfully") | ||
|
||
def save(self) -> None: | ||
_LOGGER.debug("Saving application data to %s", DATA_FILE) | ||
if not DATA_FILE.exists(): | ||
_LOGGER.debug("Data file doesn't exist, creating") | ||
DATA_FILE.parent.mkdir(parents=True, exist_ok=True) | ||
DATA_FILE.touch() | ||
DATA_FILE.write_text(self.json()) | ||
DATA_FILE.write_text(self.model_dump_json()) | ||
_LOGGER.debug("Application data saved successfully") | ||
|
||
class Config: | ||
@classmethod | ||
def customise_sources( | ||
cls, | ||
init_settings: SettingsSourceCallable, | ||
env_settings: SettingsSourceCallable, | ||
file_secret_settings: SettingsSourceCallable, | ||
) -> Tuple[SettingsSourceCallable, ...]: | ||
return (init_settings, json_data_source) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters