debuggers.hg

changeset 21277:7ccf597281d8

cpupools, xend: Add missing file sxputils.py

Signed-off-by: Juergen Gross <juergen.gross@ts.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Apr 26 06:50:39 2010 +0100 (2010-04-26)
parents c87ec146229a
children 59523ceaff36
files tools/python/xen/util/sxputils.py
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/python/xen/util/sxputils.py	Mon Apr 26 06:50:39 2010 +0100
     1.3 @@ -0,0 +1,64 @@
     1.4 +#============================================================================
     1.5 +# This library is free software; you can redistribute it and/or
     1.6 +# modify it under the terms of version 2.1 of the GNU Lesser General Public
     1.7 +# License as published by the Free Software Foundation.
     1.8 +#
     1.9 +# This library is distributed in the hope that it will be useful,
    1.10 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.11 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.12 +# Lesser General Public License for more details.
    1.13 +#
    1.14 +# You should have received a copy of the GNU Lesser General Public
    1.15 +# License along with this library; if not, write to the Free Software
    1.16 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.17 +#============================================================================
    1.18 +# Copyright (c) 2009 Fujitsu Technology Solutions
    1.19 +#============================================================================
    1.20 +
    1.21 +""" convert sxp to map / map to sxp.
    1.22 +"""
    1.23 +
    1.24 +import types
    1.25 +from xen.xend import sxp
    1.26 +
    1.27 +def map2sxp(map_val):
    1.28 +    """ conversion of all key-value pairs of a map (recursively) to sxp.
    1.29 +        @param map_val: map; if a value contains a list or dict it is also
    1.30 +                    converted to sxp
    1.31 +        @type map_val: dict
    1.32 +        @return sxp expr
    1.33 +        @rtype: list
    1.34 +    """
    1.35 +    sxp_vals = []
    1.36 +    for (k, v) in map_val.items():
    1.37 +        if isinstance(v, types.DictionaryType):
    1.38 +            sxp_vals += [[k] + map2sxp(v)]
    1.39 +        elif isinstance(v, types.ListType):
    1.40 +            sxp_vals += [[k] + v]
    1.41 +        else:
    1.42 +            sxp_vals += [[k, v]]
    1.43 +    return sxp_vals
    1.44 +
    1.45 +def sxp2map( s ):
    1.46 +    """ conversion of sxp to map.
    1.47 +        @param s: sxp expr
    1.48 +        @type s:  list
    1.49 +        @return: map
    1.50 +        @rtype: dict
    1.51 +    """
    1.52 +    sxphash = {}
    1.53 +
    1.54 +    for child in sxp.children( s ):
    1.55 +        if isinstance( child, types.ListType ) and len( child ) > 1:
    1.56 +            if isinstance( child[1], types.ListType ) and len( child[1] ) > 1:
    1.57 +                sxphash[ child[0] ] = sxp2map( child )
    1.58 +            else:
    1.59 +                childs = sxp.children(child)
    1.60 +                if len(childs) > 1:
    1.61 +                    sxphash[ child[0] ] = childs
    1.62 +                else:
    1.63 +                    sxphash[ child[0] ] = childs[0]
    1.64 +
    1.65 +    return sxphash
    1.66 +
    1.67 +