leader/follower in template

This commit is contained in:
Emmanuel Garette 2019-12-20 11:02:26 +01:00
parent 1b65d22eaa
commit 729b35d372
1 changed files with 38 additions and 62 deletions

View File

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