add some optimisation
This commit is contained in:
parent
98200ecae5
commit
59f59b0b1b
|
@ -326,7 +326,7 @@ class SubConfig(object):
|
||||||
def _find(self, bytype, byname, byvalue, first, type_='option',
|
def _find(self, bytype, byname, byvalue, first, type_='option',
|
||||||
_subpath=None, check_properties=True, display_error=True,
|
_subpath=None, check_properties=True, display_error=True,
|
||||||
force_permissive=False, only_path=undefined,
|
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
|
convenience method for finding an option that lives only in the subtree
|
||||||
|
|
||||||
|
@ -338,7 +338,8 @@ class SubConfig(object):
|
||||||
if byvalue is undefined:
|
if byvalue is undefined:
|
||||||
return True
|
return True
|
||||||
try:
|
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):
|
if isinstance(value, Multi):
|
||||||
return byvalue in value
|
return byvalue in value
|
||||||
else:
|
else:
|
||||||
|
@ -369,7 +370,8 @@ class SubConfig(object):
|
||||||
if byvalue is undefined and check_properties:
|
if byvalue is undefined and check_properties:
|
||||||
try:
|
try:
|
||||||
value = self.getattr(path,
|
value = self.getattr(path,
|
||||||
force_permissive=force_permissive)
|
force_permissive=force_permissive,
|
||||||
|
_setting_properties=setting_properties)
|
||||||
except PropertiesOptionError: # pragma: optional cover
|
except PropertiesOptionError: # pragma: optional cover
|
||||||
# a property restricts the access of the value
|
# a property restricts the access of the value
|
||||||
continue
|
continue
|
||||||
|
@ -397,7 +399,8 @@ class SubConfig(object):
|
||||||
return find_results
|
return find_results
|
||||||
|
|
||||||
def make_dict(self, flatten=False, _currpath=None, withoption=None,
|
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:
|
"""exports the whole config into a `dict`, for example:
|
||||||
|
|
||||||
>>> print cfg.make_dict()
|
>>> print cfg.make_dict()
|
||||||
|
@ -440,12 +443,16 @@ class SubConfig(object):
|
||||||
if withoption is None and withvalue is not undefined: # pragma: optional cover
|
if withoption is None and withvalue is not undefined: # pragma: optional cover
|
||||||
raise ValueError(_("make_dict can't filtering with value without "
|
raise ValueError(_("make_dict can't filtering with value without "
|
||||||
"option"))
|
"option"))
|
||||||
|
if setting_properties is undefined:
|
||||||
|
setting_properties = self.cfgimpl_get_settings()._getproperties(
|
||||||
|
read_write=False)
|
||||||
if withoption is not None:
|
if withoption is not None:
|
||||||
context = self._cfgimpl_get_context()
|
context = self._cfgimpl_get_context()
|
||||||
for path in context._find(bytype=None, byname=withoption,
|
for path in context._find(bytype=None, byname=withoption,
|
||||||
byvalue=withvalue, first=False,
|
byvalue=withvalue, first=False,
|
||||||
type_='path', _subpath=self.cfgimpl_get_path(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])
|
path = '.'.join(path.split('.')[:-1])
|
||||||
opt = context.unwrap_from_path(path, force_permissive=True)
|
opt = context.unwrap_from_path(path, force_permissive=True)
|
||||||
mypath = self.cfgimpl_get_path()
|
mypath = self.cfgimpl_get_path()
|
||||||
|
@ -462,30 +469,35 @@ class SubConfig(object):
|
||||||
'').format(path, mypath))
|
'').format(path, mypath))
|
||||||
path = path[len(tmypath):]
|
path = path[len(tmypath):]
|
||||||
self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten,
|
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 !
|
#withoption can be set to None below !
|
||||||
if withoption is None:
|
if withoption is None:
|
||||||
for opt in self.cfgimpl_get_description().impl_getchildren():
|
for opt in self.cfgimpl_get_description().impl_getchildren():
|
||||||
path = opt.impl_getname()
|
path = opt.impl_getname()
|
||||||
self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten,
|
self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten,
|
||||||
force_permissive=force_permissive)
|
force_permissive=force_permissive,
|
||||||
|
setting_properties=setting_properties)
|
||||||
if _currpath == []:
|
if _currpath == []:
|
||||||
options = dict(pathsvalues)
|
options = dict(pathsvalues)
|
||||||
return options
|
return options
|
||||||
return pathsvalues
|
return pathsvalues
|
||||||
|
|
||||||
def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten,
|
def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten,
|
||||||
force_permissive=False):
|
setting_properties, force_permissive=False):
|
||||||
try:
|
try:
|
||||||
if opt.impl_is_optiondescription():
|
if opt.impl_is_optiondescription():
|
||||||
pathsvalues += self.getattr(path,
|
pathsvalues += self.getattr(path,
|
||||||
force_permissive=force_permissive).make_dict(
|
force_permissive=force_permissive,
|
||||||
|
_setting_properties=setting_properties).make_dict(
|
||||||
flatten,
|
flatten,
|
||||||
_currpath + path.split('.'),
|
_currpath + path.split('.'),
|
||||||
force_permissive=force_permissive)
|
force_permissive=force_permissive,
|
||||||
|
setting_properties=setting_properties)
|
||||||
else:
|
else:
|
||||||
value = self.getattr(opt.impl_getname(),
|
value = self.getattr(opt.impl_getname(),
|
||||||
force_permissive=force_permissive)
|
force_permissive=force_permissive,
|
||||||
|
_setting_properties=setting_properties)
|
||||||
if flatten:
|
if flatten:
|
||||||
name = opt.impl_getname()
|
name = opt.impl_getname()
|
||||||
else:
|
else:
|
||||||
|
@ -791,12 +803,14 @@ class GroupConfig(_CommonConfig):
|
||||||
pass
|
pass
|
||||||
return ret
|
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:
|
for child in self._impl_children:
|
||||||
if name == child._impl_name:
|
if name == child._impl_name:
|
||||||
return child
|
return child
|
||||||
return super(GroupConfig, self).getattr(name, force_permissive,
|
return super(GroupConfig, self).getattr(name, force_permissive,
|
||||||
validate)
|
validate,
|
||||||
|
_setting_properties=_setting_properties)
|
||||||
|
|
||||||
|
|
||||||
class MetaConfig(GroupConfig):
|
class MetaConfig(GroupConfig):
|
||||||
|
|
|
@ -118,26 +118,25 @@ class MasterSlaves(object):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def getitem(self, values, opt, path, validate, force_permissive,
|
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,
|
slave_value=undefined, setting_properties=undefined,
|
||||||
self_properties=undefined, index=None):
|
self_properties=undefined, index=None):
|
||||||
if self.is_master(opt):
|
if self.is_master(opt):
|
||||||
return self._getmaster(values, opt, path, validate,
|
return self._getmaster(values, opt, path, validate,
|
||||||
force_permissive, force_properties,
|
force_permissive,
|
||||||
validate_properties, slave_path,
|
validate_properties, slave_path,
|
||||||
slave_value, self_properties, index)
|
slave_value, self_properties, index)
|
||||||
else:
|
else:
|
||||||
return self._getslave(values, opt, path, validate,
|
return self._getslave(values, opt, path, validate,
|
||||||
force_permissive, force_properties,
|
force_permissive, trusted_cached_properties,
|
||||||
validate_properties, setting_properties,
|
validate_properties, setting_properties,
|
||||||
self_properties, index)
|
self_properties, index)
|
||||||
|
|
||||||
def _getmaster(self, values, opt, path, validate, force_permissive,
|
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):
|
c_slave_value, self_properties, index):
|
||||||
value = values._get_validated_value(opt, path, validate,
|
value = values._get_validated_value(opt, path, validate,
|
||||||
force_permissive,
|
force_permissive,
|
||||||
force_properties,
|
|
||||||
validate_properties,
|
validate_properties,
|
||||||
self_properties=self_properties,
|
self_properties=self_properties,
|
||||||
index=index)
|
index=index)
|
||||||
|
@ -153,7 +152,7 @@ class MasterSlaves(object):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def _getslave(self, values, opt, path, validate, force_permissive,
|
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):
|
self_properties, index):
|
||||||
"""
|
"""
|
||||||
if master has length 0:
|
if master has length 0:
|
||||||
|
@ -191,7 +190,6 @@ class MasterSlaves(object):
|
||||||
value=multi,
|
value=multi,
|
||||||
path=path,
|
path=path,
|
||||||
force_permissive=force_permissive,
|
force_permissive=force_permissive,
|
||||||
force_properties=force_properties,
|
|
||||||
setting_properties=setting_properties)
|
setting_properties=setting_properties)
|
||||||
else:
|
else:
|
||||||
one_has_value = False
|
one_has_value = False
|
||||||
|
@ -202,7 +200,8 @@ class MasterSlaves(object):
|
||||||
for idx in indexes:
|
for idx in indexes:
|
||||||
try:
|
try:
|
||||||
value = values._get_cached_value(opt, path, validate,
|
value = values._get_cached_value(opt, path, validate,
|
||||||
force_permissive, force_properties,
|
force_permissive,
|
||||||
|
trusted_cached_properties,
|
||||||
validate_properties,
|
validate_properties,
|
||||||
with_meta=master_is_meta,
|
with_meta=master_is_meta,
|
||||||
index=idx,
|
index=idx,
|
||||||
|
|
|
@ -470,7 +470,7 @@ class Settings(object):
|
||||||
|
|
||||||
if force_properties is not None:
|
if force_properties is not None:
|
||||||
forced_properties = copy(setting_properties)
|
forced_properties = copy(setting_properties)
|
||||||
forced_properties.update(force_properties)
|
forced_properties.add('mandatory')
|
||||||
else:
|
else:
|
||||||
forced_properties = setting_properties
|
forced_properties = setting_properties
|
||||||
|
|
||||||
|
|
|
@ -208,12 +208,11 @@ class Values(object):
|
||||||
force_permissive=force_permissive)
|
force_permissive=force_permissive)
|
||||||
|
|
||||||
def _get_cached_value(self, opt, path=None, validate=True,
|
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,
|
validate_properties=True,
|
||||||
setting_properties=undefined, self_properties=undefined,
|
setting_properties=undefined, self_properties=undefined,
|
||||||
index=None, from_masterslave=False, with_meta=True,
|
index=None, from_masterslave=False, with_meta=True,
|
||||||
masterlen=undefined):
|
masterlen=undefined):
|
||||||
untrusted_cached_properties = force_properties is None
|
|
||||||
context = self._getcontext()
|
context = self._getcontext()
|
||||||
if path is None:
|
if path is None:
|
||||||
path = opt.impl_getpath(context)
|
path = opt.impl_getpath(context)
|
||||||
|
@ -232,12 +231,11 @@ class Values(object):
|
||||||
if is_cached:
|
if is_cached:
|
||||||
if opt.impl_is_multi() and not isinstance(value, Multi) and index is None:
|
if opt.impl_is_multi() and not isinstance(value, Multi) and index is None:
|
||||||
value = Multi(value, self.context, opt, path)
|
value = Multi(value, self.context, opt, path)
|
||||||
if not untrusted_cached_properties:
|
if not trusted_cached_properties:
|
||||||
# revalidate properties (because not default properties)
|
# revalidate properties (because not default properties)
|
||||||
context.cfgimpl_get_settings().validate_properties(opt, False, False, value=value,
|
context.cfgimpl_get_settings().validate_properties(opt, False, False, value=value,
|
||||||
path=path,
|
path=path,
|
||||||
force_permissive=force_permissive,
|
force_permissive=force_permissive,
|
||||||
force_properties=force_properties,
|
|
||||||
setting_properties=setting_properties,
|
setting_properties=setting_properties,
|
||||||
self_properties=self_properties,
|
self_properties=self_properties,
|
||||||
index=index)
|
index=index)
|
||||||
|
@ -246,7 +244,7 @@ class Values(object):
|
||||||
val = opt.impl_get_master_slaves().getitem(self, opt, path,
|
val = opt.impl_get_master_slaves().getitem(self, opt, path,
|
||||||
validate,
|
validate,
|
||||||
force_permissive,
|
force_permissive,
|
||||||
force_properties,
|
trusted_cached_properties,
|
||||||
validate_properties,
|
validate_properties,
|
||||||
setting_properties=setting_properties,
|
setting_properties=setting_properties,
|
||||||
index=index,
|
index=index,
|
||||||
|
@ -254,7 +252,6 @@ class Values(object):
|
||||||
else:
|
else:
|
||||||
val = self._get_validated_value(opt, path, validate,
|
val = self._get_validated_value(opt, path, validate,
|
||||||
force_permissive,
|
force_permissive,
|
||||||
force_properties,
|
|
||||||
validate_properties,
|
validate_properties,
|
||||||
setting_properties=setting_properties,
|
setting_properties=setting_properties,
|
||||||
self_properties=self_properties,
|
self_properties=self_properties,
|
||||||
|
@ -263,7 +260,7 @@ class Values(object):
|
||||||
index=index)
|
index=index)
|
||||||
# cache doesn't work with SubMulti yet
|
# cache doesn't work with SubMulti yet
|
||||||
if not isinstance(val, SubMulti) and 'cache' in setting_properties and validate and validate_properties \
|
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 'expire' in setting_properties:
|
||||||
if ntime is None:
|
if ntime is None:
|
||||||
ntime = int(time())
|
ntime = int(time())
|
||||||
|
@ -272,7 +269,7 @@ class Values(object):
|
||||||
return val
|
return val
|
||||||
|
|
||||||
def _get_validated_value(self, opt, path, validate, force_permissive,
|
def _get_validated_value(self, opt, path, validate, force_permissive,
|
||||||
force_properties, validate_properties,
|
validate_properties,
|
||||||
index=None, submulti_index=undefined,
|
index=None, submulti_index=undefined,
|
||||||
with_meta=True, setting_properties=undefined,
|
with_meta=True, setting_properties=undefined,
|
||||||
self_properties=undefined, masterlen=undefined):
|
self_properties=undefined, masterlen=undefined):
|
||||||
|
@ -352,7 +349,6 @@ class Values(object):
|
||||||
setting.validate_properties(opt, False, False, value=val_props,
|
setting.validate_properties(opt, False, False, value=val_props,
|
||||||
path=path,
|
path=path,
|
||||||
force_permissive=force_permissive,
|
force_permissive=force_permissive,
|
||||||
force_properties=force_properties,
|
|
||||||
setting_properties=setting_properties,
|
setting_properties=setting_properties,
|
||||||
self_properties=self_properties,
|
self_properties=self_properties,
|
||||||
index=index)
|
index=index)
|
||||||
|
@ -569,8 +565,8 @@ class Values(object):
|
||||||
"""
|
"""
|
||||||
context = self._getcontext()
|
context = self._getcontext()
|
||||||
settings = context.cfgimpl_get_settings()
|
settings = context.cfgimpl_get_settings()
|
||||||
setting_properties = context.cfgimpl_get_settings()._getproperties(
|
setting_properties = context.cfgimpl_get_settings()._getproperties()
|
||||||
read_write=False)
|
setting_properties.add('mandatory')
|
||||||
|
|
||||||
def _mandatory_warnings(description, currpath=None):
|
def _mandatory_warnings(description, currpath=None):
|
||||||
if currpath is None:
|
if currpath is None:
|
||||||
|
@ -604,7 +600,7 @@ class Values(object):
|
||||||
if 'mandatory' in self_properties:
|
if 'mandatory' in self_properties:
|
||||||
try:
|
try:
|
||||||
self._get_cached_value(true_opt, path=true_path,
|
self._get_cached_value(true_opt, path=true_path,
|
||||||
force_properties=frozenset(('mandatory',)),
|
trusted_cached_properties=False,
|
||||||
force_permissive=force_permissive,
|
force_permissive=force_permissive,
|
||||||
setting_properties=setting_properties,
|
setting_properties=setting_properties,
|
||||||
self_properties=self_properties,
|
self_properties=self_properties,
|
||||||
|
@ -732,7 +728,7 @@ class Multi(list):
|
||||||
def _get_validated_value(self, index):
|
def _get_validated_value(self, index):
|
||||||
values = self._getcontext().cfgimpl_get_values()
|
values = self._getcontext().cfgimpl_get_values()
|
||||||
return values._get_validated_value(self.opt, self.path,
|
return values._get_validated_value(self.opt, self.path,
|
||||||
True, False, None, True,
|
True, False, True,
|
||||||
index=index)
|
index=index)
|
||||||
|
|
||||||
def append(self, value=undefined, force=False, setitem=True, validate=True):
|
def append(self, value=undefined, force=False, setitem=True, validate=True):
|
||||||
|
@ -894,6 +890,6 @@ class SubMulti(Multi):
|
||||||
def _get_validated_value(self, index):
|
def _get_validated_value(self, index):
|
||||||
values = self._getcontext().cfgimpl_get_values()
|
values = self._getcontext().cfgimpl_get_values()
|
||||||
return values._get_validated_value(self.opt, self.path,
|
return values._get_validated_value(self.opt, self.path,
|
||||||
True, False, None, True,
|
True, False, True,
|
||||||
index=index,
|
index=index,
|
||||||
submulti_index=self._index)
|
submulti_index=self._index)
|
||||||
|
|
Loading…
Reference in New Issue