an OptionDescription can be serialized
This commit is contained in:
parent
f379a0fc0a
commit
2c1da6d72e
@ -92,9 +92,10 @@ class BaseInformation(object):
|
|||||||
|
|
||||||
|
|
||||||
class BaseOption(BaseInformation):
|
class BaseOption(BaseInformation):
|
||||||
__slots__ = ('_readonly',)
|
__slots__ = ('_readonly', '_state_consistencies', '_state_requires')
|
||||||
|
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
|
if not name.startswith('_state'):
|
||||||
is_readonly = False
|
is_readonly = False
|
||||||
# never change _name
|
# never change _name
|
||||||
if name == '_name':
|
if name == '_name':
|
||||||
@ -116,25 +117,36 @@ class BaseOption(BaseInformation):
|
|||||||
if is_readonly:
|
if is_readonly:
|
||||||
raise AttributeError(_("'{0}' ({1}) object attribute '{2}' is"
|
raise AttributeError(_("'{0}' ({1}) object attribute '{2}' is"
|
||||||
" read-only").format(
|
" read-only").format(
|
||||||
self.__class__.__name__, self._name,
|
self.__class__.__name__,
|
||||||
|
self._name,
|
||||||
name))
|
name))
|
||||||
object.__setattr__(self, name, value)
|
object.__setattr__(self, name, value)
|
||||||
|
|
||||||
def _impl_convert_consistencies(self, value, cache):
|
def _impl_convert_consistencies(self, cache):
|
||||||
# cache is a dico in import/not a dico in export
|
# cache is a dico in import/not a dico in export
|
||||||
new_value = []
|
if self._consistencies is None:
|
||||||
for consistency in value:
|
self._state_consistencies = None
|
||||||
if isinstance(cache, dict):
|
|
||||||
new_value = (consistency[0], cache[consistency[1]])
|
|
||||||
else:
|
else:
|
||||||
new_value = (consistency[0], cache.impl_get_path_by_opt(
|
|
||||||
consistency[1]))
|
|
||||||
return tuple(new_value)
|
|
||||||
|
|
||||||
def _impl_convert_requires(self, value, cache):
|
|
||||||
# cache is a dico in import/not a dico in export
|
|
||||||
new_value = []
|
new_value = []
|
||||||
for requires in value:
|
for consistency in self._consistencies:
|
||||||
|
if isinstance(cache, dict):
|
||||||
|
new_value.append((consistency[0], cache[consistency[1]]))
|
||||||
|
else:
|
||||||
|
new_value.append((consistency[0],
|
||||||
|
cache.impl_get_path_by_opt(
|
||||||
|
consistency[1])))
|
||||||
|
if isinstance(cache, dict):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
self._state_consistencies = tuple(new_value)
|
||||||
|
|
||||||
|
def _impl_convert_requires(self, cache):
|
||||||
|
# cache is a dico in import/not a dico in export
|
||||||
|
if self._requires is None:
|
||||||
|
self._state_requires = None
|
||||||
|
else:
|
||||||
|
new_value = []
|
||||||
|
for requires in self._requires:
|
||||||
new_requires = []
|
new_requires = []
|
||||||
for require in requires:
|
for require in requires:
|
||||||
if isinstance(cache, dict):
|
if isinstance(cache, dict):
|
||||||
@ -144,30 +156,36 @@ class BaseOption(BaseInformation):
|
|||||||
new_require.extend(require[1:])
|
new_require.extend(require[1:])
|
||||||
new_requires.append(tuple(new_require))
|
new_requires.append(tuple(new_require))
|
||||||
new_value.append(tuple(new_requires))
|
new_value.append(tuple(new_requires))
|
||||||
return tuple(new_value)
|
if isinstance(cache, dict):
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
self._state_requires = new_value
|
||||||
|
|
||||||
def impl_export(self, descr):
|
def _impl_getstate(self, descr):
|
||||||
descr.impl_build_cache()
|
self._impl_convert_consistencies(descr)
|
||||||
# add _opt_type (not in __slots__)
|
self._impl_convert_requires(descr)
|
||||||
slots = set(['_opt_type'])
|
|
||||||
|
def __getstate__(self, export=False):
|
||||||
|
slots = set()
|
||||||
for subclass in self.__class__.__mro__:
|
for subclass in self.__class__.__mro__:
|
||||||
if subclass is not object:
|
if subclass is not object:
|
||||||
slots.update(subclass.__slots__)
|
slots.update(subclass.__slots__)
|
||||||
slots -= frozenset(['_children', '_readonly', '_cache_paths',
|
slots -= frozenset(['_children', '_cache_paths', '__weakref__'])
|
||||||
'__weakref__'])
|
states = {}
|
||||||
exported_object = {}
|
for slot in slots:
|
||||||
for attr in slots:
|
# remove variable if save variable converted in _state_xxxx variable
|
||||||
|
if '_state' + slot not in slots:
|
||||||
|
if slot.startswith('_state'):
|
||||||
|
# should exists
|
||||||
|
states[slot] = getattr(self, slot)
|
||||||
|
# remove _state_xxx variable
|
||||||
|
self.__delattr__(slot)
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
value = getattr(self, attr)
|
states[slot] = getattr(self, slot)
|
||||||
if value is not None:
|
|
||||||
if attr == '_consistencies':
|
|
||||||
value = self._impl_convert_consistencies(value, descr)
|
|
||||||
elif attr == '_requires':
|
|
||||||
value = self._impl_convert_requires(value, descr)
|
|
||||||
exported_object[attr] = value
|
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
return exported_object
|
return states
|
||||||
|
|
||||||
|
|
||||||
class Option(BaseOption):
|
class Option(BaseOption):
|
||||||
@ -530,7 +548,7 @@ else:
|
|||||||
|
|
||||||
|
|
||||||
class SymLinkOption(BaseOption):
|
class SymLinkOption(BaseOption):
|
||||||
__slots__ = ('_name', '_opt')
|
__slots__ = ('_name', '_opt', '_state_opt')
|
||||||
_opt_type = 'symlink'
|
_opt_type = 'symlink'
|
||||||
|
|
||||||
def __init__(self, name, opt):
|
def __init__(self, name, opt):
|
||||||
@ -548,11 +566,9 @@ class SymLinkOption(BaseOption):
|
|||||||
else:
|
else:
|
||||||
return getattr(self._opt, name)
|
return getattr(self._opt, name)
|
||||||
|
|
||||||
def impl_export(self, descr):
|
def _impl_getstate(self, descr):
|
||||||
export = super(SymLinkOption, self).impl_export(descr)
|
super(SymLinkOption, self)._impl_getstate(descr)
|
||||||
export['_opt'] = descr.impl_get_path_by_opt(self._opt)
|
self._state_opt = descr.impl_get_path_by_opt(self._opt)
|
||||||
del(export['_impl_informations'])
|
|
||||||
return export
|
|
||||||
|
|
||||||
|
|
||||||
class IPOption(Option):
|
class IPOption(Option):
|
||||||
@ -774,8 +790,9 @@ class OptionDescription(BaseOption):
|
|||||||
The `OptionsDescription` objects lives in the `tiramisu.config.Config`.
|
The `OptionsDescription` objects lives in the `tiramisu.config.Config`.
|
||||||
"""
|
"""
|
||||||
__slots__ = ('_name', '_requires', '_cache_paths', '_group_type',
|
__slots__ = ('_name', '_requires', '_cache_paths', '_group_type',
|
||||||
'_properties', '_children', '_consistencies',
|
'_state_group_type', '_properties', '_children',
|
||||||
'_calc_properties', '__weakref__', '_readonly', '_impl_informations')
|
'_consistencies', '_calc_properties', '__weakref__',
|
||||||
|
'_readonly', '_impl_informations')
|
||||||
_opt_type = 'optiondescription'
|
_opt_type = 'optiondescription'
|
||||||
|
|
||||||
def __init__(self, name, doc, children, requires=None, properties=None):
|
def __init__(self, name, doc, children, requires=None, properties=None):
|
||||||
@ -980,23 +997,23 @@ class OptionDescription(BaseOption):
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _impl_convert_group_type(self, value, cache):
|
def _impl_getstate(self, descr=None):
|
||||||
if isinstance(cache, dict):
|
|
||||||
value = str(value)
|
|
||||||
else:
|
|
||||||
value = getattr(groups, value)
|
|
||||||
return value
|
|
||||||
|
|
||||||
def impl_export(self, descr=None):
|
|
||||||
if descr is None:
|
if descr is None:
|
||||||
|
self.impl_build_cache()
|
||||||
descr = self
|
descr = self
|
||||||
export = super(OptionDescription, self).impl_export(descr)
|
super(OptionDescription, self)._impl_getstate(descr)
|
||||||
export['_group_type'] = self._impl_convert_group_type(
|
self._state_group_type = str(self._group_type)
|
||||||
export['_group_type'], descr)
|
|
||||||
export['options'] = []
|
|
||||||
for option in self.impl_getchildren():
|
for option in self.impl_getchildren():
|
||||||
export['options'].append(option.impl_export(descr))
|
option._impl_getstate(descr)
|
||||||
return export
|
|
||||||
|
def __getstate__(self, export=False):
|
||||||
|
if not export:
|
||||||
|
self._impl_getstate()
|
||||||
|
states = super(OptionDescription, self).__getstate__(True)
|
||||||
|
states['_state_children'] = []
|
||||||
|
for option in self.impl_getchildren():
|
||||||
|
states['_state_children'].append(option.__getstate__(True))
|
||||||
|
return states
|
||||||
|
|
||||||
|
|
||||||
def validate_requires_arg(requires, name):
|
def validate_requires_arg(requires, name):
|
||||||
|
Loading…
Reference in New Issue
Block a user