Skip to content

Commit

Permalink
WIP: Add schema and from_file classmethod
Browse files Browse the repository at this point in the history
  • Loading branch information
JimMadge committed Oct 27, 2023
1 parent 5e6726d commit b50ca47
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
68 changes: 45 additions & 23 deletions data_safe_haven/config/backend_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import appdirs
import yaml
from schema import Schema, SchemaError
from yaml.parser import ParserError

from data_safe_haven.exceptions import (
Expand All @@ -14,6 +15,12 @@
from data_safe_haven.utility import LoggingSingleton


config_directory = pathlib.Path(
appdirs.user_config_dir(appname="data_safe_haven")
).resolve()
config_file_path = config_directory / "config.yaml"


@dataclass
class Context:
admin_group_id: str
Expand All @@ -35,34 +42,31 @@ class ContextSettings:
...
"""

def __init__(self) -> None:
def __init__(self, settings_dict: dict[Any, Any]) -> None:
self.logger = LoggingSingleton()

self._settings: dict[Any, Any] | None = None

config_directory = pathlib.Path(
appdirs.user_config_dir(appname="data_safe_haven")
).resolve()
self.config_file_path = config_directory / "config.yaml"
context_schema = Schema({
"name": str,
"admin_group_id": str,
"location": str,
"subscription_name": str,
})

schema = Schema({
"selected": str,
"contexts": Schema({
str: context_schema,
}),
})

try:
self._settings: schema.validate(settings_dict)
except SchemaError as exc:
msg = f"Invalid context configuration file.\n{exc}"
raise DataSafeHavenParameterError(msg)

@property
def settings(self) -> dict[Any, Any]:
if not self._settings:
try:
with open(self.config_file_path, encoding="utf-8") as f_yaml:
settings = yaml.safe_load(f_yaml)
if isinstance(settings, dict):
self.logger.info(
f"Reading project settings from '[green]{self.config_file_path}[/]'."
)
self._settings = settings
self._context = Context(**settings.get("contexts").get(self._selected))
except FileNotFoundError as exc:
msg = f"Could not find file {self.config_file_path}.\n{exc}"
raise DataSafeHavenConfigError(msg) from exc
except ParserError as exc:
msg = f"Could not load settings from {self.config_file_path}.\n{exc}"
raise DataSafeHavenConfigError(msg) from exc

return self._settings

Expand Down Expand Up @@ -128,3 +132,21 @@ def write(self) -> None:
self.logger.info(
f"Saved context settings to '[green]{self.config_file_path}[/]'."
)

@classmethod
def from_file(cls) -> None:
logger = LoggingSingleton()
try:
with open(config_file_path, encoding="utf-8") as f_yaml:
settings = yaml.safe_load(f_yaml)
if isinstance(settings, dict):
logger.info(
f"Reading project settings from '[green]{config_file_path}[/]'."
)
return cls(settings)
except FileNotFoundError as exc:
msg = f"Could not find file {config_file_path}.\n{exc}"
raise DataSafeHavenConfigError(msg) from exc
except ParserError as exc:
msg = f"Could not load settings from {config_file_path}.\n{exc}"
raise DataSafeHavenConfigError(msg) from exc
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies = [
"pytz~=2022.7.0",
"PyYAML~=6.0",
"rich~=13.4.2",
"schema~=0.7.0",
"simple-acme-dns~=1.2.0",
"typer~=0.9.0",
"websocket-client~=1.5.0",
Expand Down

0 comments on commit b50ca47

Please sign in to comment.