/usr/share/pyshared/soaplib/serializers/clazz.py is in python-soaplib 0.8.1-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | import inspect
from soaplib.xml import ns, create_xml_element, create_xml_subelement, qualify
from primitive import Null
class ClassSerializerMeta(type):
'''
This is the metaclass that populates ClassSerializer instances with
the appropriate datatypes for (de)serialization
'''
def __init__(cls, clsname, bases, dictionary):
'''
This initializes the class, and sets all the appropriate
types onto the class for serialization. This implementation
assumes that all attributes assigned to this class are internal
serializers for this class
'''
if not hasattr(cls,'types'):
return
types = cls.types
members = dict(inspect.getmembers(types))
cls.soap_members = {}
cls.namespace = None
for k,v in members.items():
if k == '_namespace_':
cls.namespace=v
elif not k.startswith('__'):
cls.soap_members[k] = v
# COM bridge attributes that are otherwise harmless
cls._public_methods_ = []
cls._public_attrs_ = cls.soap_members.keys()
class ClassSerializer(object):
__metaclass__ = ClassSerializerMeta
def __init__(self):
cls = self.__class__
for k,v in cls.soap_members.items():
setattr(self,k,None)
@classmethod
def to_xml(cls,value,name='retval', nsmap=ns):
element = create_xml_element(
nsmap.get(cls.get_namespace_id()) + name, nsmap)
for k,v in cls.soap_members.items():
member_value = getattr(value,k,None)
subvalue = getattr(value,k,None)
if subvalue is None:
v = Null
subelements = v.to_xml(subvalue,name=k,nsmap=nsmap)
if type(subelements) != list:
subelements = [subelements]
for s in subelements:
element.append(s)
return element
@classmethod
def from_xml(cls, element):
obj = cls()
children = element.getchildren()
d = {}
for c in children:
name = c.tag.split('}')[-1]
if not d.has_key(name):
d[name] = []
d[name].append(c)
for tag,v in d.items():
member = cls.soap_members.get(tag)
value = member.from_xml(*v)
setattr(obj,tag,value)
return obj
@classmethod
def get_datatype(cls,nsmap=None):
if nsmap is not None:
return nsmap.get(cls.get_namespace_id()) + cls.__name__
return cls.__name__
@classmethod
def get_namespace_id(cls):
return 'tns'
@classmethod
def add_to_schema(cls, schemaDict, nsmap):
if not schemaDict.has_key(cls.get_datatype(nsmap)):
for k,v in cls.soap_members.items():
v.add_to_schema(schemaDict, nsmap)
schema_node = create_xml_element(
nsmap.get("xs") + "complexType", nsmap)
schema_node.set('name',cls.__name__)
sequence_node = create_xml_subelement(
schema_node, nsmap.get('xs') + 'sequence')
for k,v in cls.soap_members.items():
member_node = create_xml_subelement(
sequence_node, nsmap.get('xs') + 'element')
member_node.set('name',k)
member_node.set('minOccurs','0')
member_node.set('type',
"%s:%s" % (v.get_namespace_id(), v.get_datatype()))
typeElement = create_xml_element(
nsmap.get('xs') + 'element', nsmap)
typeElement.set('name',cls.__name__)
typeElement.set('type',
"%s:%s" % (cls.get_namespace_id(),cls.__name__))
schemaDict[cls.get_datatype(nsmap)+'Complex'] = schema_node
schemaDict[cls.get_datatype(nsmap)] = typeElement
|