Package suds :: Package umx :: Module typed
[hide private]
[frames] | no frames]

Source Code for Module suds.umx.typed

  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 typed unmarshaller classes. 
 19  """ 
 20   
 21  from logging import getLogger 
 22  from suds import * 
 23  from suds.umx import * 
 24  from suds.umx.core import Core 
 25  from suds.resolver import NodeResolver, Frame 
 26  from suds.sudsobject import Factory 
 27   
 28  log = getLogger(__name__) 
 29   
 30   
 31  # 
 32  # Add typed extensions 
 33  # type = The expected xsd type 
 34  # real = The 'true' XSD type 
 35  # 
 36  Content.extensions.append('type') 
 37  Content.extensions.append('real') 
 38   
 39   
40 -class Typed(Core):
41 """ 42 A I{typed} XML unmarshaller 43 @ivar resolver: A schema type resolver. 44 @type resolver: L{NodeResolver} 45 """ 46
47 - def __init__(self, schema):
48 """ 49 @param schema: A schema object. 50 @type schema: L{xsd.schema.Schema} 51 """ 52 self.resolver = NodeResolver(schema)
53
54 - def process(self, node, type):
55 """ 56 Process an object graph representation of the xml L{node}. 57 @param node: An XML tree. 58 @type node: L{sax.element.Element} 59 @param type: The I{optional} schema type. 60 @type type: L{xsd.sxbase.SchemaObject} 61 @return: A suds object. 62 @rtype: L{Object} 63 """ 64 content = Content(node) 65 content.type = type 66 return Core.process(self, content)
67
68 - def reset(self):
69 log.debug('reset') 70 self.resolver.reset()
71
72 - def start(self, content):
73 # 74 # Resolve to the schema type; build an object and setup metadata. 75 # 76 if content.type is None: 77 found = self.resolver.find(content.node) 78 if found is None: 79 log.error(self.resolver.schema) 80 raise TypeNotFound(content.node.qname()) 81 content.type = found 82 else: 83 known = self.resolver.known(content.node) 84 frame = Frame(content.type, resolved=known) 85 self.resolver.push(frame) 86 real = self.resolver.top().resolved 87 content.real = real 88 cls_name = real.name 89 if cls_name is None: 90 cls_name = content.node.name 91 content.data = Factory.object(cls_name) 92 md = content.data.__metadata__ 93 md.sxtype = real
94
95 - def end(self, content):
96 self.resolver.pop()
97
98 - def unbounded(self, content):
99 return content.type.unbounded()
100
101 - def nillable(self, content):
102 resolved = content.type.resolve() 103 return ( content.type.nillable or \ 104 (resolved.builtin() and resolved.nillable ) )
105
106 - def append_attribute(self, name, value, content):
107 """ 108 Append an attribute name/value into L{Content.data}. 109 @param name: The attribute name 110 @type name: basestring 111 @param value: The attribute's value 112 @type value: basestring 113 @param content: The current content being unmarshalled. 114 @type content: L{Content} 115 """ 116 type = self.resolver.findattr(name) 117 if type is None: 118 log.warn('attribute (%s) type, not-found', name) 119 else: 120 value = self.translated(value, type) 121 Core.append_attribute(self, name, value, content)
122
123 - def append_text(self, content):
124 """ 125 Append text nodes into L{Content.data} 126 Here is where the I{true} type is used to translate the value 127 into the proper python type. 128 @param content: The current content being unmarshalled. 129 @type content: L{Content} 130 """ 131 Core.append_text(self, content) 132 known = self.resolver.top().resolved 133 content.text = self.translated(content.text, known)
134
135 - def translated(self, value, type):
136 """ translate using the schema type """ 137 if value is not None: 138 resolved = type.resolve() 139 return resolved.translate(value) 140 else: 141 return value
142