mirror of
https://github.com/CumulusNetworks/ifupdown2.git
synced 2024-05-06 15:54:50 +00:00
Use json.dumps() with 'separator attribute' to separate json object list
dumps + some cosmetic fixes + refactoring Ticket: CM-2642 Reviewed By: sergey Testing Done: Tested json output + sanity test
This commit is contained in:
33
pkg/iface.py
33
pkg/iface.py
@@ -26,11 +26,13 @@ The module contains the following public classes:
|
||||
"""
|
||||
|
||||
from collections import OrderedDict
|
||||
import json
|
||||
import logging
|
||||
import json
|
||||
|
||||
_tickmark = ' (' + u'\u2713'.encode('utf8') + ')'
|
||||
_crossmark = ' (' + u'\u2717'.encode('utf8') + ')'
|
||||
_success_sym = _tickmark
|
||||
_error_sym = _crossmark
|
||||
|
||||
class ifaceStatus():
|
||||
"""Enumerates iface status """
|
||||
@@ -126,11 +128,8 @@ class ifaceJsonEncoder(json.JSONEncoder):
|
||||
def default(self, o):
|
||||
retconfig = {}
|
||||
if o.config:
|
||||
for k, v in o.config.items():
|
||||
if len(v) == 1:
|
||||
retconfig[k] = v[0]
|
||||
else:
|
||||
retconfig[k] = v
|
||||
retconfig = {k: (v[0] if len(v) == 1 else v)
|
||||
for k,v in o.config.items()}
|
||||
return OrderedDict({'name' : o.name,
|
||||
'addr_method' : o.addr_method,
|
||||
'addr_family' : o.addr_family,
|
||||
@@ -196,10 +195,8 @@ class iface():
|
||||
|
||||
def is_config_present(self):
|
||||
addr_method = self.addr_method
|
||||
if addr_method:
|
||||
if (addr_method.find('dhcp') != -1 or
|
||||
addr_method.find('dhcp6') != -1):
|
||||
return True
|
||||
if addr_method and addr_method in ['dhcp', 'dhcp6', 'loopback']:
|
||||
return True
|
||||
if not self.config:
|
||||
return False
|
||||
else:
|
||||
@@ -286,9 +283,9 @@ class iface():
|
||||
def get_config_attr_status_str(self, attr_name, idx=0):
|
||||
ret = self.get_config_attr_status(attr_name, idx)
|
||||
if ret:
|
||||
return _crossmark
|
||||
return _error_sym
|
||||
else:
|
||||
return _tickmark
|
||||
return _success_sym
|
||||
|
||||
def compare(self, dstiface):
|
||||
""" Compares two objects
|
||||
@@ -377,6 +374,15 @@ class iface():
|
||||
outbuf += ' %s' %self.addr_family
|
||||
if self.addr_method:
|
||||
outbuf += ' %s' %self.addr_method
|
||||
if with_status:
|
||||
if (self.status == ifaceStatus.NOTFOUND or
|
||||
self.status == ifaceStatus.ERROR):
|
||||
outbuf += ' %s' %_error_sym
|
||||
else:
|
||||
outbuf += ' %s' %_success_sym
|
||||
if self.status == ifaceStatus.NOTFOUND:
|
||||
print outbuf + '\n'
|
||||
return
|
||||
outbuf += '\n'
|
||||
config = self.config
|
||||
if config:
|
||||
@@ -391,6 +397,3 @@ class iface():
|
||||
outbuf += indent + '%s %s\n' %(cname, cv)
|
||||
idx += 1
|
||||
print outbuf
|
||||
|
||||
def dump_json(self, with_status=False):
|
||||
print json.dumps(self, cls=ifaceJsonEncoder, indent=4)
|
||||
|
@@ -14,6 +14,7 @@ import pprint
|
||||
import logging
|
||||
import sys, traceback
|
||||
import copy
|
||||
import json
|
||||
from statemanager import *
|
||||
from networkinterfaces import *
|
||||
from iface import *
|
||||
@@ -187,6 +188,7 @@ class ifupdownMain(ifupdownBase):
|
||||
ifaceobjcurr.name = ifaceobj.name
|
||||
ifaceobjcurr.lowerifaces = ifaceobj.lowerifaces
|
||||
ifaceobjcurr.priv_flags = ifaceobj.priv_flags
|
||||
ifaceobjcurr.auto = ifaceobj.auto
|
||||
self.ifaceobjcurrdict[ifaceobj.name] = ifaceobjcurr
|
||||
return ifaceobjcurr
|
||||
|
||||
@@ -309,7 +311,6 @@ class ifupdownMain(ifupdownBase):
|
||||
if not ifacenames:
|
||||
ifacenames = self.ifaceobjdict.keys()
|
||||
|
||||
self.logger.debug('populating dependency info for %s' %str(ifacenames))
|
||||
iqueue = deque(ifacenames)
|
||||
while iqueue:
|
||||
i = iqueue.popleft()
|
||||
@@ -326,8 +327,6 @@ class ifupdownMain(ifupdownBase):
|
||||
if dlist:
|
||||
self.preprocess_dependency_list(ifaceobj.name,
|
||||
dlist, ops)
|
||||
self.logger.debug('%s: lowerifaces/dependents: %s'
|
||||
%(i, str(dlist)))
|
||||
ifaceobj.lowerifaces = dlist
|
||||
[iqueue.append(d) for d in dlist]
|
||||
if not self.dependency_graph.get(i):
|
||||
@@ -881,21 +880,49 @@ class ifupdownMain(ifupdownBase):
|
||||
if not dlist: continue
|
||||
self.print_ifaceobjs_raw(dlist)
|
||||
|
||||
def print_ifaceobjs_pretty(self, ifacenames, format='native'):
|
||||
""" pretty prints iface in format given by keyword arg format """
|
||||
def _get_ifaceobjs_pretty(self, ifacenames, ifaceobjs, running=False):
|
||||
""" returns iface obj list """
|
||||
|
||||
for i in ifacenames:
|
||||
for ifaceobj in self.get_ifaceobjs(i):
|
||||
if (self.is_ifaceobj_noconfig(ifaceobj)):
|
||||
if ((not running and self.is_ifaceobj_noconfig(ifaceobj)) or
|
||||
(running and not ifaceobj.is_config_present())):
|
||||
continue
|
||||
if format == 'json':
|
||||
ifaceobj.dump_json()
|
||||
else:
|
||||
ifaceobj.dump_pretty()
|
||||
ifaceobjs.append(ifaceobj)
|
||||
if self.WITH_DEPENDS and not self.ALL:
|
||||
dlist = ifaceobj.lowerifaces
|
||||
if not dlist: continue
|
||||
self.print_ifaceobjs_pretty(dlist, format)
|
||||
self._get_ifaceobjs_pretty(dlist, ifaceobjs, running)
|
||||
|
||||
def print_ifaceobjs_pretty(self, ifacenames, format='native'):
|
||||
""" pretty prints iface in format given by keyword arg format """
|
||||
|
||||
ifaceobjs = []
|
||||
self._get_ifaceobjs_pretty(ifacenames, ifaceobjs)
|
||||
if not ifaceobjs: return
|
||||
if format == 'json':
|
||||
print json.dumps(ifaceobjs, cls=ifaceJsonEncoder, indent=4,
|
||||
separators=(',', ': '))
|
||||
else:
|
||||
map(lambda i: i.dump_pretty(), ifaceobjs)
|
||||
|
||||
def _get_ifaceobjscurr_pretty(self, ifacenames, ifaceobjs):
|
||||
ret = 0
|
||||
for i in ifacenames:
|
||||
ifaceobj = self.get_ifaceobjcurr(i)
|
||||
if not ifaceobj: continue
|
||||
if (ifaceobj.status == ifaceStatus.NOTFOUND or
|
||||
ifaceobj.status == ifaceStatus.ERROR):
|
||||
ret = 1
|
||||
if self.is_ifaceobj_noconfig(ifaceobj):
|
||||
continue
|
||||
ifaceobjs.append(ifaceobj)
|
||||
if self.WITH_DEPENDS and not self.ALL:
|
||||
dlist = ifaceobj.lowerifaces
|
||||
if not dlist: continue
|
||||
dret = self._get_ifaceobjscurr_pretty(dlist, ifaceobjs)
|
||||
if dret: ret = 1
|
||||
return ret
|
||||
|
||||
def print_ifaceobjscurr_pretty(self, ifacenames, format='native'):
|
||||
""" pretty prints current running state of interfaces with status.
|
||||
@@ -904,48 +931,28 @@ class ifupdownMain(ifupdownBase):
|
||||
else returns 0
|
||||
"""
|
||||
|
||||
ret = 0
|
||||
for i in ifacenames:
|
||||
ifaceobj = self.get_ifaceobjcurr(i)
|
||||
if not ifaceobj: continue
|
||||
if ifaceobj.status == ifaceStatus.NOTFOUND:
|
||||
print 'iface %s (%s)\n' %(ifaceobj.name,
|
||||
ifaceStatus.to_str(ifaceStatus.NOTFOUND))
|
||||
ret = 1
|
||||
continue
|
||||
elif ifaceobj.status == ifaceStatus.ERROR:
|
||||
ret = 1
|
||||
if (self.is_ifaceobj_noconfig(ifaceobj)):
|
||||
continue
|
||||
if format == 'json':
|
||||
ifaceobj.dump_json(with_status=True)
|
||||
else:
|
||||
ifaceobj.dump_pretty(with_status=True)
|
||||
if self.WITH_DEPENDS and not self.ALL:
|
||||
dlist = ifaceobj.lowerifaces
|
||||
if not dlist: continue
|
||||
self.print_ifaceobjscurr_pretty(dlist, format)
|
||||
ifaceobjs = []
|
||||
ret = self._get_ifaceobjscurr_pretty(ifacenames, ifaceobjs)
|
||||
if not ifaceobjs: return
|
||||
self.logger.debug(ifaceobjs)
|
||||
if format == 'json':
|
||||
print json.dumps(ifaceobjs, cls=ifaceJsonEncoder, indent=2,
|
||||
separators=(',', ': '))
|
||||
else:
|
||||
map(lambda i: i.dump_pretty(with_status=True), ifaceobjs)
|
||||
return ret
|
||||
|
||||
def print_ifaceobjsrunning_pretty(self, ifacenames, format='native'):
|
||||
""" pretty prints iface running state """
|
||||
|
||||
for i in ifacenames:
|
||||
ifaceobj = self.get_ifaceobj_first(i)
|
||||
if ifaceobj.status == ifaceStatus.NOTFOUND:
|
||||
print 'iface %s' %ifaceobj.name + ' (not found)\n'
|
||||
continue
|
||||
if not ifaceobj.is_config_present():
|
||||
continue
|
||||
if format == 'json':
|
||||
ifaceobj.dump_json()
|
||||
else:
|
||||
ifaceobj.dump_pretty()
|
||||
if self.WITH_DEPENDS and not self.ALL:
|
||||
dlist = ifaceobj.lowerifaces
|
||||
if not dlist: continue
|
||||
self.print_ifaceobjsrunning_pretty(dlist, format)
|
||||
return
|
||||
ifaceobjs = []
|
||||
self._get_ifaceobjs_pretty(ifacenames, ifaceobjs, running=True)
|
||||
if not ifaceobjs: return
|
||||
if format == 'json':
|
||||
print json.dumps(ifaceobjs, cls=ifaceJsonEncoder, indent=2,
|
||||
separators=(',', ': '))
|
||||
else:
|
||||
map(lambda i: i.dump_pretty(), ifaceobjs)
|
||||
|
||||
def _dump(self):
|
||||
print 'ifupdown main object dump'
|
||||
|
Reference in New Issue
Block a user