serialize new callback

This commit is contained in:
2013-09-20 23:47:40 +02:00
parent ae4df32d0e
commit 972dff0a1c
2 changed files with 92 additions and 4 deletions

View File

@ -188,9 +188,11 @@ class BaseOption(object):
_list_cons = []
for _con in _cons:
if load:
_list_cons.append(descr.impl_get_opt_by_path(_con))
_list_cons.append(
descr.impl_get_opt_by_path(_con))
else:
_list_cons.append(descr.impl_get_path_by_opt(_con))
_list_cons.append(
descr.impl_get_path_by_opt(_con))
new_value[key].append((key_cons, tuple(_list_cons)))
if load:
del(self._state_consistencies)
@ -322,7 +324,8 @@ class Option(BaseOption):
Reminder: an Option object is **not** a container for the value.
"""
__slots__ = ('_multi', '_validator', '_default_multi', '_default',
'_callback', '_multitype', '_master_slaves', '__weakref__')
'_state_callback', '_callback', '_multitype',
'_master_slaves', '__weakref__')
_empty = ''
def __init__(self, name, doc, default=None, default_multi=None,
@ -558,6 +561,57 @@ class Option(BaseOption):
"must be different as {2} option"
"").format(value, self._name, optname))
def _impl_convert_callbacks(self, descr, load=False):
if not load and self._callback is None:
self._state_callback = None
elif load and self._state_callback is None:
self._callback = None
del(self._state_callback)
else:
if load:
callback, callback_params = self._state_callback
else:
callback, callback_params = self._callback
if callback_params is not None:
cllbck_prms = {}
for key, values in callback_params.items():
vls = []
for value in values:
if isinstance(value, tuple):
if load:
value = (descr.impl_get_opt_by_path(value[0]),
value[1])
else:
value = (descr.impl_get_path_by_opt(value[0]),
value[1])
vls.append(value)
cllbck_prms[key] = tuple(vls)
else:
cllbck_prms = None
if load:
del(self._state_callback)
self._callback = (callback, cllbck_prms)
else:
self._state_callback = (callback, cllbck_prms)
# serialize
def _impl_getstate(self, descr):
"""the under the hood stuff that need to be done
before the serialization.
"""
self._stated = True
self._impl_convert_callbacks(descr)
super(Option, self)._impl_getstate(descr)
# unserialize
def _impl_setstate(self, descr):
"""the under the hood stuff that need to be done
before the serialization.
"""
self._impl_convert_callbacks(descr, load=True)
super(Option, self)._impl_setstate(descr)
class ChoiceOption(Option):
"""represents a choice out of several objects.
@ -1287,4 +1341,5 @@ def validate_callback(callback, callback_params, type_):
if force_permissive not in [True, False]:
raise ValueError(_('{0}_params should have a boolean'
'not a {0} for second argument'
).format(type_, type(force_permissive)))
).format(type_, type(
force_permissive)))