diff --git a/src/rougail/template/base.py b/src/rougail/template/base.py index 5cad8ff5..9b335b48 100644 --- a/src/rougail/template/base.py +++ b/src/rougail/template/base.py @@ -74,12 +74,21 @@ class RougailLeaderIndex: def __getattr__(self, name): if name not in self._follower: - raise AttributeError() + raise AttributeError(f'unable to find follower "{name}"') value = self._follower[name] if isinstance(value, PropertiesOptionError): - raise AttributeError() + raise AttributeError(f'unable to access to follower "{name}": {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): return str(self._value) @@ -141,11 +150,11 @@ class RougailLeader: def __contains__(self, value): return self._value.__contains__(value) - async def add_follower(self, - config, - name: str, - path: str, - ): + async def _add_follower(self, + config, + name: str, + path: str, + ): """Add a new follower """ self._follower[name] = [] @@ -389,10 +398,10 @@ class RougailBaseTemplate: if is_variable_namespace: self.rougail_variables_dict[await suboption.option.name()] = leader else: - await leader.add_follower(self.config, - await suboption.option.name(), - await suboption.option.path(), - ) + await leader._add_follower(self.config, + await suboption.option.name(), + await suboption.option.path(), + ) variables[leader_name] = leader else: if is_service_namespace == 'root': diff --git a/tests/dictionaries/40ifin_leadershipauto/result/etc/mailname b/tests/dictionaries/40ifin_leadershipauto/result/etc/mailname index 155aa30c..6da0a4c2 100644 --- a/tests/dictionaries/40ifin_leadershipauto/result/etc/mailname +++ b/tests/dictionaries/40ifin_leadershipauto/result/etc/mailname @@ -1,2 +1,6 @@ +#attr +leader: a +follower2: +#key leader: a follower2: diff --git a/tests/dictionaries/40ifin_leadershipauto/tmpl/mailname b/tests/dictionaries/40ifin_leadershipauto/tmpl/mailname index 43dbe7d1..507012d7 100644 --- a/tests/dictionaries/40ifin_leadershipauto/tmpl/mailname +++ b/tests/dictionaries/40ifin_leadershipauto/tmpl/mailname @@ -1,3 +1,4 @@ +#attr %for %%lead in %%leader leader: %%lead %if %%hasattr(%%lead, 'follower1') @@ -7,3 +8,13 @@ follower1: %%lead.follower1 follower2: %%lead.follower2 %end if %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