Skip to content

Commit

Permalink
Add ignore_blank_lines option in CiscoConfParse()
Browse files Browse the repository at this point in the history
--HG--
branch : ciscoconfparse
  • Loading branch information
mpenning committed Apr 4, 2014
1 parent fa76caf commit 81a63aa
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions ciscoconfparse/ciscoconfparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"""

## Docstring props: http://stackoverflow.com/a/1523456/667301
__version_tuple__ = (0,9,29)
__version_tuple__ = (0,9,30)
__version__ = '.'.join(map(str, __version_tuple__))
__email__ = "mike /at\ pennington [dot] net"
__author__ = "David Michael Pennington <{0}>".format(__email__)
Expand Down Expand Up @@ -90,7 +90,7 @@ class CiscoConfParse(object):
"""

def __init__(self, config="", comment="!", debug=False, factory=False,
linesplit_rgx=r"\r*\n+"):
linesplit_rgx=r"\r*\n+", ignore_blank_lines=True):
"""Initialize the class, read the config, and spawn the parser"""

# re: modules usage... thank you Delnan
Expand All @@ -106,16 +106,23 @@ def __init__(self, config="", comment="!", debug=False, factory=False,

if isinstance(config, list):
# we already have a list object, simply call the parser
self.ConfigObjs = IOSConfigList(config, comment, debug, factory)
self.ConfigObjs = IOSConfigList(data=config,
comment_delimiter=comment,
debug=debug,
factory=factory,
ignore_blank_lines=ignore_blank_lines)
elif isinstance(config, str):
# Try opening as a file
try:
# string - assume a filename... open file, split and parse
f = open(config)
text = f.read()
rgx = re.compile(linesplit_rgx)
self.ConfigObjs = IOSConfigList(rgx.split(text), comment, debug,
factory)
self.ConfigObjs = IOSConfigList(rgx.split(text),
comment_delimiter=comment,
debug=debug,
factory=factory,
ignore_blank_lines=ignore_blank_lines)
except IOError:
print("[FATAL] CiscoConfParse could not open '%s'" % config)
raise RuntimeError
Expand Down Expand Up @@ -1257,14 +1264,16 @@ def _objects_to_uncfg(self, objectlist, unconflist):
class IOSConfigList(MutableSequence):
"""A custom list to hold IOSCfgLine objects"""
def __init__(self, data=None, comment_delimiter='!', debug=False,
factory=False):
factory=False, ignore_blank_lines=True):
super(IOSConfigList, self).__init__()

self._list = list()
self.CiscoConfParse = None
self.DBGFLAG = debug
self.comment_delimiter = comment_delimiter
self.factory = factory
self.ignore_blank_lines = ignore_blank_lines

if isinstance(data, list) and (data):
self._bootstrap_obj_init(data)
else:
Expand Down Expand Up @@ -1393,7 +1402,7 @@ def _bootstrap_obj_init(self, text_list=[]):
tmp = list()
for idx, line in enumerate(text_list):
# Reject empty lines
if line.strip()=='':
if self.ignore_blank_lines and line.strip()=='':
continue
if not self.factory:
obj = IOSCfgLine(line, self.comment_delimiter)
Expand Down Expand Up @@ -1535,8 +1544,8 @@ def _mark_banner(self, banner_str, os):


## Debugging only...
# print "found endpoint: line %s, text %s" % \
# (kk - 1, self.ioscfg[kk - 1])
if (DBGFLAG is True):
print("[DEBUG] _mark_banner: found endpoint - line %s, text %s" % (kk - 1, self.ioscfg[kk - 1]))
#
# Set oldest_ancestor on the parent
self._list[ii].oldest_ancestor = True
Expand Down

0 comments on commit 81a63aa

Please sign in to comment.