Add some basic provider config code
This commit is contained in:
parent
3dfaa58751
commit
ba9763484d
|
@ -0,0 +1 @@
|
|||
from .provider import Config, Setting
|
|
@ -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)
|
Loading…
Reference in New Issue