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 from xml.dom import minidom
26
27 from rsb.converter import Converter, registerGlobalConverter, \
28 getGlobalConverterMap
29 import rst.xml.XOP_pb2 as pb
30
31 import rsb
32
33
35 """
36 Instances of this class represent individual binary data items
37 associated to an XOP XML document.
38
39 For the XOP specification, see U{http://www.w3.org/TR/xop10/}.
40
41 @author: jmoringe
42 """
44 self._uri = uri
45 self._data = data
46
49
52
53 uri = property(getURI, setURI)
54
57
60
61 data = property(getData, setData)
62
64 if hasattr(self.data, '__length__'):
65 data = '%s of length %d' % (type(self.data).__name__,
66 len(self.data))
67 else:
68 data = self.data
69 return '<%s %s: %s>' \
70 % (type(self).__name__, self.uri, data)
71
74
75
77 """
78 Instances of this class represent composite XOP structures
79 consisting of an XML document and associated binary data items.
80
81 For the XOP specification, see U{http://www.w3.org/TR/xop10/}.
82
83 @author: jmoringe
84 """
85 - def __init__(self, document, attachments=None):
94
97
102
103 document = property(getDocument, setDocument)
104
106 return self._attachments.values()
107
109 """
110 Set the associated binary data items to L{attachments}.
111
112 @param attachments: Either a C{dict} the keys of which are
113 URIs and the values of which are the
114 binary data items, or a list of either
115 L{Attachment} objects or pairs of the same
116 kind as in the C{dict} case.
117 @type attachments: dict or list
118
119 Example:
120
121 >>> p.attachments = { 'http://foo.bar': bytearray(str('bla')) }
122 ...
123
124 >>> p.attachments = [ Attachment('http://foo.bar', bytearray(str('bla'))) ]
125 ...
126 """
127
128 if isinstance(attachments, dict):
129 attachments = dict((key, self._makeAttachment(key, value))
130 for (key, value) in attachments.items())
131
132 elif isinstance(attachments, list):
133 def toPair(item):
134 if isinstance(item, Attachment):
135 return (item.uri, item)
136 else:
137 return item
138 attachments = dict(toPair(item) for item in attachments)
139 else:
140 raise TypeError, attachments
141 self._attachments = attachments
142
143 attachments = property(getAttachments, setAttachments)
144
146 return self._attachments[uri]
147
153
155 """
156 Add L{attachment} to the list of attachments.
157
158 @param attachment: The attachment that should be added.
159 @type attachment: Attachment
160 @raise KeyError: If there already is a attachment for the URI of
161 L{attachment}.
162 """
163 if attachment.uri in self._attachments:
164 raise KeyError('duplicate attachment URI: %s' % attachment.uri)
165 self._attachments[attachment.uri] = attachment
166
169
171 return '<%s <%s .../> with %s attachment(s)>' \
172 % (type(self).__name__,
173 self.document.documentElement.tagName,
174 len(self._attachments))
175
178
179
181 - def __init__(self, attachmentConverter=getGlobalConverterMap(bytearray)):
186
188 return self.__attachmentConverter
189
190 attachmentConverter = property(getAttachmentConverter)
191
193 if not isinstance(input, XOP):
194 raise TypeError(input)
195
196
197
198 holder = pb.XOP()
199 holder.xml = input.document.toxml(encoding='UTF-8')
200 for attachment in input.attachments:
201 attachment_ = holder.attachments.add()
202 attachment_.url = attachment.uri
203
204 data, wire_schema \
205 = self.attachmentConverter \
206 .getConverterForDataType(type(attachment.data)) \
207 .serialize(attachment.data)
208 attachment_.data = str(data)
209 attachment_.wire_schema = wire_schema
210
211
212
213 return holder.SerializeToString(), self.wireSchema
214
216 if not wireSchema == self.wireSchema:
217 raise ValueError("Cannot handle wire-schema %s" % wireSchema)
218
219
220
221
222 holder = pb.XOP()
223 holder.ParseFromString(str(input))
224
225
226
227 package = XOP(document=minidom.parseString(holder.xml))
228 for attachment_ in holder.attachments:
229 data = self.attachmentConverter \
230 .getConverterForWireSchema(attachment_.wire_schema) \
231 .deserialize(attachment_.data, attachment_.wire_schema)
232 package.setAttachment(attachment_.url, data)
233
234 return package
235
236
237 registerGlobalConverter(XOPConverter())
238
239
240 rsb.__defaultParticipantConfig = rsb.ParticipantConfig.fromDefaultSources()
241