Skip to content

Commit

Permalink
add ability to set sectional exit text at the parent level
Browse files Browse the repository at this point in the history
  • Loading branch information
jtdub committed Dec 19, 2024
1 parent 7ab373a commit 2eb9f74
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion hier_config/child.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions hier_config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions hier_config/platforms/cisco_xr/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),),
Expand Down

0 comments on commit 2eb9f74

Please sign in to comment.