[apparmor] [patch] common.py: add recursive_print()
Christian Boltz
apparmor at cboltz.de
Thu Feb 27 00:48:31 UTC 2014
Hello,
this patch adds recursive_print() to common.py.
It prints a data structure in an easily readable output and is quite
useful[1] for debugging. However, I don't recommend to call it in
production code ;-)
=== modified file 'utils/apparmor/common.py'
--- utils/apparmor/common.py 2014-02-24 19:34:21 +0000
+++ utils/apparmor/common.py 2014-02-27 00:42:07 +0000
@@ -71,6 +71,38 @@
except IOError:
pass
+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)
+ """ Recursively prints nested elements."""
+ tabs = lambda n: ' ' * n * 4 # or 2 or 8 or...
+ brace = lambda s, n: '[%s]' % (s)
+
+ if isinstance(src, dict):
+ empty = True
+ for key, value in src.iteritems():
+ print (tabs(dpth) + brace(key, dpth))
+ recursive_print(value, dpth + 1, key)
+ empty = False
+ if empty:
+ print (tabs(dpth) + '[--- empty ---]')
+ elif isinstance(src, list):
+ empty = True
+ for litem in src:
+ recursive_print(litem, dpth + 2)
+ empty = False
+ if empty:
+ print (tabs(dpth) + '[--- empty ---]')
+ else:
+ if key:
+ print (tabs(dpth) + '%s = %s' % (key, src))
+ else:
+ print (tabs(dpth) + '- %s' % src)
+
def cmd(command):
'''Try to execute the given command.'''
debug(command)
Regards,
Christian Boltz
[1] it helped me a lot to hunt down the aa-autodep issue :-)
--
> > > Ein Update auf eine EIN JAHR alte Version?
> > Ich denke er hat einfach auf das geupdated, was bei Debian derzeit
> > als "aktuell" ausgeliefert wird...
> Ja, ist mir dann auch aufgegangen.
Immer diese "Debian-Hasser". :)
[>> nighthawk, >(>>) Ralf Hildebrandt und crandler in postfixbuch-users]
More information about the AppArmor
mailing list