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

Kshitij Gupta kgupta8592 at gmail.com
Fri Feb 28 20:11:54 UTC 2014


Hello,

On Mar 1, 2014 12:08 AM, "Christian Boltz" <apparmor at cboltz.de> wrote:
>
> Hello,
>
> Am Donnerstag, 27. Februar 2014 schrieb Kshitij Gupta:
> > On Feb 27, 2014 6:18 AM, "Christian Boltz" <apparmor at cboltz.de> wrote:
> > > 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
>
> Nice. Looks like I didn't hit a tuple while debugging ;-)
>
> > > +    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).
>
> This is meant as a debugging aid to show the content of $something.

$something? The inner phper/scripter bleeds through ;-)

> Before modifying some variables / hashers / lists / tuples / whatever,
> you should have a look at the existing code, which will also show you if
> something is a list or a tuple.
>
> So I'd say we can handle list and tuple in the same code for now.
> If someone really needs to see the type, we can always split the if
> condition - but for now it's easier to handle it in the same code.

Fair enough. That someone can decide the new formatting and push a patch.
;-).
>
> > Nacked in present state. I hope thats fine?
>
> Yes, of course. Feedback to improve code is always fine ;-)
>
> > Can please look into the suggested change and resend the patch? :-)
>
> I didn't hear anything from Jamie to defend pprint after my last mail ;-)

;-)
> so here's v2 (with the "or isinstance(src, tuple)" part added):
>

Looks good. (Haven't tested it again)

Acked-by: Kshitij Gupta <kgupta8592 at gmail.com>

Maybe you should ask the activestate folks to update the same too. Perhaps
a bug report.

Regards,

Kshitij Gupta

> === modified file 'utils/apparmor/common.py'
> --- utils/apparmor/common.py    2014-02-24 19:34:21 +0000
> +++ utils/apparmor/common.py    2014-02-28 18:27:39 +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) or isinstance(src, tuple):
> +        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
> --
> > oder das absolut berauschende ;-))
> >   [ -d "/test/" ] || echo mkd
> Danke, zum Glück muß ich heute nicht mehr mit dem Auto fahren :-)
> [> Thomas Preissler und Al Bogner in suse-linux]
>
>
> --
> AppArmor mailing list
> AppArmor at lists.ubuntu.com
> Modify settings or unsubscribe at:
https://lists.ubuntu.com/mailman/listinfo/apparmor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/apparmor/attachments/20140301/25c787a5/attachment.html>


More information about the AppArmor mailing list