Skip to content

Commit

Permalink
Add Plugins facility. Supports hooks for onInit(), onLoad(), onSend()…
Browse files Browse the repository at this point in the history
… onReply() events.

git-svn-id: http://svn.fedorahosted.org/svn/suds/trunk@680 0b8c961e-115e-4cb0-8d11-a7d6dae58e8c
  • Loading branch information
jortel committed May 11, 2010
1 parent fa103e8 commit 5ebc900
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
14 changes: 12 additions & 2 deletions suds/client.py
Expand Up @@ -40,6 +40,7 @@
from suds.properties import Unskin
from urlparse import urlparse
from copy import deepcopy
from suds.plugin import PluginContainer
from logging import getLogger

log = getLogger(__name__)
Expand Down Expand Up @@ -109,6 +110,8 @@ def __init__(self, url, **kwargs):
self.set_options(**kwargs)
reader = DefinitionsReader(options, Definitions)
self.wsdl = reader.open(url)
plugins = PluginContainer(options.plugins)
plugins.onInit(wsdl=self.wsdl)
self.factory = Factory(self.wsdl)
self.service = ServiceSelector(self, self.wsdl.services)
self.sd = []
Expand Down Expand Up @@ -593,13 +596,15 @@ def invoke(self, args, kwargs):
timer.stop()
metrics.log.debug(
"message for '%s' created: %s",
self.method.name, timer)
self.method.name,
timer)
timer.start()
result = self.send(msg)
timer.stop()
metrics.log.debug(
"method '%s' invoked: %s",
self.method.name, timer)
self.method.name,
timer)
return result

def send(self, msg):
Expand All @@ -618,6 +623,8 @@ def send(self, msg):
log.debug('sending to (%s)\nmessage:\n%s', location, msg)
try:
self.last_sent(Document(msg))
plugins = PluginContainer(self.options.plugins)
plugins.onSend(env=msg)
request = Request(location, str(msg))
request.headers = self.headers()
reply = transport.send(request)
Expand Down Expand Up @@ -655,6 +662,9 @@ def succeeded(self, binding, reply):
@raise WebFault: On server.
"""
log.debug('http succeeded:\n%s', reply)
plugins = PluginContainer(self.options.plugins)
ctx = plugins.onReply(reply=reply)
reply = ctx.reply
if len(reply) > 0:
r, p = binding.get_reply(self.method, reply)
self.last_received(r)
Expand Down
3 changes: 3 additions & 0 deletions suds/options.py
Expand Up @@ -93,6 +93,8 @@ class Options(Skin):
- 0 = Cache XML documents.
- 1 = Cache WSDL (pickled) object.
- default: 0
- B{plugins} - A plugin container.
- type: I{list}
"""
def __init__(self, **kwargs):
domain = __name__
Expand All @@ -111,5 +113,6 @@ def __init__(self, **kwargs):
Definition('retxml', bool, False),
Definition('autoblend', bool, False),
Definition('cachingpolicy', int, 0),
Definition('plugins', [], (list, tuple)),
]
Skin.__init__(self, domain, definitions, kwargs)
3 changes: 3 additions & 0 deletions suds/wsdl.py
Expand Up @@ -31,6 +31,7 @@
from suds.xsd.query import ElementQuery
from suds.sudsobject import Object, Facade, Metadata
from suds.reader import DocumentReader, DefinitionsReader
from suds.plugin import PluginContainer
from urlparse import urljoin
import re, soaparray

Expand Down Expand Up @@ -135,6 +136,8 @@ def __init__(self, url, options):
reader = DocumentReader(options)
d = reader.open(url)
root = d.root()
plugins = PluginContainer(options.plugins)
plugins.onLoad(root=root)
WObject.__init__(self, root)
self.id = objid(self)
self.options = options
Expand Down
10 changes: 9 additions & 1 deletion suds/xsd/doctor.py
Expand Up @@ -22,6 +22,7 @@
from logging import getLogger
from suds.sax import splitPrefix, Namespace
from suds.sax.element import Element
from suds.plugin import Plugin

log = getLogger(__name__)

Expand Down Expand Up @@ -186,7 +187,7 @@ def exists(self, root):
return 0


class ImportDoctor(Doctor):
class ImportDoctor(Doctor, Plugin):
"""
Doctor used to fix missing imports.
@ivar imports: A list of imports to apply.
Expand All @@ -210,3 +211,10 @@ def add(self, *imports):
def examine(self, root):
for imp in self.imports:
imp.apply(root)

def onLoad(self, context):
root = context.root
if root.get('name') == 'schema':
self.examine(root)
else:
pass
7 changes: 3 additions & 4 deletions suds/xsd/schema.py
Expand Up @@ -33,6 +33,7 @@
from suds.xsd.deplist import DepList
from suds.sax.element import Element
from suds.sax import splitPrefix, Namespace
from suds.plugin import PluginContainer

log = getLogger(__name__)

Expand Down Expand Up @@ -168,10 +169,6 @@ class Schema:
@type container: L{SchemaCollection}
@ivar children: A list of direct top level children.
@type children: [L{SchemaObject},...]
@ivar all: A children.
@type all: [L{SchemaObject},...]
@ivar groups: A schema groups cache.
@type groups: {name:L{SchemaObject}}
@ivar all: A list of all (includes imported) top level children.
@type all: [L{SchemaObject},...]
@ivar types: A schema types cache.
Expand Down Expand Up @@ -217,6 +214,8 @@ def __init__(self, root, baseurl, options, container=None):
self.attributes = {}
self.groups = {}
self.agrps = {}
plugins = PluginContainer(options.plugins)
plugins.onLoad(root=root)
if options.doctor is not None:
options.doctor.examine(root)
form = self.root.get('elementFormDefault')
Expand Down
18 changes: 17 additions & 1 deletion tests/axis1.py
Expand Up @@ -29,6 +29,7 @@
from suds.client import Client
from suds.sudsobject import Object
from suds.transport.https import HttpAuthenticated
from suds.plugin import Plugin

errors = 0

Expand All @@ -37,6 +38,21 @@
setup_logging()


class TestPlugin(Plugin):

def onInit(self, context):
print 'init: ctx=%s' % context.__dict__

def onLoad(self, context):
print 'loading: ctx=%s' % context.__dict__

def onSend(self, context):
print 'sending: ctx=%s' % context.__dict__

def onReply(self, context):
print 'gotreply: ctx=%s' % context.__dict__


#logging.getLogger('suds.client').setLevel(logging.DEBUG)

def start(url):
Expand All @@ -48,7 +64,7 @@ def start(url):
url = 'http://localhost:8081/axis/services/basic-rpc-encoded?wsdl'
start(url)
t = HttpAuthenticated(**credentials)
client = Client(url, transport=t, cache=None)
client = Client(url, transport=t, cache=None, plugins=[TestPlugin()])
print client
#
# create a name object using the wsdl
Expand Down

0 comments on commit 5ebc900

Please sign in to comment.