Package suds :: Package mx :: Module encoded
[hide private]
[frames] | no frames]

Source Code for Module suds.mx.encoded

  1  # This program is free software; you can redistribute it and/or modify 
  2  # it under the terms of the (LGPL) GNU Lesser General Public License as 
  3  # published by the Free Software Foundation; either version 3 of the  
  4  # License, or (at your option) any later version. 
  5  # 
  6  # This program is distributed in the hope that it will be useful, 
  7  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  9  # GNU Library Lesser General Public License for more details at 
 10  # ( http://www.gnu.org/licenses/lgpl.html ). 
 11  # 
 12  # You should have received a copy of the GNU Lesser General Public License 
 13  # along with this program; if not, write to the Free Software 
 14  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 15  # written by: Jeff Ortel ( jortel@redhat.com ) 
 16   
 17  """ 
 18  Provides encoded I{marshaller} classes. 
 19  """ 
 20   
 21  from logging import getLogger 
 22  from suds import * 
 23  from suds.mx import * 
 24  from suds.mx.literal import Literal 
 25  from suds.mx.typer import Typer 
 26  from suds.sudsobject import Factory, Object 
 27  from suds.xsd.query import TypeQuery 
 28   
 29  log = getLogger(__name__) 
 30   
 31  # 
 32  # Add encoded extensions 
 33  # aty = The soap (section 5) encoded array type. 
 34  # 
 35  Content.extensions.append('aty') 
 36   
 37   
38 -class Encoded(Literal):
39 """ 40 A SOAP section (5) encoding marshaller. 41 This marshaller supports rpc/encoded soap styles. 42 """ 43
44 - def start(self, content):
45 # 46 # For soap encoded arrays, the 'aty' (array type) information 47 # is extracted and added to the 'content'. Then, the content.value 48 # is replaced with an object containing an 'item=[]' attribute 49 # containing values that are 'typed' suds objects. 50 # 51 start = Literal.start(self, content) 52 if start and isinstance(content.value, (list,tuple)): 53 resolved = content.type.resolve() 54 for c in resolved: 55 if hasattr(c[0], 'aty'): 56 content.aty = (content.tag, c[0].aty) 57 self.cast(content) 58 break 59 return start
60
61 - def end(self, parent, content):
62 # 63 # For soap encoded arrays, the soapenc:arrayType attribute is 64 # added with proper type and size information. 65 # Eg: soapenc:arrayType="xs:int[3]" 66 # 67 Literal.end(self, parent, content) 68 if content.aty is None: 69 return 70 tag, aty = content.aty 71 ns0 = ('at0', aty[1]) 72 ns1 = ('at1', 'http://schemas.xmlsoap.org/soap/encoding/') 73 array = content.value.item 74 child = parent.getChild(tag) 75 child.addPrefix(ns0[0], ns0[1]) 76 child.addPrefix(ns1[0], ns1[1]) 77 name = '%s:arrayType' % ns1[0] 78 value = '%s:%s[%d]' % (ns0[0], aty[0], len(array)) 79 child.set(name, value)
80
81 - def encode(self, node, content):
82 if content.type.any(): 83 Typer.auto(node, content.value) 84 return 85 if content.real.any(): 86 Typer.auto(node, content.value) 87 return 88 ns = None 89 name = content.real.name 90 if self.xstq: 91 ns = content.real.namespace() 92 Typer.manual(node, name, ns)
93
94 - def cast(self, content):
95 """ 96 Cast the I{untyped} list items found in content I{value}. 97 Each items contained in the list is checked for XSD type information. 98 Items (values) that are I{untyped}, are replaced with suds objects and 99 type I{metadata} is added. 100 @param content: The content holding the collection. 101 @type content: L{Content} 102 @return: self 103 @rtype: L{Encoded} 104 """ 105 aty = content.aty[1] 106 resolved = content.type.resolve() 107 array = Factory.object(resolved.name) 108 array.item = [] 109 query = TypeQuery(aty) 110 ref = query.execute(self.schema) 111 if ref is None: 112 raise TypeNotFound(qref) 113 for x in content.value: 114 if isinstance(x, (list, tuple)): 115 array.item.append(x) 116 continue 117 if isinstance(x, Object): 118 md = x.__metadata__ 119 md.sxtype = ref 120 array.item.append(x) 121 continue 122 if isinstance(x, dict): 123 x = Factory.object(ref.name, x) 124 md = x.__metadata__ 125 md.sxtype = ref 126 array.item.append(x) 127 continue 128 x = Factory.property(ref.name, x) 129 md = x.__metadata__ 130 md.sxtype = ref 131 array.item.append(x) 132 content.value = array 133 return self
134