remove pickle dump/load support

This commit is contained in:
2017-07-21 18:46:11 +02:00
parent ddaadb0701
commit 32e88299e2
13 changed files with 20 additions and 900 deletions

View File

@ -256,156 +256,8 @@ class BaseOption(Base):
"""
__slots__ = tuple()
# ____________________________________________________________
# serialize object
def _impl_convert_dependencies(self, descr, load=False):
"""export of the requires during the serialization process
:type descr: :class:`tiramisu.option.OptionDescription`
:param load: `True` if we are at the init of the option description
:type load: bool
"""
if not load and getattr(self, '_dependencies', None) is None:
self._state_dependencies = None
elif load and self._state_dependencies is None:
del(self._state_dependencies)
else:
if load:
self._dependencies = []
for dependency in self._state_dependencies:
option = descr.impl_get_opt_by_path(dependency)
if option.impl_is_optiondescription():
# test if _group_type is already loaded
group_type = getattr(option, '_group_type', undefined)
if group_type is undefined:
group_type = getattr(groups, option._state_group_type)
if group_type == groups.master:
master_path = dependency + '.' + dependency.split('.')[-1]
option = descr.impl_get_opt_by_path(master_path).impl_get_master_slaves()
self._dependencies.append(option)
self._dependencies = tuple(self._dependencies)
del(self._state_dependencies)
else:
self._state_dependencies = []
for dependency in self._dependencies:
if isinstance(dependency, MasterSlaves):
self._state_dependencies.append('.'.join(descr.impl_get_path_by_opt(dependency._p_.master).split('.')[:-1]))
else:
self._state_dependencies.append(descr.impl_get_path_by_opt(dependency))
def _impl_convert_requires(self, descr, load=False):
"""export of the requires during the serialization process
:type descr: :class:`tiramisu.option.OptionDescription`
:param load: `True` if we are at the init of the option description
:type load: bool
"""
if not load and self.impl_getrequires() == []:
self._state_requires = None
elif load and self._state_requires is None:
del(self._state_requires)
else:
if load:
_requires = self._state_requires
else:
_requires = self.impl_getrequires()
new_value = []
for requires in _requires:
new_requires = []
new_req = []
for require in requires:
for req in require[0]:
if load:
new_req.append([(descr.impl_get_opt_by_path(req[0]), req[1])])
else:
new_req.append([(descr.impl_get_path_by_opt(req[0]), req[1])])
new_req.extend(require[1:])
new_requires.append(tuple(new_req))
new_value.append(tuple(new_requires))
if load:
del(self._state_requires)
if new_value != []:
self._requires = tuple(new_value)
else:
self._state_requires = tuple(new_value)
# serialize
def _impl_getstate(self, descr):
"""the under the hood stuff that need to be done
before the serialization.
:param descr: the parent :class:`tiramisu.option.OptionDescription`
"""
self._stated = True
for func in dir(self):
if func.startswith('_impl_convert_'):
getattr(self, func)(descr)
def __getstate__(self, stated=True):
"""special method to enable the serialization with pickle
Usualy, a `__getstate__` method does'nt need any parameter,
but somme under the hood stuff need to be done before this action
:parameter stated: if stated is `True`, the serialization protocol
can be performed, not ready yet otherwise
:parameter type: bool
"""
try:
self._stated
except AttributeError:
raise SystemError(_('cannot serialize Option, '
'only in OptionDescription'))
if isinstance(self, SymLinkOption):
slots = frozenset(['_name', '_state_opt', '_stated'])
else:
slots = self._impl_getattributes()
slots -= frozenset(['_cache_paths', '_cache_consistencies',
'__weakref__'])
states = {}
for slot in slots:
# remove variable if save variable converted
# in _state_xxxx variable
if '_state' + slot not in slots:
try:
if slot.startswith('_state'):
states[slot] = getattr(self, slot)
# remove _state_xxx variable
self.__delattr__(slot)
else:
states[slot] = getattr(self, slot)
except AttributeError:
pass
if not stated:
del(states['_stated'])
return states
# unserialize
def _impl_setstate(self, descr):
"""the under the hood stuff that need to be done
before the serialization.
:type descr: :class:`tiramisu.option.OptionDescription`
"""
for func in dir(self):
if func.startswith('_impl_convert_'):
getattr(self, func)(descr, load=True)
try:
del(self._stated)
except AttributeError:
pass
def __setstate__(self, state):
"""special method that enables us to serialize (pickle)
Usualy, a `__setstate__` method does'nt need any parameter,
but somme under the hood stuff need to be done before this action
:parameter state: a dict is passed to the loads, it is the attributes
of the options object
:type state: dict
"""
for key, value in state.items():
setattr(self, key, value)
def __getstate__(self):
raise NotImplementedError()
def __setattr__(self, name, value):
"""set once and only once some attributes in the option,
@ -417,9 +269,7 @@ class BaseOption(Base):
propertie or "read_only" property)
"""
if name != '_option' and \
not isinstance(value, tuple) and \
not name.startswith('_state') and \
not name == '_sa_instance_state':
not isinstance(value, tuple):
is_readonly = False
# never change _name dans _opt
if name == '_name':
@ -932,41 +782,6 @@ class Option(OnlyOption):
equal_name.append(opt.impl_get_display_name())
return ValueError(msg.format(display_list(list(equal_name))))
# serialize/unserialize
def _impl_convert_consistencies(self, descr, load=False):
"""during serialization process, many things have to be done.
one of them is the localisation of the options.
The paths are set once for all.
:type descr: :class:`tiramisu.option.OptionDescription`
:param load: `True` if we are at the init of the option description
:type load: bool
"""
if not load and not self._has_consistencies():
self._state_consistencies = None
elif load and self._state_consistencies is None:
del(self._state_consistencies)
else:
if load:
consistencies = self._state_consistencies
else:
consistencies = self._get_consistencies()
new_value = []
for consistency in consistencies:
values = []
for obj in consistency[1]:
if load:
values.append(descr.impl_get_opt_by_path(obj))
else:
values.append(descr.impl_get_path_by_opt(obj))
new_value.append((consistency[0], tuple(values), consistency[2]))
if load:
del(self._state_consistencies)
for new_val in new_value:
self._add_consistency(new_val[0], new_val[1], new_val[2])
else:
self._state_consistencies = new_value
def _second_level_validation(self, value, warnings_only):
pass
@ -1139,7 +954,6 @@ def validate_requires_arg(new_option, multi, requires, name):
class SymLinkOption(OnlyOption):
# __slots__ = ('_opt', '_state_opt')
def __init__(self, name, opt):
if not isinstance(opt, Option):
@ -1155,21 +969,11 @@ class SymLinkOption(OnlyOption):
def __getattr__(self, name, context=undefined):
if name in ('_opt', '_readonly', 'impl_getpath', '_name',
'_state_opt', '_impl_setopt'):
'_impl_setopt'):
return object.__getattr__(self, name)
else:
return getattr(self._impl_getopt(), name)
def _impl_getstate(self, descr):
self._stated = True
self._state_opt = descr.impl_get_path_by_opt(self._impl_getopt())
def _impl_setstate(self, descr):
self._impl_setopt(descr.impl_get_opt_by_path(self._state_opt))
del(self._state_opt)
del(self._stated)
self._set_readonly(True)
def impl_get_information(self, key, default=undefined):
return self._impl_getopt().impl_get_information(key, default)
@ -1208,7 +1012,7 @@ class DynSymLinkOption(object):
self._opt = opt
def __getattr__(self, name, context=undefined):
if name in ('_opt', '_readonly', 'impl_getpath', '_name', '_state_opt'):
if name in ('_opt', '_readonly', 'impl_getpath', '_name'):
return object.__getattr__(self, name)
else:
return getattr(self._impl_getopt(), name)

View File

@ -260,58 +260,8 @@ class OptionDescription(BaseOption, StorageOptionDescription):
raise ValueError(_('group_type: {0}'
' not allowed').format(group_type))
def _impl_getstate(self, descr=None):
"""enables us to export into a dict
:param descr: parent :class:`tiramisu.option.OptionDescription`
"""
if descr is None:
self.impl_build_cache_option()
descr = self
super(OptionDescription, self)._impl_getstate(descr)
self._state_group_type = str(self._group_type)
for option in self._impl_getchildren():
option._impl_getstate(descr)
def __getstate__(self):
"""special method to enable the serialization with pickle
"""
stated = True
try:
# the `_state` attribute is a flag that which tells us if
# the serialization can be performed
self._stated
except AttributeError:
# if cannot delete, _impl_getstate never launch
# launch it recursivement
# _stated prevent __getstate__ launch more than one time
# _stated is delete, if re-serialize, re-lauch _impl_getstate
self._impl_getstate()
stated = False
return super(OptionDescription, self).__getstate__(stated)
def _impl_setstate(self, descr=None):
"""enables us to import from a dict
:param descr: parent :class:`tiramisu.option.OptionDescription`
"""
if descr is None:
self._cache_consistencies = None
self.impl_build_cache_option()
descr = self
self._group_type = getattr(groups, self._state_group_type)
if isinstance(self._group_type, groups.MasterGroupType):
MasterSlaves(self.impl_getname(), self.impl_getchildren(),
validate=False)
del(self._state_group_type)
super(OptionDescription, self)._impl_setstate(descr)
for option in self._impl_getchildren(dyn=False):
option._impl_setstate(descr)
def __setstate__(self, state):
super(OptionDescription, self).__setstate__(state)
try:
self._stated
except AttributeError:
self._impl_setstate()
raise NotImplementedError()
def _impl_get_suffixes(self, context):
callback, callback_params = self.impl_get_callback()