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

Source Code for Module suds.reader

  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  Contains xml document reader classes. 
 19  """ 
 20   
 21   
 22  from suds.sax.parser import Parser 
 23  from suds.transport import Request 
 24  from suds.cache import Cache, NoCache 
 25  from suds.store import DocumentStore 
 26  from suds.plugin import PluginContainer 
 27  from logging import getLogger 
 28   
 29   
 30  log = getLogger(__name__) 
 31   
 32   
33 -class Reader:
34 """ 35 The reader provides integration with cache. 36 @ivar options: An options object. 37 @type options: I{Options} 38 """ 39
40 - def __init__(self, options):
41 """ 42 @param options: An options object. 43 @type options: I{Options} 44 """ 45 self.options = options 46 self.plugins = PluginContainer(options.plugins)
47
48 - def mangle(self, name, x):
49 """ 50 Mangle the name by hashing the I{name} and appending I{x}. 51 @return: the mangled name. 52 """ 53 h = abs(hash(name)) 54 return '%s-%s' % (h, x)
55 56
57 -class DocumentReader(Reader):
58 """ 59 The XML document reader provides an integration 60 between the SAX L{Parser} and the document cache. 61 """ 62
63 - def open(self, url):
64 """ 65 Open an XML document at the specified I{url}. 66 First, the document attempted to be retrieved from 67 the I{object cache}. If not found, it is downloaded and 68 parsed using the SAX parser. The result is added to the 69 cache for the next open(). 70 @param url: A document url. 71 @type url: str. 72 @return: The specified XML document. 73 @rtype: I{Document} 74 """ 75 cache = self.cache() 76 id = self.mangle(url, 'document') 77 d = cache.get(id) 78 if d is None: 79 d = self.download(url) 80 cache.put(id, d) 81 self.plugins.document.parsed(url=url, document=d.root()) 82 return d
83
84 - def download(self, url):
85 """ 86 Download the docuemnt. 87 @param url: A document url. 88 @type url: str. 89 @return: A file pointer to the docuemnt. 90 @rtype: file-like 91 """ 92 store = DocumentStore() 93 fp = store.open(url) 94 if fp is None: 95 fp = self.options.transport.open(Request(url)) 96 content = fp.read() 97 fp.close() 98 ctx = self.plugins.document.loaded(url=url, document=content) 99 content = ctx.document 100 sax = Parser() 101 return sax.parse(string=content)
102
103 - def cache(self):
104 """ 105 Get the cache. 106 @return: The I{options} when I{cachingpolicy} = B{0}. 107 @rtype: L{Cache} 108 """ 109 if self.options.cachingpolicy == 0: 110 return self.options.cache 111 else: 112 return NoCache()
113 114
115 -class DefinitionsReader(Reader):
116 """ 117 The WSDL definitions reader provides an integration 118 between the Definitions and the object cache. 119 @ivar fn: A factory function (constructor) used to 120 create the object not found in the cache. 121 @type fn: I{Constructor} 122 """ 123
124 - def __init__(self, options, fn):
125 """ 126 @param options: An options object. 127 @type options: I{Options} 128 @param fn: A factory function (constructor) used to 129 create the object not found in the cache. 130 @type fn: I{Constructor} 131 """ 132 Reader.__init__(self, options) 133 self.fn = fn
134
135 - def open(self, url):
136 """ 137 Open a WSDL at the specified I{url}. 138 First, the WSDL attempted to be retrieved from 139 the I{object cache}. After unpickled from the cache, the 140 I{options} attribute is restored. 141 If not found, it is downloaded and instantiated using the 142 I{fn} constructor and added to the cache for the next open(). 143 @param url: A WSDL url. 144 @type url: str. 145 @return: The WSDL object. 146 @rtype: I{Definitions} 147 """ 148 cache = self.cache() 149 id = self.mangle(url, 'wsdl') 150 d = cache.get(id) 151 if d is None: 152 d = self.fn(url, self.options) 153 cache.put(id, d) 154 else: 155 d.options = self.options 156 for imp in d.imports: 157 imp.imported.options = self.options 158 return d
159
160 - def cache(self):
161 """ 162 Get the cache. 163 @return: The I{options} when I{cachingpolicy} = B{1}. 164 @rtype: L{Cache} 165 """ 166 if self.options.cachingpolicy == 1: 167 return self.options.cache 168 else: 169 return NoCache()
170