reduce memory usage

This commit is contained in:
2014-10-25 22:11:31 +02:00
parent 58c22aa70f
commit 9f3d676280
9 changed files with 427 additions and 294 deletions

View File

@ -25,49 +25,188 @@ from tiramisu.error import ConfigError
#____________________________________________________________
#
# Base
#('_name', '_informations', '_multi', '_multitype', '_warnings_only', '_extra', '_readonly', '_subdyn)
class StorageBase(object):
__slots__ = ('_name', '_requires', '_properties', '_readonly',
'_calc_properties', '_informations',
'_state_readonly', '_state_requires', '_stated',
'_multi', '_validator', '_validator_params',
'_default', '_default_multi', '_state_callback', '_callback',
'_state_callback_params', '_callback_params', '_multitype',
'_consistencies', '_warnings_only', '_master_slaves',
'_state_consistencies', '_extra', '_subdyn', '__weakref__',
'_state_master_slaves', '_choice_values',
'_choice_values_params')
__slots__ = ('_name',
'_informations',
'_multi',
'_extra',
'_warnings_only',
#valeur
'_default',
'_default_multi',
#calcul
'_subdyn',
'_requires',
'_properties',
'_calc_properties',
'_val_call',
#'_validator',
#'_validator_params',
#'_callback',
#'_callback_params',
'_consistencies',
'_master_slaves',
'_choice_values',
'_choice_values_params',
#autre
'_state_master_slaves',
'_state_callback',
'_state_callback_params',
'_state_requires',
'_stated',
'_state_consistencies',
'_state_informations',
'_state_readonly',
'__weakref__'
)
def __init__(self):
try:
self._subdyn
except AttributeError:
self._subdyn = None
try:
self._consistencies
except AttributeError:
self._consistencies = []
try:
self._callback
except AttributeError:
self._callback = None
try:
self._callback_params
except AttributeError:
self._callback_params = {}
try:
self._validator
except AttributeError:
self._validator = None
try:
self._validator_params
except AttributeError:
self._validator_params = None
def __init__(self, name, multi, warnings_only, doc, extra):
self._name = name
if doc is not undefined:
self._informations = {'doc': doc}
if multi != 1:
self._multi = multi
if extra is not None:
self._extra = extra
if warnings_only is True:
self._warnings_only = warnings_only
def _set_default_values(self, default, default_multi):
if self.impl_is_multi() and default is None:
default = []
self.impl_validate(default)
if ((self.impl_is_multi() and default != tuple()) or
(not self.impl_is_multi() and default is not None)):
if self.impl_is_multi():
default = tuple(default)
self._default = default
if self.impl_is_multi() and default_multi is not None:
try:
self._validate(default_multi)
except ValueError as err: # pragma: optional cover
raise ValueError(_("invalid default_multi value {0} "
"for option {1}: {2}").format(
str(default_multi),
self.impl_getname(), err))
self._default_multi = default_multi
# information
def impl_set_information(self, key, value):
"""updates the information's attribute
(which is a dictionary)
:param key: information's key (ex: "help", "doc"
:param value: information's value (ex: "the help string")
"""
self._informations[key] = value
def impl_get_information(self, key, default=undefined):
"""retrieves one information's item
:param key: the item string (ex: "help")
"""
error = False
dico = self._informations
if dico is None or isinstance(dico, str) or isinstance(dico, unicode):
if key == 'doc':
return dico
error = True
elif isinstance(dico, tuple):
try:
return dico[1][dico[0].index(key)]
except AttributeError:
if default is not undefined:
return default
error = True
else:
if default is not undefined:
return self._informations.get(key, default)
try:
return self._informations[key]
except KeyError: # pragma: optional cover
error = True
if error:
raise ValueError(_("information's item not found: {0}").format(
key))
def _add_consistency(self, func, all_cons_opts, params):
self._consistencies.append((func, all_cons_opts, params))
cons = (func, all_cons_opts, params)
try:
self._consistencies.append(cons)
except AttributeError:
self._consistencies = [cons]
def _get_consistencies(self):
return self._consistencies
try:
return self._consistencies
except AttributeError:
return tuple()
def _set_callback(self, callback, callback_params):
if callback_params is None or callback_params == {}:
val_call = (callback,)
else:
val_call = tuple([callback, callback_params])
try:
self._val_call = (self._val_call[0], val_call)
except AttributeError:
self._val_call = (None, val_call)
def impl_get_callback(self):
default = (None, {})
try:
call = self._val_call[1]
except (AttributeError, IndexError):
ret_call = default
else:
if call is None:
ret_call = default
else:
if len(call) == 1:
ret_call = (call[0], default[1])
else:
ret_call = call
return ret_call
def impl_get_calc_properties(self):
try:
return self._calc_properties
except AttributeError:
return frozenset()
def impl_getrequires(self):
try:
return self._requires
except AttributeError:
return []
def _set_validator(self, validator, validator_params):
if validator_params is None:
val_call = (validator,)
else:
val_call = (validator, validator_params)
try:
self._val_call = (val_call, self._val_call[1])
except (AttributeError, IndexError):
self._val_call = (val_call, None)
def impl_get_validator(self):
default = (None, {})
try:
val = self._val_call[0]
except (AttributeError, IndexError):
ret_val = default
else:
if val is None:
ret_val = default
else:
if len(val) == 1:
ret_val = (val[0], default[1])
else:
ret_val = val
return ret_val
def _get_id(self):
return id(self)
@ -75,9 +214,103 @@ class StorageBase(object):
def _impl_getsubdyn(self):
return self._subdyn
def _set_readonly(self):
if not self.impl_is_readonly():
dico = self._informations
if not (dico is None or isinstance(dico, str) or isinstance(dico, unicode)):
keys = tuple(dico.keys())
if keys == ('doc',):
dico = dico['doc']
else:
dico = tuple([tuple(dico.keys()), tuple(dico.values())])
self._informations = dico
try:
extra = self._extra
self._extra = tuple([tuple(extra.keys()), tuple(extra.values())])
except AttributeError:
pass
def _impl_setsubdyn(self, subdyn):
self._subdyn = subdyn
def _impl_convert_informations(self, descr, load=False):
if not load:
infos = self._informations
if isinstance(infos, tuple):
self._state_informations = {}
for key, value in infos:
self._state_informations[key] = value
elif isinstance(infos, str) or isinstance(infos, unicode):
self._state_informations = {'doc': infos}
else:
self._state_informations = infos
self._state_readonly = self.impl_is_readonly()
else:
try:
self._informations = self._state_informations
del(self._state_informations)
except AttributeError:
pass
if self._state_readonly:
self._set_readonly()
del(self._state_readonly)
def impl_is_readonly(self):
try:
return not isinstance(self._informations, dict)
except AttributeError:
return False
def impl_getname(self):
return self._name
def impl_is_multi(self):
try:
_multi = self._multi
except AttributeError:
_multi = 1
return _multi == 0 or _multi == 2
def impl_is_submulti(self):
try:
_multi = self._multi
except IndexError:
_multi = 1
return _multi == 2
def _get_extra(self, key):
extra = self._extra
if isinstance(extra, tuple):
return extra[1][extra[0].index(key)]
else:
return extra[key]
def _is_warnings_only(self):
try:
return self._warnings_only
except:
return False
def impl_getdefault(self):
"accessing the default value"
try:
ret = self._default
if self.impl_is_multi():
return list(ret)
return ret
except AttributeError:
if self.impl_is_multi():
return []
return None
def impl_getdefault_multi(self):
"accessing the default value for a multi"
if self.impl_is_multi():
try:
return self._default_multi
except (AttributeError, IndexError):
pass
def commit(self):
pass
@ -86,7 +319,8 @@ class StorageOptionDescription(StorageBase):
__slots__ = ('_children', '_cache_paths', '_cache_consistencies',
'_group_type', '_is_build_cache', '_state_group_type')
def __init__(self):
def __init__(self, name, multi, warnings_only, doc, extra):
super(StorageOptionDescription, self).__init__(name, multi, warnings_only, doc, None)
self._cache_paths = None
def _add_children(self, child_names, children):
@ -128,7 +362,7 @@ class StorageOptionDescription(StorageBase):
cache_path = []
cache_option = []
for option in self._impl_getchildren(dyn=False):
attr = option._name
attr = option.impl_getname()
path = str('.'.join(_currpath + [attr]))
cache_option.append(option)
cache_path.append(path)
@ -272,7 +506,7 @@ class StorageOptionDescription(StorageBase):
if error:
raise AttributeError(_('unknown Option {0} '
'in OptionDescription {1}'
'').format(name, self._name))
'').format(name, self.impl_getname()))
def _get_force_store_value(self):
#FIXME faire des tests (notamment pas ajouter à un config)

View File

@ -254,7 +254,6 @@ class _Base(SqlAlchemyBase):
_reqs = relationship("_Require", collection_class=list)
_requires = association_proxy("_reqs", "requires", getset_factory=load_requires)
_multi = Column(Integer)
_multitype = Column(String)
######
_callback = Column(PickleType)
_call_params = relationship('_CallbackParam',