From 2eb9f74117c69a854924407a1ba5a7ab06935312 Mon Sep 17 00:00:00 2001 From: jtdub Date: Wed, 18 Dec 2024 19:26:41 -0600 Subject: [PATCH 1/2] add ability to set sectional exit text at the parent level --- hier_config/child.py | 11 ++++++++++- hier_config/models.py | 1 + hier_config/platforms/cisco_xr/driver.py | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/hier_config/child.py b/hier_config/child.py index 956aed92..597df6aa 100644 --- a/hier_config/child.py +++ b/hier_config/child.py @@ -114,7 +114,8 @@ def lines(self, *, sectional_exiting: bool = False) -> Iterable[str]: yield from child.lines(sectional_exiting=sectional_exiting) if sectional_exiting and (exit_text := self.sectional_exit): - yield " " * self.driver.rules.indentation * self.depth() + exit_text + depth = self.depth() - 1 if self.sectional_exit_text_parent_level else self.depth() + yield " " * self.driver.rules.indentation * depth + exit_text @property def sectional_exit(self) -> Optional[str]: @@ -129,6 +130,14 @@ def sectional_exit(self) -> Optional[str]: return "exit" + @property + def sectional_exit_text_parent_level(self) -> Optional[bool]: + for rule in self.driver.rules.sectional_exiting: + if self.is_lineage_match(rule.match_rules): + return rule.exit_text_parent_level + + return None + def delete_sectional_exit(self) -> None: try: potential_exit = self.children[-1] diff --git a/hier_config/models.py b/hier_config/models.py index fe1fd229..29cdc333 100644 --- a/hier_config/models.py +++ b/hier_config/models.py @@ -35,6 +35,7 @@ class TagRule(BaseModel): class SectionalExitingRule(BaseModel): match_rules: tuple[MatchRule, ...] exit_text: str + exit_text_parent_level: bool = False class SectionalOverwriteRule(BaseModel): diff --git a/hier_config/platforms/cisco_xr/driver.py b/hier_config/platforms/cisco_xr/driver.py index 6df3deb2..6f414a06 100644 --- a/hier_config/platforms/cisco_xr/driver.py +++ b/hier_config/platforms/cisco_xr/driver.py @@ -40,30 +40,37 @@ def _instantiate_rules() -> HConfigDriverRules: SectionalExitingRule( match_rules=(MatchRule(startswith="route-policy"),), exit_text="end-policy", + exit_text_parent_level=True, ), SectionalExitingRule( match_rules=(MatchRule(startswith="prefix-set"),), exit_text="end-set", + exit_text_parent_level=True, ), SectionalExitingRule( match_rules=(MatchRule(startswith="policy-map"),), exit_text="end-policy-map", + exit_text_parent_level=True, ), SectionalExitingRule( match_rules=(MatchRule(startswith="class-map"),), exit_text="end-class-map", + exit_text_parent_level=True, ), SectionalExitingRule( match_rules=(MatchRule(startswith="community-set"),), exit_text="end-set", + exit_text_parent_level=True, ), SectionalExitingRule( match_rules=(MatchRule(startswith="extcommunity-set"),), exit_text="end-set", + exit_text_parent_level=True, ), SectionalExitingRule( match_rules=(MatchRule(startswith="template"),), exit_text="end-template", + exit_text_parent_level=True, ), SectionalExitingRule( match_rules=(MatchRule(startswith="interface"),), From a37b7d24b4617d4d7fa1cebf2358894c66acedc9 Mon Sep 17 00:00:00 2001 From: jtdub Date: Wed, 18 Dec 2024 19:43:16 -0600 Subject: [PATCH 2/2] linting --- hier_config/child.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/hier_config/child.py b/hier_config/child.py index 597df6aa..6dacd36e 100644 --- a/hier_config/child.py +++ b/hier_config/child.py @@ -114,7 +114,11 @@ def lines(self, *, sectional_exiting: bool = False) -> Iterable[str]: yield from child.lines(sectional_exiting=sectional_exiting) if sectional_exiting and (exit_text := self.sectional_exit): - depth = self.depth() - 1 if self.sectional_exit_text_parent_level else self.depth() + depth = ( + self.depth() - 1 + if self.sectional_exit_text_parent_level + else self.depth() + ) yield " " * self.driver.rules.indentation * depth + exit_text @property @@ -135,7 +139,7 @@ def sectional_exit_text_parent_level(self) -> Optional[bool]: for rule in self.driver.rules.sectional_exiting: if self.is_lineage_match(rule.match_rules): return rule.exit_text_parent_level - + return None def delete_sectional_exit(self) -> None: