Page MenuHomePhabricator
Paste P17854

(An Untitled Masterwork)

Authored by dcaro on Thu, Nov 25, 3:31 PM.
def from_file(cls, configfile: Optional[Path], overrides: Dict[str, Any]) -> "ControllerConfig":
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)
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]
# 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)