[apparmor] [patch] common.py: add recursive_print()

Kshitij Gupta kgupta8592 at gmail.com
Thu Feb 27 04:10:09 UTC 2014


On Feb 27, 2014 6:18 AM, "Christian Boltz" <apparmor at cboltz.de> wrote:
>
> Hello,
>
> this patch adds recursive_print() to common.py.
>
> It prints a data structure in an easily readable output and is quite

Works with nested dictionaries, lists, strings, numbers, sets.
Err it doesnt work with tuples. Gives a nice trace:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    recursive_print(a)
  File "<pyshell#1>", line 24, in recursive_print
    print (tabs(dpth) + '- %s' % src)
TypeError: not all arguments converted during string formatting

> 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):

Matter can be fixed by changing the above line to:
elif isinstance(src, list) or isinstance(src, tuple):

Given we agree that the same style works for both lists and tuples and
we dont want people to know its a tuple (list is mutable and tuple
immutable).

Nacked in present state. I hope thats fine?

Can please look into the suggested change and resend the patch? :-)

Regards,

Kshitij Gupta

> +        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]
>
>
> --
> AppArmor mailing list
> AppArmor at lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/apparmor



More information about the AppArmor mailing list