Skip to content
This repository has been archived by the owner on Mar 2, 2020. It is now read-only.

Commit

Permalink
Add DocumentPlugin.loaded() hook per ticket 347.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.fedorahosted.org/svn/suds/trunk@694 0b8c961e-115e-4cb0-8d11-a7d6dae58e8c
  • Loading branch information
jortel committed Sep 1, 2010
1 parent baeef9c commit 6cab009
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
20 changes: 16 additions & 4 deletions suds/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ class InitContext(Context):
class DocumentContext(Context):
"""
The XML document load context.
@ivar root: The loaded xsd document root.
@type root: L{sax.Element}
@ivar url: The URL.
@type url: str
@ivar document: Either the XML text or the B{parsed} document root.
@type document: (str|L{sax.Element})
"""
pass

Expand Down Expand Up @@ -101,13 +103,23 @@ class DocumentPlugin(Plugin):
The base class for suds I{document} plugins.
"""

def loaded(self, context):
"""
Suds has loaded a WSDL/XSD document. Provides the plugin
with an opportunity to inspect/modify the unparsed document.
Called after each WSDL/XSD document is loaded.
@param context: The document context.
@type context: L{DocumentContext}
"""
pass

def parsed(self, context):
"""
Suds has parsed a WSDL/XSD document. Provides the plugin
with an opportunity to inspect/modify the parsed document.
Called after each WSDL/XSD document is parsed.
@param context: The document context.
@type context: L{LDocumentContext}
@type context: L{DocumentContext}
"""
pass

Expand Down Expand Up @@ -252,7 +264,7 @@ def __call__(self, **kwargs):
for plugin in self.domain.plugins:
try:
method = getattr(plugin, self.name, None)
if method:
if method and callable(method):
method(ctx)
except Exception, pe:
log.exception(pe)
Expand Down
8 changes: 6 additions & 2 deletions suds/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def open(self, url):
if d is None:
d = self.download(url)
cache.put(id, d)
self.plugins.document.parsed(root=d.root())
self.plugins.document.parsed(url=url, document=d.root())
return d

def download(self, url):
Expand All @@ -93,8 +93,12 @@ def download(self, url):
fp = store.open(url)
if fp is None:
fp = self.options.transport.open(Request(url))
content = fp.read()
fp.close()
ctx = self.plugins.document.loaded(url=url, document=content)
content = ctx.document
sax = Parser()
return sax.parse(file=fp)
return sax.parse(string=content)

def cache(self):
"""
Expand Down
10 changes: 8 additions & 2 deletions tests/axis1.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ def initialized(self, context):


class MyDocumentPlugin(DocumentPlugin):

def loaded(self, context):
print 'PLUGIN (document): loaded: ctx=%s' % context.__dict__

def parsed(self, context):
print 'PLUGIN (document): parsed: ctx=%s' % context.__dict__


class MyMessagePlugin(MessagePlugin):

Expand All @@ -68,7 +71,10 @@ def unmarshalled(self, context):
print 'PLUGIN: (massage): unmarshalled: ctx=%s' % context.__dict__


myplugins = (MyInitPlugin(), MyDocumentPlugin(), MyMessagePlugin(),)
myplugins = (
MyInitPlugin(),
MyDocumentPlugin(),
MyMessagePlugin(),)


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

0 comments on commit 6cab009

Please sign in to comment.