# This program is free software; you can redistribute it and/or modify # it under the terms of the (LGPL) GNU Lesser General Public License as # published by the Free Software Foundation; either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Library Lesser General Public License for more details at # ( http://www.gnu.org/licenses/lgpl.html ). # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # written by: Jeff Ortel ( jortel@redhat.com ) """ Support for holding XML document texts that may then be accessed internally by suds without having to download them from an external source. Also contains XML document content to be distributed alongside the suds library. """ import suds soap5_encoding_schema = suds.byte_str("""\ 'root' can be used to distinguish serialization roots from other elements that are present in a serialization but are not roots of a serialized value graph Attributes common to all elements that function as accessors or represent independent (multi-ref) values. The href attribute is intended to be used in a manner like CONREF. That is, the element content should be empty iff the href attribute appears 'Array' is a complex type for accessors identified by position """) class DocumentStore: """ The I{suds} document store provides a local repository for XML documents. @cvar protocol: The URL protocol for the store. @type protocol: str @cvar store: The mapping of URL location to documents. @type store: dict """ def __init__(self, *args, **kwargs): self.__store = { 'schemas.xmlsoap.org/soap/encoding/':soap5_encoding_schema} self.update = self.__store.update self.update(*args, **kwargs) def __len__(self): # Implementation note: # We can not implement '__len__' as simply self.__store.__len__, as # we do for 'update' because that causes py2to3 conversion to fail. # (08.05.2013.) (Jurko) return len(self.__store) def open(self, url): """ Open a document at the specified URL. Missing documents referenced using the internal 'suds' protocol are reported by raising an exception. For other protocols, None is returned instead. @param url: A document URL. @type url: str @return: Document content or None if not found. @rtype: bytes """ protocol, location = self.__split(url) content = self.__find(location) if protocol == 'suds' and content is None: raise Exception('location "%s" not in document store' % location) return content def __find(self, location): """ Find the specified location in the store. @param location: The I{location} part of a URL. @type location: str @return: Document content or None if not found. @rtype: bytes """ return self.__store.get(location) def __split(self, url): """ Split the URL into I{protocol} and I{location} @param url: A URL. @param url: str @return: (I{url}, I{location}) @rtype: tuple """ parts = url.split('://', 1) if len(parts) == 2: return parts return None, url defaultDocumentStore = DocumentStore()