In creole template support of %%lead.follower2 and %%lead['follower2'] notation

%if 'follower1' in %%lead now search if a follower exists has follower
This commit is contained in:
Emmanuel Garette 2021-04-04 19:42:12 +02:00
parent 1249276b9d
commit 3999969aa0
3 changed files with 35 additions and 11 deletions

View File

@ -74,12 +74,21 @@ class RougailLeaderIndex:
def __getattr__(self, name): def __getattr__(self, name):
if name not in self._follower: if name not in self._follower:
raise AttributeError() raise AttributeError(f'unable to find follower "{name}"')
value = self._follower[name] value = self._follower[name]
if isinstance(value, PropertiesOptionError): if isinstance(value, PropertiesOptionError):
raise AttributeError() raise AttributeError(f'unable to access to follower "{name}": {value}')
return value return value
def __getitem__(self, name):
return self.__getattr__(name)
def __contains__(self, name):
if self._follower.__contains__(name):
value = self._follower[name]
return not isinstance(value, PropertiesOptionError)
return False
def __str__(self): def __str__(self):
return str(self._value) return str(self._value)
@ -141,7 +150,7 @@ class RougailLeader:
def __contains__(self, value): def __contains__(self, value):
return self._value.__contains__(value) return self._value.__contains__(value)
async def add_follower(self, async def _add_follower(self,
config, config,
name: str, name: str,
path: str, path: str,
@ -389,7 +398,7 @@ class RougailBaseTemplate:
if is_variable_namespace: if is_variable_namespace:
self.rougail_variables_dict[await suboption.option.name()] = leader self.rougail_variables_dict[await suboption.option.name()] = leader
else: else:
await leader.add_follower(self.config, await leader._add_follower(self.config,
await suboption.option.name(), await suboption.option.name(),
await suboption.option.path(), await suboption.option.path(),
) )

View File

@ -1,2 +1,6 @@
#attr
leader: a
follower2:
#key
leader: a leader: a
follower2: follower2:

View File

@ -1,3 +1,4 @@
#attr
%for %%lead in %%leader %for %%lead in %%leader
leader: %%lead leader: %%lead
%if %%hasattr(%%lead, 'follower1') %if %%hasattr(%%lead, 'follower1')
@ -7,3 +8,13 @@ follower1: %%lead.follower1
follower2: %%lead.follower2 follower2: %%lead.follower2
%end if %end if
%end for %end for
#key
%for %%lead in %%leader
leader: %%lead
%if 'follower1' in %%lead
follower1: %%lead['follower1']
%end if
%if 'follower2' in %%lead
follower2: %%lead['follower2']
%end if
%end for