Skip to content
Merged
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
46 changes: 29 additions & 17 deletions pyeapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,27 +718,39 @@ def _chunkify( self, config, indent=0 ):
last parsed (sub)section, which in turn may contain sub-sections
"""
def is_subsection_present( section, indent ):
return any( [line[ indent ] == ' ' for line in section] )
return any( line[ indent ] == ' ' for line in section )

def get_indent( line ):
return len( line ) - len( line.lstrip() )

sections = {}
key = None
banner = None
for line in config.splitlines( keepends=True )[ indent > 0: ]:
# indent > 0: no need processing subsection line, which is 1st line
if line[ indent ] == ' ': # section continuation
sections[key] += line
line_rs = line.rstrip()
if indent == 0:
if banner:
sections[ banner ] += line
if line_rs == 'EOF':
banner = None
continue
if line.startswith( 'banner ' ):
banner = line_rs
sections[ banner ] = line
continue
if get_indent( line_rs ) > indent: # i.e. subsection line
# key is always expected to be set by now
sections[ key ] += line
continue
# new section is found (if key is not None)
if key: # process prior (last recorded) section
lines = sections[key].splitlines()[ 1: ]
if len( lines ): # section may contain sub-sections
ind = len( lines[0] ) - len( lines[0].lstrip() )
if is_subsection_present( lines, ind ):
subs = self._chunkify( sections[key], indent=ind )
subs.update( sections )
sections = subs
elif indent > 0: # record only subsections
del sections[key]
key = line.rstrip()
sections[key] = line
subsection = sections.get( key, '' ).splitlines()[ 1: ]
if subsection:
sub_indent = get_indent( subsection[0] )
if is_subsection_present( subsection, sub_indent ):
parsed = self._chunkify( sections[key], indent=sub_indent )
parsed.update( sections )
sections = parsed
key = line_rs
sections[ key ] = line
return sections

def section(self, regex, config='running_config'):
Expand Down
2 changes: 1 addition & 1 deletion pyeapi/eapilib.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def request(self, commands, encoding=None, reqid=None, **kwargs):
params = {'version': 1, 'cmds': commands, 'format': encoding}
streaming = False
if 'apiVersion' in kwargs:
params['version'] = kwargs['apiVersion']
params['version'] = kwargs['apiVersion']
if 'autoComplete' in kwargs:
params['autoComplete'] = kwargs['autoComplete']
if 'expandAliases' in kwargs:
Expand Down
26 changes: 16 additions & 10 deletions test/fixtures/running_config.text
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,22 @@ vlan 300
state active
no private-vlan
!
banner login
+++++++++++++++++++++++++++++++++++++++++
banner:

vlan 1
this
is the loging ban
that would b emult
EOF
!

banner motd
this text
can be multine
EOF
!
interface Port-Channel10
no description
no shutdown
Expand Down Expand Up @@ -2106,16 +2122,6 @@ no ip tacacs source-interface
!
no vxlan vni notation dotted
!
banner login
this
is the loging ban
that would b emult
EOF
banner motd
this text
can be multine
EOF
!
system coredump compressed
!
no dot1x system-auth-control
Expand Down
14 changes: 11 additions & 3 deletions test/lib/systestlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import random

from testlib import get_fixture
from pyeapi.utils import CliVariants

import pyeapi.client

Expand All @@ -48,9 +49,16 @@ def setUp(self):

self.duts = list()
for name in config.sections():
if name.startswith('connection:') and 'localhost' not in name:
name = name.split(':')[1]
self.duts.append(pyeapi.client.connect_to(name))
if not name.startswith('connection:'):
continue
if 'localhost' in name:
continue
name = name.split(':')[1]
self.duts.append( pyeapi.client.connect_to(name) )
# revert to a legacy behavior for interface availability
if self.duts[ -1 ]:
self.duts[ -1 ].config( CliVariants(
'service interface inactive expose', 'enable') )

def sort_dict_by_keys(self, d):
keys = sorted(d.keys())
Expand Down