First implementation of services and basic injection. Not working with CLI for now.
This commit is contained in:
56
bonobo/config/configurables.py
Normal file
56
bonobo/config/configurables.py
Normal file
@ -0,0 +1,56 @@
|
||||
from bonobo.config.options import Option
|
||||
|
||||
|
||||
class ConfigurableMeta(type):
|
||||
"""
|
||||
Metaclass for Configurables that will add options to a special __options__ dict.
|
||||
"""
|
||||
|
||||
def __init__(cls, what, bases=None, dict=None):
|
||||
super().__init__(what, bases, dict)
|
||||
cls.__options__ = {}
|
||||
for typ in cls.__mro__:
|
||||
for name, value in typ.__dict__.items():
|
||||
if isinstance(value, Option):
|
||||
if not value.name:
|
||||
value.name = name
|
||||
if not name in cls.__options__:
|
||||
cls.__options__[name] = value
|
||||
|
||||
|
||||
class Configurable(metaclass=ConfigurableMeta):
|
||||
"""
|
||||
Generic class for configurable objects. Configurable objects have a dictionary of "options" descriptors that defines
|
||||
the configuration schema of the type.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__()
|
||||
|
||||
self.__options_values__ = {}
|
||||
|
||||
missing = set()
|
||||
for name, option in type(self).__options__.items():
|
||||
if option.required and not option.name in kwargs:
|
||||
missing.add(name)
|
||||
|
||||
if len(missing):
|
||||
raise TypeError(
|
||||
'{}() missing {} required option{}: {}.'.format(
|
||||
type(self).__name__,
|
||||
len(missing), 's' if len(missing) > 1 else '', ', '.join(map(repr, sorted(missing)))
|
||||
)
|
||||
)
|
||||
|
||||
extraneous = set(kwargs.keys()) - set(type(self).__options__.keys())
|
||||
if len(extraneous):
|
||||
raise TypeError(
|
||||
'{}() got {} unexpected option{}: {}.'.format(
|
||||
type(self).__name__,
|
||||
len(extraneous), 's' if len(extraneous) > 1 else '', ', '.join(map(repr, sorted(extraneous)))
|
||||
)
|
||||
)
|
||||
|
||||
for name, value in kwargs.items():
|
||||
setattr(self, name, kwargs[name])
|
||||
Reference in New Issue
Block a user