diff --git a/server/intake/__init__.py b/server/intake/__init__.py new file mode 100644 index 0000000..1a7a680 --- /dev/null +++ b/server/intake/__init__.py @@ -0,0 +1 @@ +from .provider import Config, Setting diff --git a/server/intake/provider.py b/server/intake/provider.py new file mode 100644 index 0000000..2265162 --- /dev/null +++ b/server/intake/provider.py @@ -0,0 +1,39 @@ +class SettingMissingError(Exception): + """ + No value was provided for a required setting. + """ + def __init__(self, missing_names): + super().__init__("Missing required settings: {}".format(", ".join(missing_names))) + self.missing_names = missing_names + + +class Setting: + """ + A setting value declared by a provider and defined by a source. + """ + + def __init__(self, required=False, default=None): + self.required = required + self.value = default + + +class Config: + """ + Base class for provider configs. + """ + + name = Setting(required=True) + + provider = Setting(required=True) + + def __init__(self, **kwargs): + setting_names = [name for name in dir(self) if not name.startswith("__")] + missing = [] + for setting_name in setting_names: + setting: Setting = getattr(self, setting_name) + if setting_name in kwargs: + setting.value = kwargs[setting_name] + elif setting.required: + missing.append(setting_name) + if missing: + raise SettingMissingError(missing)