Skip to content

Commit

Permalink
Change BaseCfgLine().get_indent() to BaseCfgLine().indent
Browse files Browse the repository at this point in the history
  • Loading branch information
mpenning committed Dec 13, 2023
1 parent 2e27273 commit 690f2da
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
- Summary:
- Insert something here

## Version: 0.2.2

- Released: 2023-12-13
- Summary:
- Convert the `BaseCfgLine().get_indent()` method to a `BaseCfgLine().indent` property
- Documentation updates

## Version: 0.2.1

- Released: 2023-12-13
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

[![SonarCloud][51]][52] [![SonarCloud Maintainability Rating][53]][54] [![SonarCloud Lines of Code][55]][56] [![SonarCloud Bugs][59]][60] [![SonarCloud Code Smells][57]][58] [![SonarCloud Tech Debt][61]][62]

[![Snyk Package Health][37]][38]


## Introduction: What is ciscoconfparse?

Expand Down
34 changes: 17 additions & 17 deletions ciscoconfparse2/ccp_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class BaseCfgLine(object):
parent: Any = None
child_indent: int = 0
_children: list = []
indent: int = 0 # assign indent in the self.text setter method
confobj: Any = None # Reference to the list object which owns it
blank_line_keep: bool = False # CiscoConfParse() uses blank_line_keep

Expand Down Expand Up @@ -180,8 +179,9 @@ def get_unique_identifier(self):
return hash(linenum) * hash(_text)

# On BaseCfgLine()
@property
@logger.catch(reraise=True)
def get_indent(self):
def indent(self):
return len(self._text) - len(self.text.lstrip())

