Positional options (WIP).
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
# bonobo (see github.com/python-edgy/project)
|
||||
|
||||
name = 'bonobo'
|
||||
description = 'Bonobo'
|
||||
description = 'Bonobo, a simple, modern and atomic extract-transform-load toolkit for python 3.5+.'
|
||||
license = 'Apache License, Version 2.0'
|
||||
|
||||
url = 'https://www.bonobo-project.org/'
|
||||
|
||||
@ -1,5 +1,32 @@
|
||||
from bonobo.config.options import Option
|
||||
|
||||
__all__ = [
|
||||
'Configurable',
|
||||
'Option',
|
||||
]
|
||||
|
||||
_options_insert_order = 0
|
||||
|
||||
class Option:
|
||||
def __init__(self, type=None, *, required=False, positional=False, default=None):
|
||||
self.name = None
|
||||
self.type = type
|
||||
self.required = required
|
||||
self.positional = positional
|
||||
self.default = default
|
||||
|
||||
global _options_insert_order
|
||||
self.order = _options_insert_order
|
||||
_options_insert_order += 1
|
||||
|
||||
def __get__(self, inst, typ):
|
||||
if not self.name in inst.__options_values__:
|
||||
inst.__options_values__[self.name] = self.default() if callable(self.default) else self.default
|
||||
return inst.__options_values__[self.name]
|
||||
|
||||
def __set__(self, inst, value):
|
||||
inst.__options_values__[self.name] = self.type(value) if self.type else value
|
||||
|
||||
|
||||
class ConfigurableMeta(type):
|
||||
"""
|
||||
@ -25,15 +52,18 @@ class Configurable(metaclass=ConfigurableMeta):
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__()
|
||||
|
||||
self.__options_values__ = {}
|
||||
|
||||
missing = set()
|
||||
missing = list()
|
||||
for name, option in type(self).__options__.items():
|
||||
if option.required and not option.name in kwargs:
|
||||
missing.add(name)
|
||||
missing.append(name)
|
||||
|
||||
for i in range(min(len(args), len(missing))):
|
||||
kwargs
|
||||
|
||||
if len(missing):
|
||||
raise TypeError(
|
||||
|
||||
@ -8,7 +8,7 @@ __all__ = [
|
||||
]
|
||||
|
||||
|
||||
class JsonHandler:
|
||||
class JsonHandler():
|
||||
eol = ',\n'
|
||||
prefix, suffix = '[', ']'
|
||||
|
||||
|
||||
@ -18,6 +18,11 @@ class MyHarderConfigurable(MyConfigurable):
|
||||
class MyBetterConfigurable(MyConfigurable):
|
||||
required_str = Option(str, required=False, default='kaboom')
|
||||
|
||||
class MyConfigurableUsingPositionalOptions(MyConfigurable):
|
||||
first = Option(str, required=True, positional=True)
|
||||
second = Option(str, required=True, positional=True)
|
||||
third = Option(str, required=False, positional=True)
|
||||
|
||||
|
||||
class PrinterInterface():
|
||||
def print(self, *args):
|
||||
@ -133,3 +138,8 @@ def test_service_dependency_unavailable():
|
||||
o = MyServiceDependantConfigurable(printer='printer2')
|
||||
with pytest.raises(KeyError):
|
||||
SERVICES.args_for(o)
|
||||
|
||||
|
||||
def test_option_positional():
|
||||
o = MyConfigurableUsingPositionalOptions('1', '2', '3', required_str='hello')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user