From cd67cdbe0033c5da748f3c02ff0bf5915545efb0 Mon Sep 17 00:00:00 2001 From: jortel Date: Wed, 1 Sep 2010 18:40:40 +0000 Subject: Add DocumentPlugin.loaded() hook per ticket 347. --- suds/plugin.py | 20 ++++++++++++++++---- suds/reader.py | 8 ++++++-- tests/axis1.py | 10 ++++++++-- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/suds/plugin.py b/suds/plugin.py index 219a7da..f05e22f 100644 --- a/suds/plugin.py +++ b/suds/plugin.py @@ -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 @@ -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 @@ -252,7 +264,7 @@ class Method: 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) diff --git a/suds/reader.py b/suds/reader.py index fbd8760..1184f12 100644 --- a/suds/reader.py +++ b/suds/reader.py @@ -78,7 +78,7 @@ class DocumentReader(Reader): 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): @@ -93,8 +93,12 @@ class DocumentReader(Reader): 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): """ diff --git a/tests/axis1.py b/tests/axis1.py index 3a0ebd4..e221fae 100644 --- a/tests/axis1.py +++ b/tests/axis1.py @@ -45,10 +45,13 @@ class MyInitPlugin(InitPlugin): 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): @@ -68,7 +71,10 @@ class MyMessagePlugin(MessagePlugin): print 'PLUGIN: (massage): unmarshalled: ctx=%s' % context.__dict__ -myplugins = (MyInitPlugin(), MyDocumentPlugin(), MyMessagePlugin(),) +myplugins = ( + MyInitPlugin(), + MyDocumentPlugin(), + MyMessagePlugin(),) #logging.getLogger('suds.client').setLevel(logging.DEBUG) -- cgit v1.2.1