# On BaseCfgLine()
Expand Down Expand Up @@ -307,7 +307,7 @@ def calculate_line_id(self):
Do NOT cache this value. It must be recalculated when self._text
changes.
"""
indent = self.get_indent()
indent = self.indent

# Do NOT make changes to _line_id. This hash() value built from
# _line_id is the glue that holds `ciscoconfparse2.HDiff()`
Expand Down Expand Up @@ -440,7 +440,7 @@ def as_diff_dict(self):
"linenum": self.diff_linenum,
"diff_side": self.diff_side,
"diff_word": self.diff_word,
"indent": self.get_indent(),
"indent": self.indent,
"parents": [ii.text for ii in self.all_parents],
"text": self.text,
"diff_id_list": self.diff_id_list,
Expand Down Expand Up @@ -609,7 +609,7 @@ def add_child(self, childobj):
## Add the child, unless we already know it
if not (childobj in self.children):
self.children.append(childobj)
self.child_indent = childobj.get_indent()
self.child_indent = childobj.indent
return True
else:
return False
Expand Down Expand Up @@ -673,10 +673,10 @@ def uncfgtext(self):
# condition the first in this if-else logic...
elif tmp[0].lower() == "no":
assert len(tmp) > 1 # join() below only makes sense if len(tmp)>1
return self.get_indent() * " " + " ".join(tmp[1:])
return self.indent * " " + " ".join(tmp[1:])

else:
return self.get_indent() * " " + "no " + self.text.lstrip()
return self.indent * " " + "no " + self.text.lstrip()

@uncfgtext.setter
def uncfgtext(self, value=""):
Expand Down Expand Up @@ -979,7 +979,7 @@ def append_to_family(self, insertstr, indent=-1, auto_indent=False):
raise NotImplementedError(error)

# This object is the parent
insertstr_parent_indent = self.get_indent()
insertstr_parent_indent = self.indent

# Build the string to insert with proper indentation...
if indent > 0:
Expand Down Expand Up @@ -1172,15 +1172,15 @@ def classify_family_indent(self, insertstr=None):
logger.critical(error)
raise NotImplementedError(error)

if self.get_indent() == indent_width:
if self.indent == indent_width:
return 0
elif self.get_indent() < indent_width:
elif self.indent < indent_width:
this_val = indent_width / self.confobj.ccp_ref.auto_indent_width
self_val = self.get_indent() / self.confobj.ccp_ref.auto_indent_width
self_val = self.indent / self.confobj.ccp_ref.auto_indent_width
return int(this_val - self_val)
elif self.get_indent() > indent_width:
elif self.indent > indent_width:
this_val = indent_width / self.confobj.ccp_ref.auto_indent_width
self_val = self.get_indent() / self.confobj.ccp_ref.auto_indent_width
self_val = self.indent / self.confobj.ccp_ref.auto_indent_width
return int(this_val - self_val)
else:
error = "unexpected condition"
Expand All @@ -1204,8 +1204,8 @@ def find_parent_for(self, val):
#########################################################################
# build a list of candidate parent indents
#########################################################################
obj_indent_list = sorted([obj.get_indent() for obj in self.all_children])
obj_indent_list.insert(0, self.get_indent())
obj_indent_list = sorted([obj.indent for obj in self.all_children])
obj_indent_list.insert(0, self.indent)
obj_indent_list.reverse()

#########################################################################
Expand Down Expand Up @@ -1897,8 +1897,8 @@ def is_child(self):
# On BaseCfgLine()
@property
def siblings(self):
indent = self.get_indent()
return [obj for obj in self.parent.children if (obj.get_indent() == indent)]
indent = self.indent
return [obj for obj in self.parent.children if (obj.indent == indent)]

# On BaseCfgLine()
@classmethod
Expand Down
16 changes: 7 additions & 9 deletions ciscoconfparse2/ciscoconfparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,7 @@ def _build_bootstrap_parent_child(self, retval, parents_cache, parent, idx, inde
while candidate_parent_idx >= 0:
candidate_parent = retval[candidate_parent_idx]
if (
candidate_parent.get_indent() < indent
candidate_parent.indent < indent
) and candidate_parent.is_config_line:
# We found the parent
parent = candidate_parent
Expand Down Expand Up @@ -1667,7 +1667,7 @@ def bootstrap(self, text_list=None, debug=0):
factory=self.factory,
)
obj.confobj = self
indent = obj.get_indent()
indent = obj.indent
is_config_line = obj.is_config_line

# list out macro parent line numbers...
Expand Down Expand Up @@ -1767,7 +1767,7 @@ def _add_child_to_parent(self, _list, idx, indent, parentobj, childobj):
),
)

if childobj.is_comment and (_list[idx - 1].get_indent() > indent):
if childobj.is_comment and (_list[idx - 1].indent > indent):
# I *really* hate making this exception, but legacy
# ciscoconfparse2 never marked a comment as a child
# when the line immediately above it was indented more
Expand Down Expand Up @@ -2224,11 +2224,12 @@ def __repr__(self) -> str:
elif isinstance(self.config_objs, Sequence):
num_lines = len(self.config_objs)
return (
"<CiscoConfParse: %s lines / syntax: %s / comment delimiters: %s / factory: %s / ignore_blank_lines: %s / encoding: '%s' / auto_commit: %s>"
"<CiscoConfParse: %s lines / syntax: %s / comment delimiters: %s / auto_indent_width: %s / factory: %s / ignore_blank_lines: %s / encoding: '%s' / auto_commit: %s>"
% (
num_lines,
self.syntax,
self.comment_delimiters,
self.auto_indent_width,
self.factory,
self.ignore_blank_lines,
self.encoding,
Expand Down Expand Up @@ -2879,9 +2880,7 @@ def find_parent_objects(
... '!',
... ]
>>> p = CiscoConfParse(config=config)
>>> p.find_parent_objects('^interface',
... 'switchport access vlan 300')
...
>>> p.find_parent_objects(['interface', 'vlan 300'])
[<IOSCfgLine # 5 'interface FastEthernet0/2'>, <IOSCfgLine # 9 'interface FastEthernet0/3'>]
>>>
"""
Expand Down Expand Up @@ -3156,8 +3155,7 @@ def find_child_objects(
... ' address 172.16.15.5/22',
... ]
>>> p = CiscoConfParse(config=config)
>>> p.find_child_objects('^\s*interfaces',
... r'\s+ge-0/0/1')
>>> p.find_child_objects(['interface', r'ge-0/0/1'])
[<IOSCfgLine # 7 ' ge-0/0/1' (parent is # 0)>]
>>>
"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ requires-python = ">=3.8.0"

[tool.poetry]
name = "ciscoconfparse2"
version = "0.2.1"
version = "0.2.2"
description = "Parse, Audit, Query, Build, and Modify Cisco IOS-style and JunOS-style configs"
license = "GPL-3.0-only"
authors = [
Expand Down
2 changes: 1 addition & 1 deletion tests/test_CiscoConfParse.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ def testParse_f5_as_ios_02(parse_f02_ios_01):
# Be sure to strip off any double-spacing before comparing to obj.text
tmp = correct_result[dict_idx]
indent = len(tmp.rstrip()) - len(tmp.strip())
assert indent == obj.get_indent()
assert indent == obj.indent
assert correct_result[dict_idx] == obj.text

assert correct_result_linenum_dict[dict_idx]['linenum'] == obj.linenum + 1
Expand Down

0 comments on commit 690f2da

Please sign in to comment.