leader/follower in template

This commit is contained in:
Emmanuel Garette 2019-12-20 11:02:26 +01:00
parent 1b65d22eaa
commit 729b35d372

View file

@ -33,6 +33,7 @@ from Cheetah.Template import Template as ChtTemplate
from Cheetah.NameMapper import NotFound as CheetahNotFound from Cheetah.NameMapper import NotFound as CheetahNotFound
from tiramisu import Config from tiramisu import Config
from tiramisu.error import PropertiesOptionError
from .config import patch_dir from .config import patch_dir
from .error import FileNotFound, TemplateError, TemplateDisabled from .error import FileNotFound, TemplateError, TemplateDisabled
@ -42,7 +43,7 @@ from .i18n import _
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler()) log.addHandler(logging.NullHandler())
class IsDefined(object): class IsDefined:
""" """
filtre permettant de ne pas lever d'exception au cas où filtre permettant de ne pas lever d'exception au cas où
la variable Creole n'est pas définie la variable Creole n'est pas définie
@ -64,7 +65,7 @@ class IsDefined(object):
return varname in self.context return varname in self.context
class CreoleGet(object): class CreoleGet:
def __init__(self, context): def __init__(self, context):
self.context = context self.context = context
@ -123,7 +124,7 @@ class CheetahTemplate(ChtTemplate):
}]) }])
class CreoleMaster(object): class CreoleLeader:
def __init__(self, value, slave=None, index=None): def __init__(self, value, slave=None, index=None):
""" """
On rend la variable itérable pour pouvoir faire: On rend la variable itérable pour pouvoir faire:
@ -148,8 +149,8 @@ class CreoleMaster(object):
""" """
if name in self.slave: if name in self.slave:
value = self.slave[name] value = self.slave[name]
if isinstance(value, Exception): if isinstance(value, PropertiesOptionError):
raise value raise AttributeError()
return value return value
else: else:
return getattr(self._value, name) return getattr(self._value, name)
@ -160,7 +161,7 @@ class CreoleMaster(object):
ret = {} ret = {}
for key, values in self.slave.items(): for key, values in self.slave.items():
ret[key] = values[index] ret[key] = values[index]
return CreoleMaster(self._value[index], ret, index) return CreoleLeader(self._value[index], ret, index)
def __iter__(self): def __iter__(self):
"""Iterate over master.slave. """Iterate over master.slave.
@ -171,7 +172,7 @@ class CreoleMaster(object):
ret = {} ret = {}
for key, values in self.slave.items(): for key, values in self.slave.items():
ret[key] = values[i] ret[key] = values[i]
yield CreoleMaster(self._value[i], ret, i) yield CreoleLeader(self._value[i], ret, i)
def __len__(self): def __len__(self):
"""Delegate to master value """Delegate to master value
@ -179,7 +180,7 @@ class CreoleMaster(object):
return len(self._value) return len(self._value)
def __repr__(self): def __repr__(self):
"""Show CreoleMaster as dictionary. """Show CreoleLeader as dictionary.
The master value is stored under 'value' key. The master value is stored under 'value' key.
The slaves are stored under 'slave' key. The slaves are stored under 'slave' key.
@ -218,31 +219,17 @@ class CreoleMaster(object):
def __contains__(self, item): def __contains__(self, item):
return item in self._value return item in self._value
def add_slave(self, name, value): def add_slave(self, config, name, path):
"""Add a slave variable
Minimal check on type and value of the slave in regards to the
master one.
@param name: name of the slave variable
@type name: C{str}
@param value: value of the slave variable
"""
if isinstance(self._value, list): if isinstance(self._value, list):
if not isinstance(value, list): values = []
raise TypeError for idx in range(len(self._value)):
elif len(value) != len(self._value): try:
raise ValueError(_('length mismatch')) values.append(config.option(path, idx).value.get())
new_value = [] except PropertiesOptionError as err:
for val in value: values.append(err)
if isinstance(val, dict):
new_value.append(ValueError(val['err']))
else: else:
new_value.append(val) raise Exception('hu?')
value = new_value self.slave[name] = values
elif isinstance(value, list):
raise TypeError
self.slave[name] = value
class CreoleTemplateEngine: class CreoleTemplateEngine:
@ -265,36 +252,25 @@ class CreoleTemplateEngine:
eos[func] = getattr(eosfunc, func) eos[func] = getattr(eosfunc, func)
self.eosfunc = eos self.eosfunc = eos
self.creole_variables_dict = {} self.creole_variables_dict = {}
self.load_eole_variables(self.config.option('creole')) self.load_eole_variables(self.config, self.config.option('creole'))
def load_eole_variables(self, optiondescription): def load_eole_variables(self, config, optiondescription):
# remplacement des variables EOLE # remplacement des variables EOLE
for option in optiondescription.list('all'): for option in optiondescription.list('all'):
if option.option.isoptiondescription(): if option.option.isoptiondescription():
if option.option.isleadership(): if option.option.isleadership():
print('leadership') for idx, suboption in enumerate(option.list('all')):
# raise Exception('a faire') if idx == 0:
leader = CreoleLeader(suboption.value.get())
self.creole_variables_dict[suboption.option.name()] = leader
else: else:
self.load_eole_variables(option) leader.add_slave(config,
suboption.option.name(),
suboption.option.path())
else:
self.load_eole_variables(config, option)
else: else:
self.creole_variables_dict[option.option.name()] = option.value.get() self.creole_variables_dict[option.option.name()] = option.value.get()
#if varname.find('.') != -1:
# #support des groupes
# mastername, slavename = varname.split('.')
# if not mastername in self.creole_variables_dict or not \
# isinstance(self.creole_variables_dict [mastername],
# CreoleMaster):
# # Create the master variable
# if mastername in values:
# self.creole_variables_dict[mastername] = CreoleMaster(values[mastername])
# else:
# #only for CreoleLint
# self.creole_variables_dict[mastername] = CreoleMaster(value)
# #test only for CreoleLint
# if mastername != slavename:
# self.creole_variables_dict[mastername].add_slave(slavename, value)
#else:
# self.creole_variables_dict[varname] = value
def patch_template(self, def patch_template(self,
filename: str): filename: str):