Changeset 3e293e4 for fedd


Ignore:
Timestamp:
Jun 24, 2008 4:59:50 PM (16 years ago)
Author:
Ted Faber <faber@…>
Branches:
axis_example, compt_changes, info-ops, master, version-1.30, version-2.00, version-3.01, version-3.02
Children:
d26d52e
Parents:
8f91e66
Message:

unpack to dictionary

File:
1 edited

Legend:

Unmodified
Added
Removed
  • fedd/fedd_util.py

    r8f91e66 r3e293e4  
    155155                self.digest_bits(str().join([chr(x) for x in b]))
    156156
    157 def print_soap(soap, out=sys.stdout, level=0, show_all=False):
    158     """Recursively print a soap element to out.
    159 
    160     This is printed as pseudo XML with a <x></x> block around each element and
    161     the element printed indented.  If show_all is true, even empty elements are
    162     displayed.
    163     """
    164 
    165     indent = level* "\t"
    166 
    167     methods = [ method for method in dir(soap) \
    168             if method.startswith("get_element") and \
    169             callable(getattr(soap, method))]
    170     if len(methods) != 0:
    171         for m in methods:
    172             element = getattr(soap, m)();
    173             if element != None or show_all:
    174                 # Element may be a list of repetitions of the same element. If
    175                 # so set elements to be that list, otherwise create a one entry
    176                 # tuple.  In either case we're going to iterate over elements.
    177                 if getattr(element,"__iter__", None) == None:
    178                     elements = (element,)
    179                 else:
    180                     elements = element
    181                 for e in elements:
    182                     # attrs are the element attributes for this element and
    183                     # they're printed in the framing XML rather than by the
    184                     # recursive call.
    185                     attrs = [ attr for attr in dir(e)\
    186                             if attr.startswith("get_attribute") and \
    187                             callable(getattr(e,attr))]
    188                     print >> out, "%s<%s" % (indent, m[len("get_element_"):]),
    189                     for a in attrs:
    190                         print >>out, ' %s="%s"' % \
    191                             (a[len("get_attribute_"):], getattr(e,a)()),
    192                     print >>out, ">"
    193                     print_soap(e, out, level+1)
    194                     print >>out, "%s</%s>" % (indent, m[len("get_element_"):])
    195     else:
    196         print >> out, "%s%s" % (indent, soap)
    197 
    198157def pack_id(id):
    199158    """
    200     Return a dictionary with the field name set by the id type
     159    Return a dictionary with the field name set by the id type.  Handy for
     160    creating dictionaries to be converted to messages.
    201161    """
    202162    if isinstance(id, type(fedid())): return { 'fedid': id }
     
    205165
    206166def pack_soap(container, name, contents):
     167    """
     168    Convert the dictionary in contents into a tree of ZSI classes.
     169
     170    The holder classes are constructed from factories in container and assigned
     171    to either the element or attribute name.  This is used to recursively
     172    create the SOAP message.
     173    """
    207174    if getattr(contents, "__iter__", None) != None:
    208175        obj = getattr(container, "new_%s" % name, None)()
     
    220187        return obj
    221188    else: return contents
     189
     190def unpack_soap(element):
     191    """
     192    Convert a tree of ZSI SOAP classes intro a hash.  The inverse of pack_soap
     193
     194    Elements or elements that are empty are ignored.
     195    """
     196    methods = [ m for m in dir(element) \
     197            if m.startswith("get_element") or m.startswith("get_attribute")]
     198    if len(methods) > 0:
     199        rv = { }
     200        for m in methods:
     201            if m.startswith("get_element_"): n = m.replace("get_element_","",1)
     202            else: n = m.replace("get_attribute_", "", 1)
     203            sub = getattr(element, m)()
     204            if sub != None:
     205                rv[n] = unpack_soap(sub)
     206        return rv
     207    elif getattr(element, "__iter__", None) != None:
     208        return [unpack_soap(e) for e in element]
     209    else:
     210        return element;
Note: See TracChangeset for help on using the changeset viewer.