mirror of
https://gitlab.com/apparmor/apparmor.git
synced 2025-03-04 08:24:42 +01:00
fix and improve recursive_print()
This patch for recursive_print() in common.py fixes printing dicts with py3. It also replaced the tabs() lambda function with a plain string, and the brace() lambda function with a simple formatstring to make the code easier to understand. Also add support for nested lists - for the start and end of each list, print a [ and ]. Without that, you get a long list of items without an indicator if/when a new parent list starts. Acked-by: Steve Beattie <steve@nxnw.org>
This commit is contained in:
parent
3364eadafc
commit
be287de823
1 changed files with 12 additions and 12 deletions
|
@ -79,33 +79,33 @@ def recursive_print(src, dpth = 0, key = ''):
|
|||
# print recursively in a nicely formatted way
|
||||
# useful for debugging, too verbose for production code ;-)
|
||||
|
||||
# "stolen" from http://code.activestate.com/recipes/578094-recursively-print-nested-dictionaries/
|
||||
# by Scott S-Allen / MIT License
|
||||
# (output format slightly modified)
|
||||
# based on code "stolen" from Scott S-Allen / MIT License
|
||||
# http://code.activestate.com/recipes/578094-recursively-print-nested-dictionaries/
|
||||
""" Recursively prints nested elements."""
|
||||
tabs = lambda n: ' ' * n * 4 # or 2 or 8 or...
|
||||
brace = lambda s, n: '[%s]' % (s)
|
||||
tabs = ' ' * dpth * 4 # or 2 or 8 or...
|
||||
|
||||
if isinstance(src, dict):
|
||||
empty = True
|
||||
for key, value in src.iteritems():
|
||||
print (tabs(dpth) + brace(key, dpth))
|
||||
recursive_print(value, dpth + 1, key)
|
||||
for key in src.keys():
|
||||
print (tabs + '[%s]' % key)
|
||||
recursive_print(src[key], dpth + 1, key)
|
||||
empty = False
|
||||
if empty:
|
||||
print (tabs(dpth) + '[--- empty ---]')
|
||||
print (tabs + '[--- empty ---]')
|
||||
elif isinstance(src, list) or isinstance(src, tuple):
|
||||
empty = True
|
||||
print (tabs + "[")
|
||||
for litem in src:
|
||||
recursive_print(litem, dpth + 2)
|
||||
empty = False
|
||||
if empty:
|
||||
print (tabs(dpth) + '[--- empty ---]')
|
||||
print (tabs + '[--- empty ---]')
|
||||
print (tabs + "]")
|
||||
else:
|
||||
if key:
|
||||
print (tabs(dpth) + '%s = %s' % (key, src))
|
||||
print (tabs + '%s = %s' % (key, src))
|
||||
else:
|
||||
print (tabs(dpth) + '- %s' % src)
|
||||
print (tabs + '- %s' % src)
|
||||
|
||||
def cmd(command):
|
||||
'''Try to execute the given command.'''
|
||||
|
|
Loading…
Add table
Reference in a new issue