add some optimisation

This commit is contained in:
Emmanuel Garette 2015-12-14 23:37:15 +01:00
parent 98200ecae5
commit 59f59b0b1b
4 changed files with 45 additions and 36 deletions

View File

@ -326,7 +326,7 @@ class SubConfig(object):
def _find(self, bytype, byname, byvalue, first, type_='option',
_subpath=None, check_properties=True, display_error=True,
force_permissive=False, only_path=undefined,
only_option=undefined):
only_option=undefined, setting_properties=undefined):
"""
convenience method for finding an option that lives only in the subtree
@ -338,7 +338,8 @@ class SubConfig(object):
if byvalue is undefined:
return True
try:
value = self.getattr(path, force_permissive=force_permissive)
value = self.getattr(path, force_permissive=force_permissive,
_setting_properties=setting_properties)
if isinstance(value, Multi):
return byvalue in value
else:
@ -369,7 +370,8 @@ class SubConfig(object):
if byvalue is undefined and check_properties:
try:
value = self.getattr(path,
force_permissive=force_permissive)
force_permissive=force_permissive,
_setting_properties=setting_properties)
except PropertiesOptionError: # pragma: optional cover
# a property restricts the access of the value
continue
@ -397,7 +399,8 @@ class SubConfig(object):
return find_results
def make_dict(self, flatten=False, _currpath=None, withoption=None,
withvalue=undefined, force_permissive=False):
withvalue=undefined, force_permissive=False,
setting_properties=undefined):
"""exports the whole config into a `dict`, for example:
>>> print cfg.make_dict()
@ -440,12 +443,16 @@ class SubConfig(object):
if withoption is None and withvalue is not undefined: # pragma: optional cover
raise ValueError(_("make_dict can't filtering with value without "
"option"))
if setting_properties is undefined:
setting_properties = self.cfgimpl_get_settings()._getproperties(
read_write=False)
if withoption is not None:
context = self._cfgimpl_get_context()
for path in context._find(bytype=None, byname=withoption,
byvalue=withvalue, first=False,
type_='path', _subpath=self.cfgimpl_get_path(False),
force_permissive=force_permissive):
force_permissive=force_permissive,
setting_properties=setting_properties):
path = '.'.join(path.split('.')[:-1])
opt = context.unwrap_from_path(path, force_permissive=True)
mypath = self.cfgimpl_get_path()
@ -462,30 +469,35 @@ class SubConfig(object):
'').format(path, mypath))
path = path[len(tmypath):]
self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten,
force_permissive=force_permissive)
force_permissive=force_permissive,
setting_properties=setting_properties)
#withoption can be set to None below !
if withoption is None:
for opt in self.cfgimpl_get_description().impl_getchildren():
path = opt.impl_getname()
self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten,
force_permissive=force_permissive)
force_permissive=force_permissive,
setting_properties=setting_properties)
if _currpath == []:
options = dict(pathsvalues)
return options
return pathsvalues
def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten,
force_permissive=False):
setting_properties, force_permissive=False):
try:
if opt.impl_is_optiondescription():
pathsvalues += self.getattr(path,
force_permissive=force_permissive).make_dict(
force_permissive=force_permissive,
_setting_properties=setting_properties).make_dict(
flatten,
_currpath + path.split('.'),
force_permissive=force_permissive)
force_permissive=force_permissive,
setting_properties=setting_properties)
else:
value = self.getattr(opt.impl_getname(),
force_permissive=force_permissive)
force_permissive=force_permissive,
_setting_properties=setting_properties)
if flatten:
name = opt.impl_getname()
else:
@ -791,12 +803,14 @@ class GroupConfig(_CommonConfig):
pass
return ret
def getattr(self, name, force_permissive=False, validate=True):
def getattr(self, name, force_permissive=False, validate=True,
_setting_properties=undefined):
for child in self._impl_children:
if name == child._impl_name:
return child
return super(GroupConfig, self).getattr(name, force_permissive,
validate)
validate,
_setting_properties=_setting_properties)
class MetaConfig(GroupConfig):

View File

