Package suds :: Module plugin
[hide private]
[frames] | no frames]

Source Code for Module suds.plugin

  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  The plugin module provides classes for implementation 
 19  of suds plugins. 
 20  """ 
 21   
 22  from suds import * 
 23  from logging import getLogger 
 24   
 25  log = getLogger(__name__) 
 26   
 27   
28 -class Context(object):
29 """ 30 Plugin context. 31 """ 32 pass
33 34
35 -class InitContext(Context):
36 """ 37 Init Context. 38 @ivar wsdl: The wsdl. 39 @type wsdl: L{wsdl.Definitions} 40 """ 41 pass
42 43
44 -class DocumentContext(Context):
45 """ 46 The XML document load context. 47 @ivar url: The URL. 48 @type url: str 49 @ivar document: Either the XML text or the B{parsed} document root. 50 @type document: (str|L{sax.element.Element}) 51 """ 52 pass
53 54
55 -class MessageContext(Context):
56 """ 57 The context for sending the soap envelope. 58 @ivar envelope: The soap envelope to be sent. 59 @type envelope: (str|L{sax.element.Element}) 60 @ivar reply: The reply. 61 @type reply: (str|L{sax.element.Element}|object) 62 """ 63 pass
64 65
66 -class Plugin:
67 """ 68 Plugin base. 69 """ 70 pass
71 72
73 -class InitPlugin(Plugin):
74 """ 75 The base class for suds I{init} plugins. 76 """ 77
78 - def initialized(self, context):
79 """ 80 Suds client initialization. 81 Called after wsdl the has been loaded. Provides the plugin 82 with the opportunity to inspect/modify the WSDL. 83 @param context: The init context. 84 @type context: L{InitContext} 85 """ 86 pass
87 88
89 -class DocumentPlugin(Plugin):
90 """ 91 The base class for suds I{document} plugins. 92 """ 93
94 - def loaded(self, context):
95 """ 96 Suds has loaded a WSDL/XSD document. Provides the plugin 97 with an opportunity to inspect/modify the unparsed document. 98 Called after each WSDL/XSD document is loaded. 99 @param context: The document context. 100 @type context: L{DocumentContext} 101 """ 102 pass
103
104 - def parsed(self, context):
105 """ 106 Suds has parsed a WSDL/XSD document. Provides the plugin 107 with an opportunity to inspect/modify the parsed document. 108 Called after each WSDL/XSD document is parsed. 109 @param context: The document context. 110 @type context: L{DocumentContext} 111 """ 112 pass
113 114
115 -class MessagePlugin(Plugin):
116 """ 117 The base class for suds I{soap message} plugins. 118 """ 119
120 - def marshalled(self, context):
121 """ 122 Suds will send the specified soap envelope. 123 Provides the plugin with the opportunity to inspect/modify 124 the envelope Document before it is sent. 125 @param context: The send context. 126 The I{envelope} is the envelope docuemnt. 127 @type context: L{MessageContext} 128 """ 129 pass
130
131 - def sending(self, context):
132 """ 133 Suds will send the specified soap envelope. 134 Provides the plugin with the opportunity to inspect/modify 135 the message text it is sent. 136 @param context: The send context. 137 The I{envelope} is the envelope text. 138 @type context: L{MessageContext} 139 """ 140 pass
141
142 - def received(self, context):
143 """ 144 Suds has received the specified reply. 145 Provides the plugin with the opportunity to inspect/modify 146 the received XML text before it is SAX parsed. 147 @param context: The reply context. 148 The I{reply} is the raw text. 149 @type context: L{MessageContext} 150 """ 151 pass
152
153 - def parsed(self, context):
154 """ 155 Suds has sax parsed the received reply. 156 Provides the plugin with the opportunity to inspect/modify 157 the sax parsed DOM tree for the reply before it is unmarshalled. 158 @param context: The reply context. 159 The I{reply} is DOM tree. 160 @type context: L{MessageContext} 161 """ 162 pass
163
164 - def unmarshalled(self, context):
165 """ 166 Suds has unmarshalled the received reply. 167 Provides the plugin with the opportunity to inspect/modify 168 the unmarshalled reply object before it is returned. 169 @param context: The reply context. 170 The I{reply} is unmarshalled suds object. 171 @type context: L{MessageContext} 172 """ 173 pass
174 175
176 -class PluginContainer:
177 """ 178 Plugin container provides easy method invocation. 179 @ivar plugins: A list of plugin objects. 180 @type plugins: [L{Plugin},] 181 @cvar ctxclass: A dict of plugin method / context classes. 182 @type ctxclass: dict 183 """ 184 185 domains = {\ 186 'init': (InitContext, InitPlugin), 187 'document': (DocumentContext, DocumentPlugin), 188 'message': (MessageContext, MessagePlugin ), 189 } 190
191 - def __init__(self, plugins):
192 """ 193 @param plugins: A list of plugin objects. 194 @type plugins: [L{Plugin},] 195 """ 196 self.plugins = plugins
197
198 - def __getattr__(self, name):
199 domain = self.domains.get(name) 200 if domain: 201 plugins = [] 202 ctx, pclass = domain 203 for p in self.plugins: 204 if isinstance(p, pclass): 205 plugins.append(p) 206 return PluginDomain(ctx, plugins) 207 else: 208 raise Exception, 'plugin domain (%s), invalid' % name
209 210
211 -class PluginDomain:
212 """ 213 The plugin domain. 214 @ivar ctx: A context. 215 @type ctx: L{Context} 216 @ivar plugins: A list of plugins (targets). 217 @type plugins: list 218 """ 219
220 - def __init__(self, ctx, plugins):
221 self.ctx = ctx 222 self.plugins = plugins
223
224 - def __getattr__(self, name):
225 return Method(name, self)
226 227
228 -class Method:
229 """ 230 Plugin method. 231 @ivar name: The method name. 232 @type name: str 233 @ivar domain: The plugin domain. 234 @type domain: L{PluginDomain} 235 """ 236
237 - def __init__(self, name, domain):
238 """ 239 @param name: The method name. 240 @type name: str 241 @param domain: A plugin domain. 242 @type domain: L{PluginDomain} 243 """ 244 self.name = name 245 self.domain = domain
246
247 - def __call__(self, **kwargs):
248 ctx = self.domain.ctx() 249 ctx.__dict__.update(kwargs) 250 for plugin in self.domain.plugins: 251 try: 252 method = getattr(plugin, self.name, None) 253 if method and callable(method): 254 method(ctx) 255 except Exception, pe: 256 log.exception(pe) 257 return ctx
258