Page MenuHomePhabricator
Paste P17854

(An Untitled Masterwork)
ActivePublic

Authored by dcaro on Nov 25 2021, 3:31 PM.
Tags
None
Referenced Files
F34767031: raw-paste-data.txt
Nov 25 2021, 3:31 PM
Subscribers
None
@classmethod
def from_file(cls, configfile: Optional[Path], overrides: Dict[str, Any]) -> "ControllerConfig":
try:
data = yaml.safe_load(configfile.read_text()) if configfile is not None else {}
except FileNotFoundError as error:
LOGGER.exception("Configuration file %s is not a file: %s", configfile, error)
except yaml.error.YAMLError as error:
LOGGER.exception("Configuration file %s contains malformed yaml: %s", configfile, error)
raise
data.update(overrides)
for key, value in data.items():
if key not in dir(cls):
raise Exception(f"Unknown config key {key}, known ones are: {dir(self)}")
expected_type = cls.__annotations__[key]
try:
# make sure to cast the strings to they annotated types
data[key] = expected_type(value)
except Exception as error:
raise Exception(f"Bad value for key {key}, got {type(value)} expected {expected_type}.") from error
return cls(**data)