NoValueReturn is not needed now + apply_requires is now in settings

This commit is contained in:
2013-04-08 16:05:56 +02:00
parent d8b68fa1ec
commit 67e67a5020
6 changed files with 134 additions and 144 deletions

View File

@ -21,6 +21,9 @@
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from tiramisu.error import (RequirementRecursionError, PropertiesOptionError,
NotFoundError)
class _const:
"""convenient class that emulates a module
@ -136,9 +139,9 @@ populate_multitypes()
#____________________________________________________________
class Setting(object):
"``Config()``'s configuration options"
__slots__ = ('properties', 'permissives', 'owner')
__slots__ = ('properties', 'permissives', 'owner', 'context')
def __init__(self):
def __init__(self, context):
# properties attribute: the name of a property enables this property
# key is None for global properties
self.properties = {None: []} # ['hidden', 'disabled', 'mandatory', 'frozen', 'validator']}
@ -146,24 +149,27 @@ class Setting(object):
self.permissives = {}
# generic owner
self.owner = owners.user
self.context = context
#____________________________________________________________
# properties methods
def has_properties(self, opt=None):
def has_properties(self, opt=None, is_apply_req=True):
"has properties means the Config's properties attribute is not empty"
return bool(len(self.get_properties(opt)))
return bool(len(self.get_properties(opt, is_apply_req)))
def get_properties(self, opt=None):
def get_properties(self, opt=None, is_apply_req=True):
if opt is None:
default = []
else:
if is_apply_req:
apply_requires(opt, self.context)
default = list(opt._properties)
return self.properties.get(opt, default)
def has_property(self, propname, opt=None):
def has_property(self, propname, opt=None, is_apply_req=True):
"""has property propname in the Config's properties attribute
:param property: string wich is the name of the property"""
return propname in self.get_properties(opt)
return propname in self.get_properties(opt, is_apply_req)
def enable_property(self, propname):
"puts property propname in the Config's properties attribute"
@ -192,14 +198,14 @@ class Setting(object):
else:
self.properties[opt] = properties
def add_property(self, propname, opt):
properties = self.get_properties(opt)
def add_property(self, propname, opt, is_apply_req=True):
properties = self.get_properties(opt, is_apply_req)
if not propname in properties:
properties.append(propname)
self.set_properties(properties, opt)
def del_property(self, propname, opt):
properties = self.get_properties(opt)
def del_property(self, propname, opt, is_apply_req=True):
properties = self.get_properties(opt, is_apply_req)
if propname in properties:
properties.remove(propname)
self.set_properties(properties, opt)
@ -241,5 +247,56 @@ class Setting(object):
self.enable_property('hidden')
self.enable_property('disabled')
self.disable_property('mandatory')
self.disable_property('validator')
self.enable_property('validator')
self.disable_property('permissive')
def apply_requires(opt, config):
"carries out the jit (just in time requirements between options"
def build_actions(requires):
"action are hide, show, enable, disable..."
trigger_actions = {}
for require in requires:
action = require[2]
trigger_actions.setdefault(action, []).append(require)
return trigger_actions
#for symlink
if hasattr(opt, '_requires') and opt._requires is not None:
# filters the callbacks
setting = config.cfgimpl_get_settings()
trigger_actions = build_actions(opt._requires)
optpath = config.cfgimpl_get_context().cfgimpl_get_description().get_path_by_opt(opt)
for requires in trigger_actions.values():
matches = False
for require in requires:
if len(require) == 3:
path, expected, action = require
inverse = False
elif len(require) == 4:
path, expected, action, inverse = require
if path == optpath or path.startswith(optpath + '.'):
raise RequirementRecursionError("malformed requirements "
"imbrication detected for option: '{0}' "
"with requirement on: '{1}'".format(optpath, path))
try:
value = config.cfgimpl_get_context()._getattr(path, force_permissive=True)
except PropertiesOptionError, err:
properties = err.proptype
raise NotFoundError("option '{0}' has requirement's property error: "
"{1} {2}".format(opt._name, path, properties))
except AttributeError:
raise NotFoundError("required option not found: "
"{0}".format(path))
if value == expected:
if inverse:
setting.del_property(action, opt, False)
else:
setting.add_property(action, opt, False)
matches = True
#FIXME optimisation : fait un double break non ? voire un return
# no requirement has been triggered, then just reverse the action
if not matches:
if inverse:
setting.add_property(action, opt, False)
else:
setting.del_property(action, opt, False)