From ba9763484d9e856eb69e1fc761360a73fd4e8fdb Mon Sep 17 00:00:00 2001 From: Jaculabilis Date: Fri, 12 Aug 2022 06:15:34 +0000 Subject: [PATCH] Add some basic provider config code --- server/intake/__init__.py | 1 + server/intake/provider.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 server/intake/__init__.py create mode 100644 server/intake/provider.py 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)