[apparmor] [patch] fix and improve recursive_print()

Christian Boltz apparmor at cboltz.de
Tue Nov 11 19:09:35 UTC 2014


Hello,

Am Dienstag, 11. November 2014 schrieb Christian Boltz:
> 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.

The updated version adds support for nested lists - for the start of 
each list, print a *. Without that, you get a long list of items without 
an indicator if/when a new parent list starts.


=== modified file 'utils/apparmor/common.py'
--- utils/apparmor/common.py    2014-10-14 10:54:39 +0000
+++ utils/apparmor/common.py    2014-11-11 19:03:30 +0000
@@ -75,33 +84,32 @@
     # 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 ---]')
     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.'''




Regards,

Christian Boltz
-- 
>> Why? As long as [the bug] is not solved, somebody is working on it.
> or sleeping on it :-)
You mean like zmd? :)
[>> houghi, > jdd and Anders Norrbring in opensuse]




More information about the AppArmor mailing list