1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 The I{sudsobject} module provides a collection of suds objects
19 that are primarily used for the highly dynamic interactions with
20 wsdl/xsd defined types.
21 """
22
23 from logging import getLogger
24 from suds import *
25 from new import classobj
26
27 log = getLogger(__name__)
31 """
32 Extract the I{items} from a suds object much like the
33 items() method works on I{dict}.
34 @param sobject: A suds object
35 @type sobject: L{Object}
36 @return: A list of items contained in I{sobject}.
37 @rtype: [(key, value),...]
38 """
39 for item in sobject:
40 yield item
41
44 """
45 Convert a sudsobject into a dictionary.
46 @param sobject: A suds object
47 @type sobject: L{Object}
48 @return: A python dictionary containing the
49 items contained in I{sobject}.
50 @rtype: dict
51 """
52 return dict(items(sobject))
53
55 """
56 Merge all attributes and metadata from I{a} to I{b}.
57 @param a: A I{source} object
58 @type a: L{Object}
59 @param b: A I{destination} object
60 @type b: L{Object}
61 """
62 for item in a:
63 setattr(b, item[0], item[1])
64 b.__metadata__ = b.__metadata__
65 return b
66
88
91
92 cache = {}
93
94 @classmethod
95 - def subclass(cls, name, bases, dict={}):
105
106 @classmethod
107 - def object(cls, classname=None, dict={}):
116
117 @classmethod
120
121 @classmethod
125
128
130 self.__keylist__ = []
131 self.__printer__ = Printer()
132 self.__metadata__ = Metadata()
133
135 builtin = name.startswith('__') and name.endswith('__')
136 if not builtin and \
137 name not in self.__keylist__:
138 self.__keylist__.append(name)
139 self.__dict__[name] = value
140
142 try:
143 del self.__dict__[name]
144 builtin = name.startswith('__') and name.endswith('__')
145 if not builtin:
146 self.__keylist__.remove(name)
147 except:
148 cls = self.__class__.__name__
149 raise AttributeError, "%s has no attribute '%s'" % (cls, name)
150
152 if isinstance(name, int):
153 name = self.__keylist__[int(name)]
154 return getattr(self, name)
155
157 setattr(self, name, value)
158
161
163 return len(self.__keylist__)
164
166 return name in self.__keylist__
167
170
172 return unicode(self).encode('utf-8')
173
175 return self.__printer__.tostr(self)
176
179
181 self.sobject = sobject
182 self.keylist = self.__keylist(sobject)
183 self.index = 0
184
186 keylist = self.keylist
187 nkeys = len(self.keylist)
188 while self.index < nkeys:
189 k = keylist[self.index]
190 self.index += 1
191 if hasattr(self.sobject, k):
192 v = getattr(self.sobject, k)
193 return (k, v)
194 raise StopIteration()
195
197 keylist = sobject.__keylist__
198 try:
199 keyset = set(keylist)
200 ordering = sobject.__metadata__.ordering
201 ordered = set(ordering)
202 if not ordered.issuperset(keyset):
203 log.debug(
204 '%s must be superset of %s, ordering ignored',
205 keylist,
206 ordering)
207 raise KeyError()
208 return ordering
209 except:
210 return keylist
211
214
220
224 Object.__init__(self)
225 md = self.__metadata__
226 md.facade = name
227
230
234
236 for item in self:
237 if item[0] != 'value':
238 yield item
239
242
243 - def set(self, value):
244 self.value = value
245 return self
246
249 """
250 Pretty printing of a Object object.
251 """
252
253 @classmethod
254 - def indent(cls, n): return '%*s'%(n*3,' ')
255
256 - def tostr(self, object, indent=-2):
257 """ get s string representation of object """
258 history = []
259 return self.process(object, history, indent)
260
261 - def process(self, object, h, n=0, nl=False):
262 """ print object using the specified indent (n) and newline (nl). """
263 if object is None:
264 return 'None'
265 if isinstance(object, Object):
266 if len(object) == 0:
267 return '<empty>'
268 else:
269 return self.print_object(object, h, n+2, nl)
270 if isinstance(object, dict):
271 if len(object) == 0:
272 return '<empty>'
273 else:
274 return self.print_dictionary(object, h, n+2, nl)
275 if isinstance(object, (list,tuple)):
276 if len(object) == 0:
277 return '<empty>'
278 else:
279 return self.print_collection(object, h, n+2)
280 if isinstance(object, basestring):
281 return '"%s"' % tostr(object)
282 return '%s' % tostr(object)
283
325
350
352 """ print collection using the specified indent (n) and newline (nl). """
353 if c in h: return '[]...'
354 h.append(c)
355 s = []
356 for item in c:
357 s.append('\n')
358 s.append(self.indent(n))
359 s.append(self.process(item, h, n-2))
360 s.append(',')
361 h.pop()
362 return ''.join(s)
363
365 """ translate (unwrap) using an optional wrapper function """
366 nopt = ( lambda x: x )
367 try:
368 md = d.__metadata__
369 pmd = getattr(md, '__print__', None)
370 if pmd is None:
371 return item
372 wrappers = getattr(pmd, 'wrappers', {})
373 fn = wrappers.get(item[0], nopt)
374 return (item[0], fn(item[1]))
375 except:
376 pass
377 return item
378
380 """ check metadata for excluded items """
381 try:
382 md = d.__metadata__
383 pmd = getattr(md, '__print__', None)
384 if pmd is None:
385 return False
386 excludes = getattr(pmd, 'excludes', [])
387 return ( item[0] in excludes )
388 except:
389 pass
390 return False
391