Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion hier_config/child.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ 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 +134,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
Loading