add some optimisations

This commit is contained in:
2015-12-22 22:06:14 +01:00
parent 93ce93e529
commit df233d3165
6 changed files with 123 additions and 106 deletions

View File

@ -20,6 +20,8 @@
from ...i18n import _
from ...setting import undefined
from ...error import ConfigError
static_tuple = tuple()
static_set = frozenset()
#____________________________________________________________
@ -55,6 +57,7 @@ class StorageBase(object):
'_stated',
'_state_consistencies',
'_state_informations',
'_state_extra',
'_state_readonly',
'__weakref__'
)
@ -125,7 +128,7 @@ class StorageBase(object):
"""
error = False
dico = self._informations
if dico is None or isinstance(dico, str) or isinstance(dico, unicode):
if isinstance(dico, str) or isinstance(dico, unicode):
if key == 'doc':
return dico
if default is not undefined:
@ -161,10 +164,7 @@ class StorageBase(object):
self._consistencies.pop(-1)
def _get_consistencies(self):
try:
return self._consistencies
except AttributeError:
return tuple()
return getattr(self, '_consistencies', static_tuple)
def _set_callback(self, callback, callback_params):
if callback_params is None or callback_params == {}:
@ -193,16 +193,10 @@ class StorageBase(object):
return ret_call
def impl_get_calc_properties(self):
try:
return self._calc_properties
except AttributeError:
return frozenset()
return getattr(self, '_calc_properties', static_set)
def impl_getrequires(self):
try:
return self._requires
except AttributeError:
return []
return getattr(self, '_requires', static_tuple)
def _set_validator(self, validator, validator_params):
if validator_params is None:
@ -239,22 +233,20 @@ class StorageBase(object):
def _impl_getopt(self):
return self._opt
def _set_readonly(self):
def _set_readonly(self, has_extra):
if not self.impl_is_readonly():
dico = self._informations
_setattr = object.__setattr__
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())])
_setattr(self, '_informations', dico)
try:
extra = self._extra
_setattr(self, '_extra', tuple([tuple(extra.keys()), tuple(extra.values())]))
except AttributeError:
pass
dico = self._informations
keys = tuple(dico.keys())
if len(keys) == 1:
dico = dico['doc']
else:
dico = tuple([keys, tuple(dico.values())])
_setattr(self, '_informations', dico)
if has_extra:
extra = getattr(self, '_extra', None)
if extra is not None:
_setattr(self, '_extra', tuple([tuple(extra.keys()), tuple(extra.values())]))
def _impl_setsubdyn(self, subdyn):
self._subdyn = subdyn
@ -268,8 +260,7 @@ class StorageBase(object):
if isinstance(infos, tuple):
self._state_informations = {}
for idx, key in enumerate(infos[0]):
value = infos[1][idx]
self._state_informations[key] = value
self._state_informations[key] = infos[1][idx]
elif isinstance(infos, str) or isinstance(infos, unicode):
self._state_informations = {'doc': infos}
else:
@ -282,9 +273,26 @@ class StorageBase(object):
except AttributeError:
pass
if self._state_readonly:
self._set_readonly()
self._set_readonly(True)
del(self._state_readonly)
def _impl_convert_extra(self, descr, load=False):
if not load:
try:
extra = self._extra
if isinstance(extra, tuple):
self._state_extra = {}
for idx, key in enumerate(extra[0]):
self._state_extra[key] = extra[1][idx]
except AttributeError:
pass
else:
try:
self._extra = self._state_extra
del(self._state_extra)
except AttributeError:
pass
def _impl_getattributes(self):
slots = set()
for subclass in self.__class__.__mro__:
@ -302,10 +310,7 @@ class StorageBase(object):
return self._name
def impl_is_multi(self):
try:
_multi = self._multi
except AttributeError:
return False
_multi = getattr(self, '_multi', 1)
return _multi != 1
def impl_is_submulti(self):
@ -361,21 +366,20 @@ class StorageBase(object):
class StorageOptionDescription(StorageBase):
__slots__ = ('_children', '_cache_paths', '_cache_consistencies',
'_group_type', '_is_build_cache', '_state_group_type')
'_group_type', '_state_group_type')
def __init__(self, name, multi, warnings_only, doc, extra):
super(StorageOptionDescription, self).__init__(name, multi,
warnings_only, doc,
None, undefined,
undefined, undefined)
self._cache_paths = None
def _add_children(self, child_names, children):
_setattr = object.__setattr__
_setattr(self, '_children', (tuple(child_names), tuple(children)))
def impl_already_build_caches(self):
return self._is_build_cache
return getattr(self, '_cache_paths', None) is not None
def impl_get_opt_by_path(self, path):
try:
@ -384,7 +388,7 @@ class StorageOptionDescription(StorageBase):
raise AttributeError(_('no option for path {0}').format(path))
def impl_get_path_by_opt(self, opt):
if self._cache_paths is None:
if getattr(self, '_cache_paths', None) is None:
raise ConfigError(_('use impl_get_path_by_opt only with root OptionDescription'))
try:
return self._cache_paths[1][self._cache_paths[0].index(opt)]
@ -396,12 +400,8 @@ class StorageOptionDescription(StorageBase):
def impl_build_cache_option(self, _currpath=None, cache_path=None,
cache_option=None):
_setattr = object.__setattr__
try:
self._cache_paths
except AttributeError:
_setattr(self, '_cache_paths', None)
if _currpath is None and self._cache_paths is not None: # pragma: optional cover
if _currpath is None and getattr(self, '_cache_paths', None) is not None:
# cache already set
return
if _currpath is None:
@ -423,8 +423,8 @@ class StorageOptionDescription(StorageBase):
cache_option)
_currpath.pop()
if save:
_setattr = object.__setattr__
_setattr(self, '_cache_paths', (tuple(cache_option), tuple(cache_path)))
_setattr(self, '_is_build_cache', True)
def impl_get_options_paths(self, bytype, byname, _subpath, only_first, context):
find_results = []