Add some basic provider config code

This commit is contained in:
Jaculabilis 2022-08-12 06:15:34 +00:00
parent 3dfaa58751
commit ba9763484d
2 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1 @@
from .provider import Config, Setting

39
server/intake/provider.py Normal file
View File

@ -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)