2015-06-02 07:04:25 -05:00
|
|
|
#
|
|
|
|
# Copyright (C) 2015 FreeIPA Contributors see COPYING for license
|
|
|
|
#
|
|
|
|
|
|
|
|
"""
|
|
|
|
The framework core.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import abc
|
2015-08-06 01:14:17 -05:00
|
|
|
import collections
|
|
|
|
import functools
|
2015-06-02 07:04:25 -05:00
|
|
|
import itertools
|
2015-08-06 01:14:17 -05:00
|
|
|
import sys
|
2015-06-02 07:04:25 -05:00
|
|
|
|
2015-08-12 07:06:54 -05:00
|
|
|
import six
|
|
|
|
|
2016-11-11 09:32:07 -06:00
|
|
|
from . import util
|
2015-06-02 07:04:25 -05:00
|
|
|
from .util import from_
|
|
|
|
|
2016-11-11 09:32:07 -06:00
|
|
|
__all__ = ['InvalidStateError', 'KnobValueError', 'Property', 'knob',
|
2016-11-02 00:30:38 -05:00
|
|
|
'Configurable', 'group', 'Component', 'Composite']
|
2015-06-02 07:04:25 -05:00
|
|
|
|
2016-11-07 07:01:10 -06:00
|
|
|
NoneType = type(None)
|
2017-03-15 02:47:46 -05:00
|
|
|
builtin_type = type
|
2016-11-07 07:01:10 -06:00
|
|
|
|
2015-06-02 07:04:25 -05:00
|
|
|
# Configurable states
|
|
|
|
_VALIDATE_PENDING = 'VALIDATE_PENDING'
|
|
|
|
_VALIDATE_RUNNING = 'VALIDATE_RUNNING'
|
|
|
|
_EXECUTE_PENDING = 'EXECUTE_PENDING'
|
|
|
|
_EXECUTE_RUNNING = 'EXECUTE_RUNNING'
|
|
|
|
_STOPPED = 'STOPPED'
|
|
|
|
_FAILED = 'FAILED'
|
|
|
|
_CLOSED = 'CLOSED'
|
|
|
|
|
|
|
|
_missing = object()
|
|
|
|
_counter = itertools.count()
|
|
|
|
|
|
|
|
|
2015-08-06 01:14:17 -05:00
|
|
|
@functools.cmp_to_key
|
|
|
|
def _class_key(a, b):
|
2015-06-02 07:04:25 -05:00
|
|
|
if a is b:
|
|
|
|
return 0
|
|
|
|
elif issubclass(a, b):
|
|
|
|
return -1
|
|
|
|
elif issubclass(b, a):
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
class InvalidStateError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class KnobValueError(ValueError):
|
|
|
|
def __init__(self, name, message):
|
|
|
|
super(KnobValueError, self).__init__(message)
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
|
2022-12-20 02:58:37 -06:00
|
|
|
class PropertyBase(metaclass=util.InnerClassMeta):
|
2015-08-06 01:14:17 -05:00
|
|
|
# shut up pylint
|
2015-06-02 07:04:25 -05:00
|
|
|
__outer_class__ = None
|
|
|
|
__outer_name__ = None
|
|
|
|
|
2015-08-06 01:14:17 -05:00
|
|
|
_order = None
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def default(self):
|
|
|
|
raise AttributeError('default')
|
|
|
|
|
|
|
|
def __init__(self, outer):
|
2015-08-06 01:14:17 -05:00
|
|
|
pass
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
def __get__(self, obj, obj_type):
|
2015-08-06 01:14:17 -05:00
|
|
|
while obj is not None:
|
|
|
|
try:
|
|
|
|
return obj.__dict__[self.__outer_name__]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
obj = obj._get_fallback()
|
|
|
|
|
2015-06-02 07:04:25 -05:00
|
|
|
try:
|
|
|
|
return self.default
|
2015-08-06 01:14:17 -05:00
|
|
|
except AttributeError:
|
|
|
|
raise AttributeError(self.__outer_name__)
|
|
|
|
|
|
|
|
def __set__(self, obj, value):
|
|
|
|
try:
|
|
|
|
obj.__dict__[self.__outer_name__] = value
|
|
|
|
except KeyError:
|
|
|
|
raise AttributeError(self.__outer_name__)
|
|
|
|
|
|
|
|
def __delete__(self, obj):
|
|
|
|
try:
|
|
|
|
del obj.__dict__[self.__outer_name__]
|
|
|
|
except KeyError:
|
|
|
|
raise AttributeError(self.__outer_name__)
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
def Property(default=_missing):
|
|
|
|
class_dict = {}
|
|
|
|
if default is not _missing:
|
|
|
|
class_dict['default'] = default
|
|
|
|
|
|
|
|
return util.InnerClassMeta('Property', (PropertyBase,), class_dict)
|
|
|
|
|
|
|
|
|
|
|
|
class KnobBase(PropertyBase):
|
|
|
|
type = None
|
|
|
|
sensitive = False
|
|
|
|
deprecated = False
|
|
|
|
description = None
|
2016-10-27 05:34:08 -05:00
|
|
|
cli_names = (None,)
|
|
|
|
cli_deprecated_names = ()
|
2015-06-02 07:04:25 -05:00
|
|
|
cli_metavar = None
|
|
|
|
|
2015-08-06 01:14:17 -05:00
|
|
|
def __init__(self, outer):
|
|
|
|
self.outer = outer
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
def validate(self, value):
|
|
|
|
pass
|
|
|
|
|
2017-03-08 02:03:13 -06:00
|
|
|
@classmethod
|
|
|
|
def group(cls):
|
|
|
|
return cls.__outer_class__.group()
|
|
|
|
|
2016-10-27 05:34:08 -05:00
|
|
|
@classmethod
|
|
|
|
def is_cli_positional(cls):
|
|
|
|
return all(n is not None and not n.startswith('-')
|
|
|
|
for n in cls.cli_names)
|
|
|
|
|
2015-06-02 07:04:25 -05:00
|
|
|
@classmethod
|
|
|
|
def default_getter(cls, func):
|
|
|
|
@property
|
|
|
|
def default(self):
|
|
|
|
return func(self.outer)
|
|
|
|
cls.default = default
|
|
|
|
|
|
|
|
return cls
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def validator(cls, func):
|
|
|
|
def validate(self, value):
|
|
|
|
func(self.outer, value)
|
|
|
|
super(cls, self).validate(value)
|
|
|
|
cls.validate = validate
|
|
|
|
|
|
|
|
return cls
|
|
|
|
|
|
|
|
|
2017-03-08 02:03:13 -06:00
|
|
|
def _knob(type=_missing, default=_missing, bases=_missing, _order=_missing,
|
|
|
|
sensitive=_missing, deprecated=_missing, description=_missing,
|
|
|
|
group=_missing, cli_names=_missing, cli_deprecated_names=_missing,
|
|
|
|
cli_metavar=_missing):
|
|
|
|
if type is None:
|
|
|
|
type = NoneType
|
2016-10-31 00:22:44 -05:00
|
|
|
|
|
|
|
if bases is _missing:
|
|
|
|
bases = (KnobBase,)
|
2017-03-15 02:47:46 -05:00
|
|
|
elif isinstance(bases, builtin_type):
|
2016-10-31 00:22:44 -05:00
|
|
|
bases = (bases,)
|
2016-11-07 07:01:10 -06:00
|
|
|
|
2016-10-27 05:34:08 -05:00
|
|
|
if cli_names is None or isinstance(cli_names, str):
|
|
|
|
cli_names = (cli_names,)
|
|
|
|
elif cli_names is not _missing:
|
|
|
|
cli_names = tuple(cli_names)
|
|
|
|
|
|
|
|
if isinstance(cli_deprecated_names, str):
|
|
|
|
cli_deprecated_names = (cli_deprecated_names,)
|
|
|
|
elif cli_deprecated_names is not _missing:
|
|
|
|
cli_deprecated_names = tuple(cli_deprecated_names)
|
|
|
|
|
2015-06-02 07:04:25 -05:00
|
|
|
class_dict = {}
|
2017-03-08 02:03:13 -06:00
|
|
|
if type is not _missing:
|
|
|
|
class_dict['type'] = type
|
2015-06-02 07:04:25 -05:00
|
|
|
if default is not _missing:
|
|
|
|
class_dict['default'] = default
|
2017-03-08 02:03:13 -06:00
|
|
|
if _order is not _missing:
|
|
|
|
class_dict['_order'] = _order
|
2015-06-02 07:04:25 -05:00
|
|
|
if sensitive is not _missing:
|
|
|
|
class_dict['sensitive'] = sensitive
|
|
|
|
if deprecated is not _missing:
|
|
|
|
class_dict['deprecated'] = deprecated
|
|
|
|
if description is not _missing:
|
|
|
|
class_dict['description'] = description
|
2017-03-08 02:03:13 -06:00
|
|
|
if group is not _missing:
|
|
|
|
class_dict['group'] = group
|
2016-10-27 05:34:08 -05:00
|
|
|
if cli_names is not _missing:
|
|
|
|
class_dict['cli_names'] = cli_names
|
|
|
|
if cli_deprecated_names is not _missing:
|
|
|
|
class_dict['cli_deprecated_names'] = cli_deprecated_names
|
2015-06-02 07:04:25 -05:00
|
|
|
if cli_metavar is not _missing:
|
|
|
|
class_dict['cli_metavar'] = cli_metavar
|
|
|
|
|
2016-10-31 00:22:44 -05:00
|
|
|
return util.InnerClassMeta('Knob', bases, class_dict)
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
|
2017-03-08 02:03:13 -06:00
|
|
|
def knob(type, default=_missing, **kwargs):
|
|
|
|
"""
|
|
|
|
Define a new knob.
|
|
|
|
"""
|
|
|
|
return _knob(
|
|
|
|
type, default,
|
|
|
|
_order=next(_counter),
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def extend_knob(base, default=_missing, bases=_missing, group=_missing,
|
|
|
|
**kwargs):
|
|
|
|
"""
|
|
|
|
Extend an existing knob.
|
|
|
|
"""
|
|
|
|
if bases is _missing:
|
|
|
|
bases = (base,)
|
|
|
|
|
|
|
|
if group is _missing:
|
|
|
|
group = staticmethod(base.group)
|
|
|
|
|
|
|
|
return _knob(
|
|
|
|
_missing, default,
|
|
|
|
bases=bases,
|
|
|
|
_order=_missing,
|
|
|
|
group=group,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-20 02:58:37 -06:00
|
|
|
class Configurable(metaclass=abc.ABCMeta):
|
2015-06-02 07:04:25 -05:00
|
|
|
"""
|
|
|
|
Base class of all configurables.
|
|
|
|
|
|
|
|
FIXME: details of validate/execute, properties and knobs
|
|
|
|
"""
|
|
|
|
|
|
|
|
@classmethod
|
2015-08-06 01:14:17 -05:00
|
|
|
def properties(cls):
|
2015-06-02 07:04:25 -05:00
|
|
|
"""
|
2015-08-06 01:14:17 -05:00
|
|
|
Iterate over properties defined for the configurable.
|
2015-06-02 07:04:25 -05:00
|
|
|
"""
|
|
|
|
|
2015-08-06 01:14:17 -05:00
|
|
|
assert not hasattr(super(Configurable, cls), 'properties')
|
|
|
|
|
|
|
|
seen = set()
|
|
|
|
|
|
|
|
for owner_cls in cls.__mro__:
|
|
|
|
result = []
|
2015-06-02 07:04:25 -05:00
|
|
|
|
2016-02-17 09:48:58 -06:00
|
|
|
for name, prop_cls in owner_cls.__dict__.items():
|
2015-08-06 01:14:17 -05:00
|
|
|
if name in seen:
|
|
|
|
continue
|
|
|
|
seen.add(name)
|
|
|
|
|
|
|
|
if not isinstance(prop_cls, type):
|
|
|
|
continue
|
|
|
|
if not issubclass(prop_cls, PropertyBase):
|
|
|
|
continue
|
|
|
|
|
|
|
|
result.append((prop_cls._order, owner_cls, name))
|
|
|
|
|
|
|
|
result = sorted(result, key=lambda r: r[0])
|
|
|
|
|
2016-10-04 09:54:44 -05:00
|
|
|
for _order, owner_cls, name in result:
|
2015-08-06 01:14:17 -05:00
|
|
|
yield owner_cls, name
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def knobs(cls):
|
|
|
|
for owner_cls, name in cls.properties():
|
|
|
|
prop_cls = getattr(owner_cls, name)
|
|
|
|
if issubclass(prop_cls, KnobBase):
|
|
|
|
yield owner_cls, name
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def group(cls):
|
|
|
|
assert not hasattr(super(Configurable, cls), 'group')
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Initialize the configurable.
|
|
|
|
"""
|
|
|
|
|
2015-08-06 01:14:17 -05:00
|
|
|
cls = self.__class__
|
|
|
|
for owner_cls, name in cls.properties():
|
2015-06-02 07:04:25 -05:00
|
|
|
if name.startswith('_'):
|
|
|
|
continue
|
2015-08-06 01:14:17 -05:00
|
|
|
prop_cls = getattr(owner_cls, name)
|
|
|
|
if not isinstance(prop_cls, type):
|
2015-06-02 07:04:25 -05:00
|
|
|
continue
|
2015-08-06 01:14:17 -05:00
|
|
|
if not issubclass(prop_cls, PropertyBase):
|
2015-06-02 07:04:25 -05:00
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
value = kwargs.pop(name)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
2015-12-07 06:35:49 -06:00
|
|
|
setattr(self, name, value)
|
|
|
|
|
|
|
|
for owner_cls, name in cls.knobs():
|
|
|
|
if name.startswith('_'):
|
|
|
|
continue
|
|
|
|
if not isinstance(self, owner_cls):
|
|
|
|
continue
|
|
|
|
value = getattr(self, name, None)
|
|
|
|
if value is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
prop_cls = getattr(owner_cls, name)
|
|
|
|
prop = prop_cls(self)
|
|
|
|
try:
|
|
|
|
prop.validate(value)
|
|
|
|
except ValueError as e:
|
|
|
|
raise KnobValueError(name, str(e))
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
if kwargs:
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
extra = sorted(kwargs)
|
2015-06-02 07:04:25 -05:00
|
|
|
raise TypeError(
|
|
|
|
"{0}() got {1} unexpected keyword arguments: {2}".format(
|
|
|
|
type(self).__name__,
|
|
|
|
len(extra),
|
|
|
|
', '.join(repr(name) for name in extra)))
|
|
|
|
|
|
|
|
self._reset()
|
|
|
|
|
|
|
|
def _reset(self):
|
|
|
|
assert not hasattr(super(Configurable, self), '_reset')
|
|
|
|
|
|
|
|
self.__state = _VALIDATE_PENDING
|
|
|
|
self.__gen = util.run_generator_with_yield_from(self._configure())
|
|
|
|
|
|
|
|
def _get_components(self):
|
|
|
|
assert not hasattr(super(Configurable, self), '_get_components')
|
|
|
|
|
|
|
|
raise TypeError("{0} is not composite".format(self))
|
|
|
|
|
2015-08-06 01:14:17 -05:00
|
|
|
def _get_fallback(self):
|
2018-07-10 15:14:04 -05:00
|
|
|
pass
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def _configure(self):
|
|
|
|
"""
|
|
|
|
Coroutine which defines the logic of the configurable.
|
|
|
|
"""
|
|
|
|
|
|
|
|
assert not hasattr(super(Configurable, self), '_configure')
|
|
|
|
|
|
|
|
self.__transition(_VALIDATE_RUNNING, _EXECUTE_PENDING)
|
|
|
|
|
|
|
|
while self.__state != _EXECUTE_RUNNING:
|
|
|
|
yield
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""
|
|
|
|
Run the configurable.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.validate()
|
|
|
|
if self.__state == _EXECUTE_PENDING:
|
2018-03-13 12:05:05 -05:00
|
|
|
return self.execute()
|
2018-07-10 15:14:04 -05:00
|
|
|
return None
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
def validate(self):
|
|
|
|
"""
|
|
|
|
Run the validation part of the configurable.
|
|
|
|
"""
|
|
|
|
|
2016-10-04 09:54:44 -05:00
|
|
|
for _nothing in self._validator():
|
2015-06-02 07:04:25 -05:00
|
|
|
pass
|
|
|
|
|
|
|
|
def _validator(self):
|
|
|
|
"""
|
|
|
|
Coroutine which runs the validation part of the configurable.
|
|
|
|
"""
|
|
|
|
|
2016-09-20 08:12:30 -05:00
|
|
|
return self.__runner(_VALIDATE_PENDING,
|
|
|
|
_VALIDATE_RUNNING,
|
|
|
|
self._handle_validate_exception)
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
def execute(self):
|
|
|
|
"""
|
|
|
|
Run the execution part of the configurable.
|
|
|
|
"""
|
2018-03-13 12:05:05 -05:00
|
|
|
return_value = 0
|
2015-06-02 07:04:25 -05:00
|
|
|
|
2018-03-13 12:05:05 -05:00
|
|
|
for rval in self._executor():
|
|
|
|
if rval is not None and rval > return_value:
|
|
|
|
return_value = rval
|
|
|
|
|
|
|
|
return return_value
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
def _executor(self):
|
|
|
|
"""
|
|
|
|
Coroutine which runs the execution part of the configurable.
|
|
|
|
"""
|
|
|
|
|
2016-09-20 08:12:30 -05:00
|
|
|
return self.__runner(_EXECUTE_PENDING,
|
|
|
|
_EXECUTE_RUNNING,
|
|
|
|
self._handle_execute_exception)
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
def done(self):
|
|
|
|
"""
|
|
|
|
Return True if the configurable has finished.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self.__state in (_STOPPED, _FAILED, _CLOSED)
|
|
|
|
|
|
|
|
def run_until_executing(self, gen):
|
|
|
|
while self.__state != _EXECUTE_RUNNING:
|
|
|
|
try:
|
2015-08-12 05:46:22 -05:00
|
|
|
yield next(gen)
|
2015-06-02 07:04:25 -05:00
|
|
|
except StopIteration:
|
|
|
|
break
|
|
|
|
|
2016-09-20 08:12:30 -05:00
|
|
|
def __runner(self, pending_state, running_state, exc_handler):
|
2015-06-02 07:04:25 -05:00
|
|
|
self.__transition(pending_state, running_state)
|
|
|
|
|
2022-12-19 10:01:45 -06:00
|
|
|
def step_next():
|
|
|
|
return next(self.__gen)
|
|
|
|
|
|
|
|
step = step_next
|
|
|
|
|
2015-06-02 07:04:25 -05:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
step()
|
|
|
|
except StopIteration:
|
|
|
|
self.__transition(running_state, _STOPPED)
|
|
|
|
break
|
|
|
|
except GeneratorExit:
|
|
|
|
self.__transition(running_state, _CLOSED)
|
|
|
|
break
|
|
|
|
except BaseException:
|
|
|
|
exc_info = sys.exc_info()
|
|
|
|
try:
|
2016-09-20 08:12:30 -05:00
|
|
|
exc_handler(exc_info)
|
2015-06-02 07:04:25 -05:00
|
|
|
except BaseException:
|
|
|
|
self.__transition(running_state, _FAILED)
|
2016-09-20 08:15:50 -05:00
|
|
|
raise
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
if self.__state != running_state:
|
|
|
|
break
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
except BaseException:
|
|
|
|
exc_info = sys.exc_info()
|
2022-12-19 10:01:45 -06:00
|
|
|
|
|
|
|
def step_throw():
|
|
|
|
return self.__gen.throw(*exc_info)
|
|
|
|
|
|
|
|
step = step_throw
|
2015-06-02 07:04:25 -05:00
|
|
|
else:
|
2022-12-19 10:01:45 -06:00
|
|
|
step = step_next
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
def _handle_exception(self, exc_info):
|
|
|
|
assert not hasattr(super(Configurable, self), '_handle_exception')
|
|
|
|
|
2015-08-12 07:06:54 -05:00
|
|
|
six.reraise(*exc_info)
|
2015-06-02 07:04:25 -05:00
|
|
|
|
2016-09-20 08:12:30 -05:00
|
|
|
def _handle_validate_exception(self, exc_info):
|
|
|
|
assert not hasattr(super(Configurable, self),
|
|
|
|
'_handle_validate_exception')
|
|
|
|
self._handle_exception(exc_info)
|
|
|
|
|
|
|
|
def _handle_execute_exception(self, exc_info):
|
|
|
|
assert not hasattr(super(Configurable, self),
|
|
|
|
'_handle_execute_exception')
|
|
|
|
self._handle_exception(exc_info)
|
|
|
|
|
2015-06-02 07:04:25 -05:00
|
|
|
def __transition(self, from_state, to_state):
|
|
|
|
if self.__state != from_state:
|
|
|
|
raise InvalidStateError(self.__state)
|
|
|
|
|
|
|
|
self.__state = to_state
|
|
|
|
|
|
|
|
|
2016-11-02 00:30:38 -05:00
|
|
|
def group(cls):
|
|
|
|
def group():
|
2015-06-02 07:04:25 -05:00
|
|
|
return cls
|
|
|
|
|
2016-11-02 00:30:38 -05:00
|
|
|
cls.group = staticmethod(group)
|
|
|
|
|
|
|
|
return cls
|
|
|
|
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
class ComponentMeta(util.InnerClassMeta, abc.ABCMeta):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-12-20 02:58:37 -06:00
|
|
|
class ComponentBase(Configurable, metaclass=ComponentMeta):
|
2015-08-06 01:14:17 -05:00
|
|
|
# shut up pylint
|
|
|
|
__outer_class__ = None
|
|
|
|
__outer_name__ = None
|
|
|
|
|
2015-06-02 07:04:25 -05:00
|
|
|
_order = None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def group(cls):
|
|
|
|
result = super(ComponentBase, cls).group()
|
|
|
|
if result is not None:
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
return cls.__outer_class__.group()
|
|
|
|
|
|
|
|
def __init__(self, parent, **kwargs):
|
|
|
|
self.__parent = parent
|
|
|
|
|
|
|
|
super(ComponentBase, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def parent(self):
|
|
|
|
return self.__parent
|
|
|
|
|
|
|
|
def __get__(self, obj, obj_type):
|
|
|
|
obj.__dict__[self.__outer_name__] = self
|
|
|
|
return self
|
|
|
|
|
2015-08-06 01:14:17 -05:00
|
|
|
def _get_fallback(self):
|
|
|
|
return self.__parent
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
def _handle_exception(self, exc_info):
|
|
|
|
try:
|
|
|
|
super(ComponentBase, self)._handle_exception(exc_info)
|
|
|
|
except BaseException:
|
|
|
|
exc_info = sys.exc_info()
|
|
|
|
self.__parent._handle_exception(exc_info)
|
|
|
|
|
|
|
|
|
|
|
|
def Component(cls):
|
|
|
|
class_dict = {}
|
|
|
|
class_dict['_order'] = next(_counter)
|
|
|
|
|
|
|
|
return ComponentMeta('Component', (ComponentBase, cls), class_dict)
|
|
|
|
|
|
|
|
|
|
|
|
class Composite(Configurable):
|
|
|
|
"""
|
|
|
|
Configurable composed of any number of components.
|
|
|
|
|
|
|
|
Provides knobs of all child components.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@classmethod
|
2015-08-06 01:14:17 -05:00
|
|
|
def properties(cls):
|
2015-06-02 07:04:25 -05:00
|
|
|
name_dict = {}
|
2015-08-06 01:14:17 -05:00
|
|
|
owner_dict = collections.OrderedDict()
|
2015-06-02 07:04:25 -05:00
|
|
|
|
2015-08-06 01:14:17 -05:00
|
|
|
for owner_cls, name in super(Composite, cls).properties():
|
2015-06-02 07:04:25 -05:00
|
|
|
name_dict[name] = owner_cls
|
2015-08-06 01:14:17 -05:00
|
|
|
owner_dict.setdefault(owner_cls, []).append(name)
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
for owner_cls, name in cls.components():
|
|
|
|
comp_cls = getattr(cls, name)
|
2015-08-06 01:14:17 -05:00
|
|
|
|
2015-06-02 07:04:25 -05:00
|
|
|
for owner_cls, name in comp_cls.knobs():
|
|
|
|
if hasattr(cls, name):
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
last_owner_cls = name_dict[name]
|
|
|
|
except KeyError:
|
|
|
|
name_dict[name] = owner_cls
|
2015-08-06 01:14:17 -05:00
|
|
|
owner_dict.setdefault(owner_cls, []).append(name)
|
2015-06-02 07:04:25 -05:00
|
|
|
else:
|
2015-08-06 01:14:17 -05:00
|
|
|
knob_cls = getattr(owner_cls, name)
|
|
|
|
last_knob_cls = getattr(last_owner_cls, name)
|
|
|
|
if issubclass(knob_cls, last_knob_cls):
|
|
|
|
name_dict[name] = owner_cls
|
|
|
|
owner_dict[last_owner_cls].remove(name)
|
|
|
|
owner_dict.setdefault(owner_cls, [])
|
|
|
|
if name not in owner_dict[owner_cls]:
|
|
|
|
owner_dict[owner_cls].append(name)
|
|
|
|
elif not issubclass(last_knob_cls, knob_cls):
|
2015-06-02 07:04:25 -05:00
|
|
|
raise TypeError("{0}.knobs(): conflicting definitions "
|
|
|
|
"of '{1}' in {2} and {3}".format(
|
|
|
|
cls.__name__,
|
|
|
|
name,
|
|
|
|
last_owner_cls.__name__,
|
|
|
|
owner_cls.__name__))
|
|
|
|
|
2015-08-06 01:14:17 -05:00
|
|
|
for owner_cls in sorted(owner_dict, key=_class_key):
|
|
|
|
for name in owner_dict[owner_cls]:
|
|
|
|
yield owner_cls, name
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def components(cls):
|
|
|
|
assert not hasattr(super(Composite, cls), 'components')
|
|
|
|
|
2015-08-06 01:14:17 -05:00
|
|
|
seen = set()
|
|
|
|
|
|
|
|
for owner_cls in cls.__mro__:
|
|
|
|
result = []
|
|
|
|
|
2016-02-17 09:48:58 -06:00
|
|
|
for name, comp_cls in owner_cls.__dict__.items():
|
2015-08-06 01:14:17 -05:00
|
|
|
if name in seen:
|
|
|
|
continue
|
|
|
|
seen.add(name)
|
|
|
|
|
|
|
|
if not isinstance(comp_cls, type):
|
|
|
|
continue
|
|
|
|
if not issubclass(comp_cls, ComponentBase):
|
|
|
|
continue
|
|
|
|
|
|
|
|
result.append((comp_cls._order, owner_cls, name))
|
|
|
|
|
|
|
|
result = sorted(result, key=lambda r: r[0])
|
|
|
|
|
2016-10-04 09:54:44 -05:00
|
|
|
for _order, owner_cls, name in result:
|
2015-08-06 01:14:17 -05:00
|
|
|
yield owner_cls, name
|
2015-06-02 07:04:25 -05:00
|
|
|
|
2015-12-16 06:43:13 -06:00
|
|
|
def __getattr__(self, name):
|
|
|
|
for owner_cls, knob_name in self.knobs():
|
|
|
|
if knob_name == name:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise AttributeError(name)
|
|
|
|
|
|
|
|
for component in self.__components:
|
|
|
|
if isinstance(component, owner_cls):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise AttributeError(name)
|
|
|
|
|
|
|
|
return getattr(component, name)
|
|
|
|
|
2015-06-02 07:04:25 -05:00
|
|
|
def _reset(self):
|
|
|
|
self.__components = list(self._get_components())
|
|
|
|
|
|
|
|
super(Composite, self)._reset()
|
|
|
|
|
|
|
|
def _get_components(self):
|
2016-10-04 09:54:44 -05:00
|
|
|
for _owner_cls, name in self.components():
|
2015-06-02 07:04:25 -05:00
|
|
|
yield getattr(self, name)
|
|
|
|
|
|
|
|
def _configure(self):
|
|
|
|
validate = [(c, c._validator()) for c in self.__components]
|
|
|
|
while True:
|
|
|
|
new_validate = []
|
|
|
|
for child, validator in validate:
|
|
|
|
try:
|
2015-08-12 05:46:22 -05:00
|
|
|
next(validator)
|
2015-06-02 07:04:25 -05:00
|
|
|
except StopIteration:
|
2015-12-16 06:43:13 -06:00
|
|
|
pass
|
2015-06-02 07:04:25 -05:00
|
|
|
else:
|
|
|
|
new_validate.append((child, validator))
|
|
|
|
if not new_validate:
|
|
|
|
break
|
|
|
|
validate = new_validate
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
if not self.__components:
|
|
|
|
return
|
|
|
|
|
|
|
|
yield from_(super(Composite, self)._configure())
|
|
|
|
|
2015-12-16 06:43:13 -06:00
|
|
|
execute = [(c, c._executor()) for c in self.__components
|
|
|
|
if not c.done()]
|
2015-06-02 07:04:25 -05:00
|
|
|
while True:
|
|
|
|
new_execute = []
|
|
|
|
for child, executor in execute:
|
|
|
|
try:
|
2015-08-12 05:46:22 -05:00
|
|
|
next(executor)
|
2015-06-02 07:04:25 -05:00
|
|
|
except StopIteration:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
new_execute.append((child, executor))
|
|
|
|
if not new_execute:
|
|
|
|
break
|
|
|
|
execute = new_execute
|
|
|
|
|
|
|
|
yield
|