@ -118,26 +118,25 @@ class MasterSlaves(object):
pass
def getitem(self, values, opt, path, validate, force_permissive,
force_properties, validate_properties, slave_path=undefined,
trusted_cached_properties, validate_properties, slave_path=undefined,
slave_value=undefined, setting_properties=undefined,
self_properties=undefined, index=None):
if self.is_master(opt):
return self._getmaster(values, opt, path, validate,
force_permissive, force_properties,
force_permissive,
validate_properties, slave_path,
slave_value, self_properties, index)
else:
return self._getslave(values, opt, path, validate,
force_permissive, force_properties,
force_permissive, trusted_cached_properties,
validate_properties, setting_properties,
self_properties, index)
def _getmaster(self, values, opt, path, validate, force_permissive,
force_properties, validate_properties, c_slave_path,
validate_properties, c_slave_path,
c_slave_value, self_properties, index):
value = values._get_validated_value(opt, path, validate,
force_permissive,
force_properties,
validate_properties,
self_properties=self_properties,
index=index)
@ -153,7 +152,7 @@ class MasterSlaves(object):
return value
def _getslave(self, values, opt, path, validate, force_permissive,
force_properties, validate_properties, setting_properties,
trusted_cached_properties, validate_properties, setting_properties,
self_properties, index):
"""
if master has length 0:
@ -191,7 +190,6 @@ class MasterSlaves(object):
value=multi,
path=path,
force_permissive=force_permissive,
force_properties=force_properties,
setting_properties=setting_properties)
else:
one_has_value = False
@ -202,7 +200,8 @@ class MasterSlaves(object):
for idx in indexes:
try:
value = values._get_cached_value(opt, path, validate,
force_permissive, force_properties,
force_permissive,
trusted_cached_properties,
validate_properties,
with_meta=master_is_meta,
index=idx,

View File

@ -470,7 +470,7 @@ class Settings(object):
if force_properties is not None:
forced_properties = copy(setting_properties)
forced_properties.update(force_properties)
forced_properties.add('mandatory')
else:
forced_properties = setting_properties

View File

@ -208,12 +208,11 @@ class Values(object):
force_permissive=force_permissive)
def _get_cached_value(self, opt, path=None, validate=True,
force_permissive=False, force_properties=None,
force_permissive=False, trusted_cached_properties=True,
validate_properties=True,
setting_properties=undefined, self_properties=undefined,
index=None, from_masterslave=False, with_meta=True,
masterlen=undefined):
untrusted_cached_properties = force_properties is None
context = self._getcontext()
if path is None:
path = opt.impl_getpath(context)
@ -232,12 +231,11 @@ class Values(object):
if is_cached:
if opt.impl_is_multi() and not isinstance(value, Multi) and index is None:
value = Multi(value, self.context, opt, path)
if not untrusted_cached_properties:
if not trusted_cached_properties:
# revalidate properties (because not default properties)
context.cfgimpl_get_settings().validate_properties(opt, False, False, value=value,
path=path,
force_permissive=force_permissive,
force_properties=force_properties,
setting_properties=setting_properties,
self_properties=self_properties,
index=index)
@ -246,7 +244,7 @@ class Values(object):
val = opt.impl_get_master_slaves().getitem(self, opt, path,
validate,
force_permissive,
force_properties,
trusted_cached_properties,
validate_properties,
setting_properties=setting_properties,
index=index,
@ -254,7 +252,6 @@ class Values(object):
else:
val = self._get_validated_value(opt, path, validate,
force_permissive,
force_properties,
validate_properties,
setting_properties=setting_properties,
self_properties=self_properties,
@ -263,7 +260,7 @@ class Values(object):
index=index)
# cache doesn't work with SubMulti yet
if not isinstance(val, SubMulti) and 'cache' in setting_properties and validate and validate_properties \
and force_permissive is False and force_properties is None:
and force_permissive is False and trusted_cached_properties is True:
if 'expire' in setting_properties:
if ntime is None:
ntime = int(time())
@ -272,7 +269,7 @@ class Values(object):
return val
def _get_validated_value(self, opt, path, validate, force_permissive,
force_properties, validate_properties,
validate_properties,
index=None, submulti_index=undefined,
with_meta=True, setting_properties=undefined,
self_properties=undefined, masterlen=undefined):
@ -352,7 +349,6 @@ class Values(object):
setting.validate_properties(opt, False, False, value=val_props,
path=path,
force_permissive=force_permissive,
force_properties=force_properties,
setting_properties=setting_properties,
self_properties=self_properties,
index=index)
@ -569,8 +565,8 @@ class Values(object):
"""
context = self._getcontext()
settings = context.cfgimpl_get_settings()
setting_properties = context.cfgimpl_get_settings()._getproperties(
read_write=False)
setting_properties = context.cfgimpl_get_settings()._getproperties()
setting_properties.add('mandatory')
def _mandatory_warnings(description, currpath=None):
if currpath is None:
@ -604,7 +600,7 @@ class Values(object):
if 'mandatory' in self_properties:
try:
self._get_cached_value(true_opt, path=true_path,
force_properties=frozenset(('mandatory',)),
trusted_cached_properties=False,
force_permissive=force_permissive,
setting_properties=setting_properties,
self_properties=self_properties,
@ -732,7 +728,7 @@ class Multi(list):
def _get_validated_value(self, index):
values = self._getcontext().cfgimpl_get_values()
return values._get_validated_value(self.opt, self.path,
True, False, None, True,
True, False, True,
index=index)
def append(self, value=undefined, force=False, setitem=True, validate=True):
@ -894,6 +890,6 @@ class SubMulti(Multi):
def _get_validated_value(self, index):
values = self._getcontext().cfgimpl_get_values()
return values._get_validated_value(self.opt, self.path,
True, False, None, True,
True, False, True,
index=index,
submulti_index=self._index)