From 4d2ca35862a40a064a048a908fd3bf7fccb744e5 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Tue, 26 Jun 2012 14:31:32 +0100 Subject: [PATCH] fixes for version 0.4, replaced prints with logger.debug() and pep8 --- .gitignore | 1 + CHANGES.txt | 7 +- MANIFEST.in | 4 +- setup.py | 22 +- src/wstools/MIMEAttachment.py | 17 +- src/wstools/Namespaces.py | 154 ++-- src/wstools/TimeoutSocket.py | 51 +- src/wstools/UserTuple.py | 63 +- src/wstools/Utility.py | 225 +++--- src/wstools/WSDLTools.py | 207 +++--- src/wstools/XMLSchema.py | 992 +++++++++++++------------- src/wstools/XMLname.py | 70 +- src/wstools/__init__.py | 1 - src/wstools/c14n.py | 155 ++-- src/wstools/logging.py | 122 ++-- src/wstools/tests/test_t1.py | 7 +- src/wstools/tests/test_wsdl.py | 31 +- src/wstools/tests/test_wstools.py | 17 +- src/wstools/tests/test_wstools_net.py | 7 +- 19 files changed, 1139 insertions(+), 1014 deletions(-) diff --git a/.gitignore b/.gitignore index 66dbf51..fae0560 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +dist *.egg-info *.pyc diff --git a/CHANGES.txt b/CHANGES.txt index 0b20c6d..6f74e87 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,12 +1,11 @@ CHANGELOG =========== -0.4 (unreleased) +0.4 (2012-06-26) ---------------- -- Nothing changed yet. - - +- Replaced print() with logging.debug() or warn() in order to allow users to change verbosity. +- Added release.sh script which runs tests, pep8 and allow you to release only when these are passing. 0.3 (2011-02-21) ---------------- diff --git a/MANIFEST.in b/MANIFEST.in index 527c14d..a3c503e 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,5 +1,5 @@ include README.txt include CHANGES.txt -include README -recursive-include src *.txt *.py *.tar.gz +recursive-include docs *.* +recursive-include src *.txt *.py *.tar.gz README diff --git a/setup.py b/setup.py index 80d8dc8..7c1636f 100644 --- a/setup.py +++ b/setup.py @@ -2,21 +2,22 @@ # # $Id: setup.py,v 1.11 2005/02/15 16:32:22 warnes Exp $ -import os,re +import os +import re from setuptools import setup, find_packages -__version__ = '0.3' +__version__ = '0.4' -url="https://github.com/kiorky/wstools.git" +url = "https://github.com/pycontribs/wstools.git" def read(*rnames): - return "\n"+ open( + return "\n" + open( os.path.join('.', *rnames) ).read() -long_description="""WSDL parsing services package for Web Services for Python. see """ + url \ +long_description = """WSDL parsing services package for Web Services for Python. see """ + url \ + read('README.txt')\ + read('CHANGES.txt')\ @@ -25,13 +26,12 @@ setup( name="wstools", version=__version__, description="wstools", - maintainer="Gregory Warnes, kiorky", - maintainer_email="Gregory.R.Warnes@Pfizer.com, kiorky@cryptelium.net", - url = url, + maintainer="Gregory Warnes, kiorky, sorin", + maintainer_email="Gregory.R.Warnes@Pfizer.com, kiorky@cryptelium.net, sorin.sbarnea@gmail.com", + url=url, long_description=long_description, packages=find_packages('src'), - package_dir = {'': 'src'}, - include_package_data=True, + package_dir={'': 'src'}, + include_package_data=True, install_requires=[] ) - diff --git a/src/wstools/MIMEAttachment.py b/src/wstools/MIMEAttachment.py index 2376cc8..f4727c7 100644 --- a/src/wstools/MIMEAttachment.py +++ b/src/wstools/MIMEAttachment.py @@ -12,11 +12,12 @@ import sys #new line -NL='\r\n' +NL = '\r\n' -_width = len(repr(sys.maxint-1)) +_width = len(repr(sys.maxint - 1)) _fmt = '%%0%dd' % _width + class MIMEMessage: def __init__(self): @@ -26,19 +27,18 @@ class MIMEMessage: self._boundary = "" def makeBoundary(self): - #create the boundary + #create the boundary msgparts = [] msgparts.append(self._xmlMessage) for i in self._files: msgparts.append(i.read()) #this sucks, all in memory alltext = NL.join(msgparts) - self._boundary = _make_boundary(alltext) + self._boundary = _make_boundary(alltext) #maybe I can save some memory del alltext del msgparts - self._startCID = "<" + (_fmt % random.randrange(sys.maxint)) + (_fmt % random.randrange(sys.maxint)) + ">" - + self._startCID = "<" + (_fmt % random.randrange(sys.maxint)) + (_fmt % random.randrange(sys.maxint)) + ">" def toString(self): '''it return a string with the MIME message''' @@ -61,7 +61,7 @@ class MIMEMessage: file.seek(0) returnstr += file.read() + NL #closing boundary - returnstr += "--" + self._boundary + "--" + NL + returnstr += "--" + self._boundary + "--" + NL return returnstr def attachFile(self, file): @@ -78,7 +78,7 @@ class MIMEMessage: def getBoundary(self): ''' - this function returns the string used in the mime message as a + this function returns the string used in the mime message as a boundary. First the write method as to be called ''' return self._boundary @@ -107,4 +107,3 @@ def _make_boundary(text=None): b = boundary + '.' + str(counter) counter += 1 return b - diff --git a/src/wstools/Namespaces.py b/src/wstools/Namespaces.py index 46a8b05..1bdb0c3 100755 --- a/src/wstools/Namespaces.py +++ b/src/wstools/Namespaces.py @@ -6,76 +6,76 @@ # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # FOR A PARTICULAR PURPOSE. -"""Namespace module, so you don't need PyXML +"""Namespace module, so you don't need PyXML """ ident = "$Id$" try: from xml.ns import SOAP, SCHEMA, WSDL, XMLNS, DSIG, ENCRYPTION - DSIG.C14N = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315" - + DSIG.C14N = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315" + except: class SOAP: - ENV = "http://schemas.xmlsoap.org/soap/envelope/" - ENC = "http://schemas.xmlsoap.org/soap/encoding/" - ACTOR_NEXT = "http://schemas.xmlsoap.org/soap/actor/next" + ENV = "http://schemas.xmlsoap.org/soap/envelope/" + ENC = "http://schemas.xmlsoap.org/soap/encoding/" + ACTOR_NEXT = "http://schemas.xmlsoap.org/soap/actor/next" class SCHEMA: - XSD1 = "http://www.w3.org/1999/XMLSchema" - XSD2 = "http://www.w3.org/2000/10/XMLSchema" - XSD3 = "http://www.w3.org/2001/XMLSchema" - XSD_LIST = [ XSD1, XSD2, XSD3] - XSI1 = "http://www.w3.org/1999/XMLSchema-instance" - XSI2 = "http://www.w3.org/2000/10/XMLSchema-instance" - XSI3 = "http://www.w3.org/2001/XMLSchema-instance" - XSI_LIST = [ XSI1, XSI2, XSI3 ] - BASE = XSD3 + XSD1 = "http://www.w3.org/1999/XMLSchema" + XSD2 = "http://www.w3.org/2000/10/XMLSchema" + XSD3 = "http://www.w3.org/2001/XMLSchema" + XSD_LIST = [XSD1, XSD2, XSD3] + XSI1 = "http://www.w3.org/1999/XMLSchema-instance" + XSI2 = "http://www.w3.org/2000/10/XMLSchema-instance" + XSI3 = "http://www.w3.org/2001/XMLSchema-instance" + XSI_LIST = [XSI1, XSI2, XSI3] + BASE = XSD3 class WSDL: - BASE = "http://schemas.xmlsoap.org/wsdl/" - BIND_HTTP = "http://schemas.xmlsoap.org/wsdl/http/" - BIND_MIME = "http://schemas.xmlsoap.org/wsdl/mime/" - BIND_SOAP = "http://schemas.xmlsoap.org/wsdl/soap/" + BASE = "http://schemas.xmlsoap.org/wsdl/" + BIND_HTTP = "http://schemas.xmlsoap.org/wsdl/http/" + BIND_MIME = "http://schemas.xmlsoap.org/wsdl/mime/" + BIND_SOAP = "http://schemas.xmlsoap.org/wsdl/soap/" BIND_SOAP12 = "http://schemas.xmlsoap.org/wsdl/soap12/" class XMLNS: - BASE = "http://www.w3.org/2000/xmlns/" - XML = "http://www.w3.org/XML/1998/namespace" - HTML = "http://www.w3.org/TR/REC-html40" + BASE = "http://www.w3.org/2000/xmlns/" + XML = "http://www.w3.org/XML/1998/namespace" + HTML = "http://www.w3.org/TR/REC-html40" class DSIG: - BASE = "http://www.w3.org/2000/09/xmldsig#" - C14N = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315" - C14N_COMM = "http://www.w3.org/TR/2000/CR-xml-c14n-20010315#WithComments" - C14N_EXCL = "http://www.w3.org/2001/10/xml-exc-c14n#" - DIGEST_MD2 = "http://www.w3.org/2000/09/xmldsig#md2" - DIGEST_MD5 = "http://www.w3.org/2000/09/xmldsig#md5" - DIGEST_SHA1 = "http://www.w3.org/2000/09/xmldsig#sha1" - ENC_BASE64 = "http://www.w3.org/2000/09/xmldsig#base64" - ENVELOPED = "http://www.w3.org/2000/09/xmldsig#enveloped-signature" - HMAC_SHA1 = "http://www.w3.org/2000/09/xmldsig#hmac-sha1" + BASE = "http://www.w3.org/2000/09/xmldsig#" + C14N = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315" + C14N_COMM = "http://www.w3.org/TR/2000/CR-xml-c14n-20010315#WithComments" + C14N_EXCL = "http://www.w3.org/2001/10/xml-exc-c14n#" + DIGEST_MD2 = "http://www.w3.org/2000/09/xmldsig#md2" + DIGEST_MD5 = "http://www.w3.org/2000/09/xmldsig#md5" + DIGEST_SHA1 = "http://www.w3.org/2000/09/xmldsig#sha1" + ENC_BASE64 = "http://www.w3.org/2000/09/xmldsig#base64" + ENVELOPED = "http://www.w3.org/2000/09/xmldsig#enveloped-signature" + HMAC_SHA1 = "http://www.w3.org/2000/09/xmldsig#hmac-sha1" SIG_DSA_SHA1 = "http://www.w3.org/2000/09/xmldsig#dsa-sha1" SIG_RSA_SHA1 = "http://www.w3.org/2000/09/xmldsig#rsa-sha1" - XPATH = "http://www.w3.org/TR/1999/REC-xpath-19991116" - XSLT = "http://www.w3.org/TR/1999/REC-xslt-19991116" + XPATH = "http://www.w3.org/TR/1999/REC-xpath-19991116" + XSLT = "http://www.w3.org/TR/1999/REC-xslt-19991116" class ENCRYPTION: - BASE = "http://www.w3.org/2001/04/xmlenc#" - BLOCK_3DES = "http://www.w3.org/2001/04/xmlenc#des-cbc" - BLOCK_AES128 = "http://www.w3.org/2001/04/xmlenc#aes128-cbc" - BLOCK_AES192 = "http://www.w3.org/2001/04/xmlenc#aes192-cbc" - BLOCK_AES256 = "http://www.w3.org/2001/04/xmlenc#aes256-cbc" - DIGEST_RIPEMD160 = "http://www.w3.org/2001/04/xmlenc#ripemd160" - DIGEST_SHA256 = "http://www.w3.org/2001/04/xmlenc#sha256" - DIGEST_SHA512 = "http://www.w3.org/2001/04/xmlenc#sha512" - KA_DH = "http://www.w3.org/2001/04/xmlenc#dh" - KT_RSA_1_5 = "http://www.w3.org/2001/04/xmlenc#rsa-1_5" - KT_RSA_OAEP = "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p" - STREAM_ARCFOUR = "http://www.w3.org/2001/04/xmlenc#arcfour" - WRAP_3DES = "http://www.w3.org/2001/04/xmlenc#kw-3des" - WRAP_AES128 = "http://www.w3.org/2001/04/xmlenc#kw-aes128" - WRAP_AES192 = "http://www.w3.org/2001/04/xmlenc#kw-aes192" - WRAP_AES256 = "http://www.w3.org/2001/04/xmlenc#kw-aes256" + BASE = "http://www.w3.org/2001/04/xmlenc#" + BLOCK_3DES = "http://www.w3.org/2001/04/xmlenc#des-cbc" + BLOCK_AES128 = "http://www.w3.org/2001/04/xmlenc#aes128-cbc" + BLOCK_AES192 = "http://www.w3.org/2001/04/xmlenc#aes192-cbc" + BLOCK_AES256 = "http://www.w3.org/2001/04/xmlenc#aes256-cbc" + DIGEST_RIPEMD160 = "http://www.w3.org/2001/04/xmlenc#ripemd160" + DIGEST_SHA256 = "http://www.w3.org/2001/04/xmlenc#sha256" + DIGEST_SHA512 = "http://www.w3.org/2001/04/xmlenc#sha512" + KA_DH = "http://www.w3.org/2001/04/xmlenc#dh" + KT_RSA_1_5 = "http://www.w3.org/2001/04/xmlenc#rsa-1_5" + KT_RSA_OAEP = "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p" + STREAM_ARCFOUR = "http://www.w3.org/2001/04/xmlenc#arcfour" + WRAP_3DES = "http://www.w3.org/2001/04/xmlenc#kw-3des" + WRAP_AES128 = "http://www.w3.org/2001/04/xmlenc#kw-aes128" + WRAP_AES192 = "http://www.w3.org/2001/04/xmlenc#kw-aes192" + WRAP_AES256 = "http://www.w3.org/2001/04/xmlenc#kw-aes256" class WSRF_V1_2: @@ -123,9 +123,9 @@ WSRFLIST = (WSRF_V1_2,) class OASIS: '''URLs for Oasis specifications ''' - WSSE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" + WSSE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" UTILITY = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" - + class X509TOKEN: Base64Binary = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" STRTransform = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0" @@ -133,7 +133,7 @@ class OASIS: X509 = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509" X509PKIPathv1 = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509PKIPathv1" X509v3SubjectKeyIdentifier = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3SubjectKeyIdentifier" - + LIFETIME = WSRF_V1_2.LIFETIME.XSD_DRAFT1 PROPERTIES = WSRF_V1_2.PROPERTIES.XSD_DRAFT1 BASENOTIFICATION = WSRF_V1_2.BASENOTIFICATION.XSD_DRAFT1 @@ -151,45 +151,50 @@ class WSTRUST: BASE = "http://schemas.xmlsoap.org/ws/2004/04/trust" ISSUE = "http://schemas.xmlsoap.org/ws/2004/04/trust/Issue" + class WSSE: - BASE = "http://schemas.xmlsoap.org/ws/2002/04/secext" - TRUST = WSTRUST.BASE + BASE = "http://schemas.xmlsoap.org/ws/2002/04/secext" + TRUST = WSTRUST.BASE class WSU: - BASE = "http://schemas.xmlsoap.org/ws/2002/04/utility" + BASE = "http://schemas.xmlsoap.org/ws/2002/04/utility" UTILITY = "http://schemas.xmlsoap.org/ws/2002/07/utility" class WSR: PROPERTIES = "http://www.ibm.com/xmlns/stdwip/web-services/WS-ResourceProperties" - LIFETIME = "http://www.ibm.com/xmlns/stdwip/web-services/WS-ResourceLifetime" + LIFETIME = "http://www.ibm.com/xmlns/stdwip/web-services/WS-ResourceLifetime" class WSA200508: - ADDRESS = "http://www.w3.org/2005/08/addressing" - ANONYMOUS = "%s/anonymous" %ADDRESS - FAULT = "%s/fault" %ADDRESS + ADDRESS = "http://www.w3.org/2005/08/addressing" + ANONYMOUS = "%s/anonymous" % ADDRESS + FAULT = "%s/fault" % ADDRESS + class WSA200408: - ADDRESS = "http://schemas.xmlsoap.org/ws/2004/08/addressing" - ANONYMOUS = "%s/role/anonymous" %ADDRESS - FAULT = "%s/fault" %ADDRESS + ADDRESS = "http://schemas.xmlsoap.org/ws/2004/08/addressing" + ANONYMOUS = "%s/role/anonymous" % ADDRESS + FAULT = "%s/fault" % ADDRESS + class WSA200403: - ADDRESS = "http://schemas.xmlsoap.org/ws/2004/03/addressing" - ANONYMOUS = "%s/role/anonymous" %ADDRESS - FAULT = "%s/fault" %ADDRESS + ADDRESS = "http://schemas.xmlsoap.org/ws/2004/03/addressing" + ANONYMOUS = "%s/role/anonymous" % ADDRESS + FAULT = "%s/fault" % ADDRESS + class WSA200303: - ADDRESS = "http://schemas.xmlsoap.org/ws/2003/03/addressing" - ANONYMOUS = "%s/role/anonymous" %ADDRESS - FAULT = None + ADDRESS = "http://schemas.xmlsoap.org/ws/2003/03/addressing" + ANONYMOUS = "%s/role/anonymous" % ADDRESS + FAULT = None WSA = WSA200408 WSA_LIST = (WSA200508, WSA200408, WSA200403, WSA200303) + class _WSAW(str): """ Define ADDRESS attribute to be compatible with WSA* layout """ ADDRESS = property(lambda s: s) @@ -197,18 +202,21 @@ class _WSAW(str): WSAW200605 = _WSAW("http://www.w3.org/2006/05/addressing/wsdl") WSAW_LIST = (WSAW200605,) - + + class WSP: POLICY = "http://schemas.xmlsoap.org/ws/2002/12/policy" + class BEA: SECCONV = "http://schemas.xmlsoap.org/ws/2004/04/sc" SCTOKEN = "http://schemas.xmlsoap.org/ws/2004/04/security/sc/sct" + class GLOBUS: SECCONV = "http://wsrf.globus.org/core/2004/07/security/secconv" - CORE = "http://www.globus.org/namespaces/2004/06/core" - SIG = "http://www.globus.org/2002/04/xmlenc#gssapi-sign" - TOKEN = "http://www.globus.org/ws/2004/09/security/sc#GSSAPI_GSI_TOKEN" + CORE = "http://www.globus.org/namespaces/2004/06/core" + SIG = "http://www.globus.org/2002/04/xmlenc#gssapi-sign" + TOKEN = "http://www.globus.org/ws/2004/09/security/sc#GSSAPI_GSI_TOKEN" ZSI_SCHEMA_URI = 'http://www.zolera.com/schemas/ZSI/' diff --git a/src/wstools/TimeoutSocket.py b/src/wstools/TimeoutSocket.py index 48b898d..3e324a5 100755 --- a/src/wstools/TimeoutSocket.py +++ b/src/wstools/TimeoutSocket.py @@ -6,16 +6,19 @@ The original timeout_socket is by: - Scott Cotton - Lloyd Zusman - Phil Mayes - Piers Lauder - Radovan Garabik + Scott Cotton + Lloyd Zusman + Phil Mayes + Piers Lauder + Radovan Garabik """ ident = "$Id$" -import string, socket, select, errno +import string +import socket +import select +import errno WSAEINVAL = getattr(errno, 'WSAEINVAL', 10022) @@ -46,7 +49,7 @@ class TimeoutSocket: apply(sock.connect, addr) sock.setblocking(timeout != 0) return 1 - except socket.error,why: + except socket.error, why: if not timeout: raise sock.setblocking(1) @@ -58,12 +61,12 @@ class TimeoutSocket: errno.EINPROGRESS, errno.EALREADY, errno.EWOULDBLOCK ): raise - r,w,e = select.select([],[sock],[],timeout) + r, w, e = select.select([], [sock], [], timeout) if w: try: apply(sock.connect, addr) return 1 - except socket.error,why: + except socket.error, why: if len(why.args) == 1: code = 0 else: @@ -77,7 +80,7 @@ class TimeoutSocket: total = len(data) next = 0 while 1: - r, w, e = select.select([],[self.sock], [], self.timeout) + r, w, e = select.select([], [self.sock], [], self.timeout) if w: buff = data[next:next + 8192] sent = self.sock.send(buff, flags) @@ -119,7 +122,8 @@ class TimeoutSocket: self._rbuf = "" while n > 0: new = self.recv(max(n, self.buffsize)) - if not new: break + if not new: + break k = len(new) if k > n: L.append(new[:n]) @@ -133,9 +137,10 @@ class TimeoutSocket: self._rbuf = "" while 1: new = self.recv(k) - if not new: break + if not new: + break L.append(new) - k = min(k*2, 1024**2) + k = min(k * 2, 1024 ** 2) return "".join(L) def readline(self, limit=-1): @@ -143,22 +148,28 @@ class TimeoutSocket: i = self._rbuf.find('\n') while i < 0 and not (0 < limit <= len(self._rbuf)): new = self.recv(self.buffsize) - if not new: break + if not new: + break i = new.find('\n') - if i >= 0: i = i + len(self._rbuf) + if i >= 0: + i = i + len(self._rbuf) self._rbuf = self._rbuf + new - if i < 0: i = len(self._rbuf) - else: i = i+1 - if 0 <= limit < len(self._rbuf): i = limit + if i < 0: + i = len(self._rbuf) + else: + i = i + 1 + if 0 <= limit < len(self._rbuf): + i = limit data, self._rbuf = self._rbuf[:i], self._rbuf[i:] return data - def readlines(self, sizehint = 0): + def readlines(self, sizehint=0): total = 0 list = [] while 1: line = self.readline() - if not line: break + if not line: + break list.append(line) total += len(line) if sizehint and total >= sizehint: diff --git a/src/wstools/UserTuple.py b/src/wstools/UserTuple.py index b8c3653..98960fc 100644 --- a/src/wstools/UserTuple.py +++ b/src/wstools/UserTuple.py @@ -8,7 +8,7 @@ Taken from Stefan Schwarzer's ftputil library, available at -Copyright (C) 1999, Stefan Schwarzer +Copyright (C) 1999, Stefan Schwarzer All rights reserved. Redistribution and use in source and binary forms, with or without @@ -42,15 +42,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ - - # $Id$ - #XXX tuple instances (in Python 2.2) contain also: # __class__, __delattr__, __getattribute__, __hash__, __new__, # __reduce__, __setattr__, __str__ # What about these? - class UserTuple: def __init__(self, inittuple=None): self.data = () @@ -67,24 +63,51 @@ class UserTuple: else: # the same applies here; (t is tuple(t)) == 1 self.data = tuple(inittuple) - def __repr__(self): return repr(self.data) - def __lt__(self, other): return self.data < self.__cast(other) - def __le__(self, other): return self.data <= self.__cast(other) - def __eq__(self, other): return self.data == self.__cast(other) - def __ne__(self, other): return self.data != self.__cast(other) - def __gt__(self, other): return self.data > self.__cast(other) - def __ge__(self, other): return self.data >= self.__cast(other) + + def __repr__(self): + return repr(self.data) + + def __lt__(self, other): + return self.data < self.__cast(other) + + def __le__(self, other): + return self.data <= self.__cast(other) + + def __eq__(self, other): + return self.data == self.__cast(other) + + def __ne__(self, other): + return self.data != self.__cast(other) + + def __gt__(self, other): + return self.data > self.__cast(other) + + def __ge__(self, other): + return self.data >= self.__cast(other) + def __cast(self, other): - if isinstance(other, UserTuple): return other.data - else: return other + if isinstance(other, UserTuple): + return other.data + else: + return other + def __cmp__(self, other): return cmp(self.data, self.__cast(other)) - def __contains__(self, item): return item in self.data - def __len__(self): return len(self.data) - def __getitem__(self, i): return self.data[i] + + def __contains__(self, item): + return item in self.data + + def __len__(self): + return len(self.data) + + def __getitem__(self, i): + return self.data[i] + def __getslice__(self, i, j): - i = max(i, 0); j = max(j, 0) + i = max(i, 0) + j = max(j, 0) return self.__class__(self.data[i:j]) + def __add__(self, other): if isinstance(other, UserTuple): return self.__class__(self.data + other.data) @@ -93,7 +116,7 @@ class UserTuple: else: return self.__class__(self.data + tuple(other)) # dir( () ) contains no __radd__ (at least in Python 2.2) + def __mul__(self, n): - return self.__class__(self.data*n) + return self.__class__(self.data * n) __rmul__ = __mul__ - diff --git a/src/wstools/Utility.py b/src/wstools/Utility.py index 1c441d6..4deff3c 100755 --- a/src/wstools/Utility.py +++ b/src/wstools/Utility.py @@ -1,7 +1,8 @@ +#!/usr/bin/env python # Copyright (c) 2003, The Regents of the University of California, # through Lawrence Berkeley National Laboratory (subject to receipt of # any required approvals from the U.S. Dept. of Energy). All rights -# reserved. +# reserved. # # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. # @@ -14,7 +15,12 @@ ident = "$Id$" -import sys, types, httplib, urllib, socket, weakref +import sys +import types +import httplib +import urllib +import socket +import weakref from os.path import isfile from string import join, strip, split from UserDict import UserDict @@ -48,17 +54,17 @@ try: except: def SplitQName(qname): '''SplitQName(qname) -> (string, string) - - Split Qualified Name into a tuple of len 2, consisting - of the prefix and the local name. - + + Split Qualified Name into a tuple of len 2, consisting + of the prefix and the local name. + (prefix, localName) - + Special Cases: xmlns -- (localName, 'xmlns') None -- (None, localName) ''' - + l = qname.split(':') if len(l) == 1: l.insert(0, None) @@ -77,10 +83,12 @@ basejoin = urllib.basejoin if sys.version_info[0:2] < (2, 4, 0, 'final', 0)[0:2]: #basejoin = lambda base,url: urllib.basejoin(base,url.lstrip('./')) token = './' - def basejoin(base, url): + + def basejoin(base, url): if url.startswith(token) is True: - return urllib.basejoin(base,url[2:]) - return urllib.basejoin(base,url) + return urllib.basejoin(base, url[2:]) + return urllib.basejoin(base, url) + class NamespaceError(Exception): """Used to indicate a Namespace Error.""" @@ -101,7 +109,7 @@ class DOMException(Exception): class Base: """Base class for instance level Logging""" def __init__(self, module=__name__): - self.logger = logging.getLogger('%s-%s(%s)' %(module, self.__class__, _get_idstr(self))) + self.logger = logging.getLogger('%s-%s(%s)' % (module, self.__class__, _get_idstr(self))) class HTTPResponse: @@ -114,6 +122,7 @@ class HTTPResponse: self.body = response.read() or None response.close() + class TimeoutHTTP(HTTPConnection): """A custom http connection object that supports socket timeout.""" def __init__(self, host, port=None, timeout=20): @@ -151,9 +160,12 @@ def urlopen(url, timeout=20, redirects=None): if not scheme in ('http', 'https'): return urllib.urlopen(url) - if params: path = '%s;%s' % (path, params) - if query: path = '%s?%s' % (path, query) - if frag: path = '%s#%s' % (path, frag) + if params: + path = '%s;%s' % (path, params) + if query: + path = '%s?%s' % (path, query) + if frag: + path = '%s#%s' % (path, frag) if scheme == 'https': # If ssl is not compiled into Python, you will not get an exception @@ -163,7 +175,7 @@ def urlopen(url, timeout=20, redirects=None): import M2Crypto except ImportError: if not hasattr(socket, 'ssl'): - raise RuntimeError, 'no built-in SSL Support' + raise RuntimeError('no built-in SSL Support') conn = TimeoutHTTPS(host, None, timeout) else: @@ -193,7 +205,7 @@ def urlopen(url, timeout=20, redirects=None): location = response.msg.getheader('location') if location is not None: response.close() - if redirects is not None and redirects.has_key(location): + if redirects is not None and location in redirects: raise RecursionError( 'Circular HTTP redirection detected.' ) @@ -210,14 +222,15 @@ def urlopen(url, timeout=20, redirects=None): response.close() return body + class DOM: """The DOM singleton defines a number of XML related constants and provides a number of utility methods for DOM related tasks. It also provides some basic abstractions so that the rest of the package need not care about actual DOM implementation in use.""" - looseNamespaces = False # if can't find a referenced namespace, try the default one - + looseNamespaces = False # if can't find a referenced namespace, try the default one + # Namespace stuff related to the SOAP specification. NS_SOAP_ENV_1_1 = 'http://schemas.xmlsoap.org/soap/envelope/' @@ -233,14 +246,14 @@ class DOM: NS_SOAP_ENC = NS_SOAP_ENC_1_1 _soap_uri_mapping = { - NS_SOAP_ENV_1_1 : '1.1', - NS_SOAP_ENV_1_2 : '1.2', + NS_SOAP_ENV_1_1: '1.1', + NS_SOAP_ENV_1_2: '1.2', } SOAP_ACTOR_NEXT_1_1 = 'http://schemas.xmlsoap.org/soap/actor/next' SOAP_ACTOR_NEXT_1_2 = 'http://www.w3.org/2001/06/soap-envelope/actor/next' SOAP_ACTOR_NEXT_ALL = (SOAP_ACTOR_NEXT_1_1, SOAP_ACTOR_NEXT_1_2) - + def SOAPUriToVersion(self, uri): """Return the SOAP version related to an envelope uri.""" value = self._soap_uri_mapping.get(uri) @@ -283,14 +296,12 @@ class DOM: 'Unsupported SOAP version: %s' % version ) - # Namespace stuff related to XML Schema. - NS_XSD_99 = 'http://www.w3.org/1999/XMLSchema' - NS_XSI_99 = 'http://www.w3.org/1999/XMLSchema-instance' + NS_XSI_99 = 'http://www.w3.org/1999/XMLSchema-instance' NS_XSD_00 = 'http://www.w3.org/2000/10/XMLSchema' - NS_XSI_00 = 'http://www.w3.org/2000/10/XMLSchema-instance' + NS_XSI_00 = 'http://www.w3.org/2000/10/XMLSchema-instance' NS_XSD_01 = 'http://www.w3.org/2001/XMLSchema' NS_XSI_01 = 'http://www.w3.org/2001/XMLSchema-instance' @@ -302,15 +313,14 @@ class DOM: NS_XSI = NS_XSI_01 _xsd_uri_mapping = { - NS_XSD_99 : NS_XSI_99, - NS_XSD_00 : NS_XSI_00, - NS_XSD_01 : NS_XSI_01, + NS_XSD_99: NS_XSI_99, + NS_XSD_00: NS_XSI_00, + NS_XSD_01: NS_XSI_01, } for key, value in _xsd_uri_mapping.items(): _xsd_uri_mapping[value] = key - def InstanceUriForSchemaUri(self, uri): """Return the appropriate matching XML Schema instance uri for the given XML Schema namespace uri.""" @@ -321,9 +331,7 @@ class DOM: the given XML Schema instance namespace uri.""" return self._xsd_uri_mapping.get(uri) - # Namespace stuff related to WSDL. - NS_WSDL_1_1 = 'http://schemas.xmlsoap.org/wsdl/' NS_WSDL_ALL = (NS_WSDL_1_1,) NS_WSDL = NS_WSDL_1_1 @@ -343,12 +351,11 @@ class DOM: NS_SOAP_HTTP_1_1 = 'http://schemas.xmlsoap.org/soap/http' NS_SOAP_HTTP_ALL = (NS_SOAP_HTTP_1_1,) NS_SOAP_HTTP = NS_SOAP_HTTP_1_1 - _wsdl_uri_mapping = { - NS_WSDL_1_1 : '1.1', + NS_WSDL_1_1: '1.1', } - + def WSDLUriToVersion(self, uri): """Return the WSDL version related to a WSDL namespace uri.""" value = self._wsdl_uri_mapping.get(uri) @@ -403,11 +410,8 @@ class DOM: 'Unsupported WSDL version: %s' % version ) - # Other xml namespace constants. - NS_XMLNS = 'http://www.w3.org/2000/xmlns/' - - + NS_XMLNS = 'http://www.w3.org/2000/xmlns/' def isElement(self, node, name, nsuri=None): """Return true if the given node is an element with the given @@ -430,7 +434,7 @@ class DOM: return child if default is not join: return default - raise KeyError, name + raise KeyError(name) def getElementById(self, node, id, default=join): """Return the first child of node matching an id reference.""" @@ -442,7 +446,7 @@ class DOM: return child if default is not join: return default - raise KeyError, name + raise KeyError(name) def getMappingById(self, document, depth=None, element=None, mapping=None, level=1): @@ -461,7 +465,7 @@ class DOM: for child in element.childNodes: if child.nodeType == ELEMENT_NODE: self.getMappingById(None, depth, child, mapping, level) - return mapping + return mapping def getElements(self, node, name, nsuri=None): """Return a sequence of the child elements of the given node that @@ -507,10 +511,10 @@ class DOM: return '' def getAttrs(self, node): - """Return a Collection of all attributes + """Return a Collection of all attributes """ attrs = {} - for k,v in node._attrs.items(): + for k, v in node._attrs.items(): attrs[k] = v.value return attrs @@ -647,11 +651,11 @@ class DOM: else: file = urlopen(url) - try: + try: result = self.loadDocument(file) except Exception, ex: file.close() - raise ParseError(('Failed to load document %s' %url,) + ex.args) + raise ParseError(('Failed to load document %s' % url,) + ex.args) else: file.close() return result @@ -660,7 +664,7 @@ DOM = DOM() class MessageInterface: - '''Higher Level Interface, delegates to DOM singleton, must + '''Higher Level Interface, delegates to DOM singleton, must be subclassed and implement all methods that throw NotImplementedError. ''' def __init__(self, sw): @@ -685,39 +689,39 @@ class MessageInterface: def canonicalize(self): '''canonicalize the underlying DOM, and return as string. ''' - raise NotImplementedError, '' + raise NotImplementedError('') def createDocument(self, namespaceURI=SOAP.ENV, localName='Envelope'): '''create Document ''' - raise NotImplementedError, '' + raise NotImplementedError('') def createAppendElement(self, namespaceURI, localName): '''create and append element(namespaceURI,localName), and return the node. ''' - raise NotImplementedError, '' + raise NotImplementedError('') def findNamespaceURI(self, qualifiedName): - raise NotImplementedError, '' + raise NotImplementedError('') def resolvePrefix(self, prefix): - raise NotImplementedError, '' + raise NotImplementedError('') def setAttributeNS(self, namespaceURI, localName, value): '''set attribute (namespaceURI, localName)=value ''' - raise NotImplementedError, '' + raise NotImplementedError('') def setAttributeType(self, namespaceURI, localName): '''set attribute xsi:type=(namespaceURI, localName) ''' - raise NotImplementedError, '' + raise NotImplementedError('') def setNamespaceAttribute(self, namespaceURI, prefix): - '''set namespace attribute xmlns:prefix=namespaceURI + '''set namespace attribute xmlns:prefix=namespaceURI ''' - raise NotImplementedError, '' + raise NotImplementedError('') class ElementProxy(Base, MessageInterface): @@ -734,27 +738,27 @@ class ElementProxy(Base, MessageInterface): _soap_env_nsuri = SOAP.ENV _soap_enc_nsuri = SOAP.ENC _zsi_nsuri = ZSI_SCHEMA_URI - _xsd_nsuri = SCHEMA.XSD3 + _xsd_nsuri = SCHEMA.XSD3 _xsi_nsuri = SCHEMA.XSI3 _xml_nsuri = XMLNS.XML _xmlns_nsuri = XMLNS.BASE standard_ns = {\ - _xml_prefix:_xml_nsuri, - _xmlns_prefix:_xmlns_nsuri + _xml_prefix: _xml_nsuri, + _xmlns_prefix: _xmlns_nsuri } reserved_ns = {\ - _soap_env_prefix:_soap_env_nsuri, - _soap_enc_prefix:_soap_enc_nsuri, - _zsi_prefix:_zsi_nsuri, - _xsd_prefix:_xsd_nsuri, - _xsi_prefix:_xsi_nsuri, + _soap_env_prefix: _soap_env_nsuri, + _soap_enc_prefix: _soap_enc_nsuri, + _zsi_prefix: _zsi_nsuri, + _xsd_prefix: _xsd_nsuri, + _xsi_prefix: _xsi_nsuri, } name = None namespaceURI = None def __init__(self, sw, message=None): - '''Initialize. + '''Initialize. sw -- SoapWriter ''' self._indx = 0 @@ -762,7 +766,7 @@ class ElementProxy(Base, MessageInterface): Base.__init__(self) self._dom = DOM self.node = None - if type(message) in (types.StringType,types.UnicodeType): + if type(message) in (types.StringType, types.UnicodeType): self.loadFromString(message) elif isinstance(message, ElementProxy): self.node = message._getNode() @@ -783,11 +787,11 @@ class ElementProxy(Base, MessageInterface): else: context = XPath.Context.Context(self.node, processorNss=processorNss) nodes = expression.evaluate(context) - return map(lambda node: ElementProxy(self.sw,node), nodes) + return map(lambda node: ElementProxy(self.sw, node), nodes) ############################################# # Methods for checking/setting the - # classes (namespaceURI,name) node. + # classes (namespaceURI,name) node. ############################################# def checkNode(self, namespaceURI=None, localName=None): ''' @@ -800,7 +804,7 @@ class ElementProxy(Base, MessageInterface): if localName and self.node: check = self._dom.isElement(self.node, localName, namespaceURI) if not check: - raise NamespaceError, 'unexpected node type %s, expecting %s' %(self.node, localName) + raise NamespaceError('unexpected node type %s, expecting %s' % (self.node, localName)) def setNode(self, node=None): if node: @@ -811,12 +815,12 @@ class ElementProxy(Base, MessageInterface): elif self.node: node = self._dom.getElement(self.node, self.name, self.namespaceURI, default=None) if not node: - raise NamespaceError, 'cant find element (%s,%s)' %(self.namespaceURI,self.name) + raise NamespaceError('cant find element (%s, %s)' % (self.namespaceURI, self.name)) self.node = node else: #self.node = self._dom.create(self.node, self.name, self.namespaceURI, default=None) self.createDocument(self.namespaceURI, localName=self.name, doctype=None) - + self.checkNode() ############################################# @@ -833,12 +837,12 @@ class ElementProxy(Base, MessageInterface): def _getUniquePrefix(self): '''I guess we need to resolve all potential prefixes - because when the current node is attached it copies the + because when the current node is attached it copies the namespaces into the parent node. ''' while 1: self._indx += 1 - prefix = 'ns%d' %self._indx + prefix = 'ns%d' % self._indx try: self._dom.findNamespaceURI(prefix, self._getNode()) except DOMException, ex: @@ -867,7 +871,7 @@ class ElementProxy(Base, MessageInterface): else: if node.parentNode: return self._getPrefix(node.parentNode, nsuri) - raise NamespaceError, 'namespaceURI "%s" is not defined' %nsuri + raise NamespaceError('namespaceURI "%s" is not defined' % nsuri) def _appendChild(self, node): ''' @@ -875,14 +879,14 @@ class ElementProxy(Base, MessageInterface): node -- DOM Element Node ''' if node is None: - raise TypeError, 'node is None' + raise TypeError('node is None') self.node.appendChild(node) def _insertBefore(self, newChild, refChild): ''' Keyword arguments: child -- DOM Element Node to insert - refChild -- DOM Element Node + refChild -- DOM Element Node ''' self.node.insertBefore(newChild, refChild) @@ -907,7 +911,7 @@ class ElementProxy(Base, MessageInterface): try: prefix = self._getPrefix(node=self.node, nsuri=namespaceURI) except NamespaceError, ex: - prefix = self._getUniquePrefix() + prefix = self._getUniquePrefix() self.setNamespaceAttribute(prefix, namespaceURI) return prefix @@ -943,20 +947,20 @@ class ElementProxy(Base, MessageInterface): prefix = self._soap_env_prefix if namespaceURI == self.reserved_ns[prefix]: - qualifiedName = '%s:%s' %(prefix,localName) + qualifiedName = '%s:%s' % (prefix, localName) elif namespaceURI is localName is None: - self.node = self._dom.createDocument(None,None,None) + self.node = self._dom.createDocument(None, None, None) return else: - raise KeyError, 'only support creation of document in %s' %self.reserved_ns[prefix] + raise KeyError('only support creation of document in %s' % self.reserved_ns[prefix]) document = self._dom.createDocument(nsuri=namespaceURI, qname=qualifiedName, doctype=doctype) self.node = document.childNodes[0] #set up reserved namespace attributes - for prefix,nsuri in self.reserved_ns.items(): - self._setAttributeNS(namespaceURI=self._xmlns_nsuri, - qualifiedName='%s:%s' %(self._xmlns_prefix,prefix), + for prefix, nsuri in self.reserved_ns.items(): + self._setAttributeNS(namespaceURI=self._xmlns_nsuri, + qualifiedName='%s:%s' % (self._xmlns_prefix, prefix), value=nsuri) ############################################# @@ -975,10 +979,10 @@ class ElementProxy(Base, MessageInterface): self.logger.debug('setAttributeType: (%s,%s)', namespaceURI, localName) value = localName if namespaceURI: - value = '%s:%s' %(self.getPrefix(namespaceURI),localName) + value = '%s:%s' % (self.getPrefix(namespaceURI), localName) xsi_prefix = self.getPrefix(self._xsi_nsuri) - self._setAttributeNS(self._xsi_nsuri, '%s:type' %xsi_prefix, value) + self._setAttributeNS(self._xsi_nsuri, '%s:type' % xsi_prefix, value) def createAttributeNS(self, namespace, name, value): document = self._getOwnerDocument() @@ -992,7 +996,7 @@ class ElementProxy(Base, MessageInterface): attributes in no namespace. localName -- local name of new attribute value -- value of new attribute - ''' + ''' prefix = None if namespaceURI: try: @@ -1002,7 +1006,7 @@ class ElementProxy(Base, MessageInterface): self.setNamespaceAttribute(prefix, namespaceURI) qualifiedName = localName if prefix: - qualifiedName = '%s:%s' %(prefix, localName) + qualifiedName = '%s:%s' % (prefix, localName) self._setAttributeNS(namespaceURI, qualifiedName, value) def setNamespaceAttribute(self, prefix, namespaceURI): @@ -1011,7 +1015,7 @@ class ElementProxy(Base, MessageInterface): prefix -- xmlns prefix namespaceURI -- value of prefix ''' - self._setAttributeNS(XMLNS.BASE, 'xmlns:%s' %prefix, namespaceURI) + self._setAttributeNS(XMLNS.BASE, 'xmlns:%s' % prefix, namespaceURI) ############################################# #Methods for elements @@ -1036,7 +1040,7 @@ class ElementProxy(Base, MessageInterface): to 'ns1' if left unspecified. ''' node = self.createAppendElement(namespaceURI, localName, prefix=None) - node=node._getNode() + node = node._getNode() self._setNode(node._getNode()) def createAppendElement(self, namespaceURI, localName, prefix=None): @@ -1056,19 +1060,19 @@ class ElementProxy(Base, MessageInterface): except: declare = True prefix = prefix or self._getUniquePrefix() - if prefix: - qualifiedName = '%s:%s' %(prefix, localName) + if prefix: + qualifiedName = '%s:%s' % (prefix, localName) node = self.createElementNS(namespaceURI, qualifiedName) if declare: - node._setAttributeNS(XMLNS.BASE, 'xmlns:%s' %prefix, namespaceURI) + node._setAttributeNS(XMLNS.BASE, 'xmlns:%s' % prefix, namespaceURI) self._appendChild(node=node._getNode()) return node def createInsertBefore(self, namespaceURI, localName, refChild): qualifiedName = localName prefix = self.getPrefix(namespaceURI) - if prefix: - qualifiedName = '%s:%s' %(prefix, localName) + if prefix: + qualifiedName = '%s:%s' % (prefix, localName) node = self.createElementNS(namespaceURI, qualifiedName) self._insertBefore(newChild=node._getNode(), refChild=refChild._getNode()) return node @@ -1091,12 +1095,12 @@ class ElementProxy(Base, MessageInterface): localName -- local name of attribute ''' if self.hasAttribute(namespaceURI, localName): - attr = self.node.getAttributeNodeNS(namespaceURI,localName) + attr = self.node.getAttributeNodeNS(namespaceURI, localName) return attr.value return None def getValue(self): - return self._dom.getElementText(self.node, preserve_ws=True) + return self._dom.getElementText(self.node, preserve_ws=True) ############################################# #Methods for text nodes @@ -1132,10 +1136,10 @@ class ElementProxy(Base, MessageInterface): return not self.node - class Collection(UserDict): """Helper class for maintaining ordered named collections.""" - default = lambda self,k: k.name + default = lambda self, k: k.name + def __init__(self, parent, key=None): UserDict.__init__(self) self.parent = weakref.ref(parent) @@ -1143,7 +1147,8 @@ class Collection(UserDict): self._func = key or self.default def __getitem__(self, key): - if type(key) is type(1): + NumberTypes = (types.IntType, types.LongType, types.FloatType, types.ComplexType) + if isinstance(key, NumberTypes): return self.list[key] return self.data[key] @@ -1164,7 +1169,8 @@ class Collection(UserDict): class CollectionNS(UserDict): """Helper class for maintaining ordered named collections.""" - default = lambda self,k: k.name + default = lambda self, k: k.name + def __init__(self, parent, key=None): UserDict.__init__(self) self.parent = weakref.ref(parent) @@ -1174,10 +1180,10 @@ class CollectionNS(UserDict): def __getitem__(self, key): self.targetNamespace = self.parent().targetNamespace - if type(key) is types.IntType: + if isinstance(key, types.IntType): return self.list[key] elif self.__isSequence(key): - nsuri,name = key + nsuri, name = key return self.data[nsuri][name] return self.data[self.parent().targetNamespace][key] @@ -1185,17 +1191,17 @@ class CollectionNS(UserDict): item.parent = weakref.ref(self) self.list.append(item) targetNamespace = getattr(item, 'targetNamespace', self.parent().targetNamespace) - if not self.data.has_key(targetNamespace): + if not targetNamespace in self.data: self.data[targetNamespace] = {} self.data[targetNamespace][key] = item def __isSequence(self, key): - return (type(key) in (types.TupleType,types.ListType) and len(key) == 2) + return (type(key) in (types.TupleType, types.ListType) and len(key) == 2) def keys(self): keys = [] for tns in self.data.keys(): - keys.append(map(lambda i: (tns,self._func(i)), self.data[tns].values())) + keys.append(map(lambda i: (tns, self._func(i)), self.data[tns].values())) return keys def items(self): @@ -1205,12 +1211,10 @@ class CollectionNS(UserDict): return self.list - # This is a runtime guerilla patch for pulldom (used by minidom) so # that xml namespace declaration attributes are not lost in parsing. # We need them to do correct QName linking for XML Schema and WSDL. # The patch has been submitted to SF for the next Python version. - from xml.dom.pulldom import PullDOM, START_ELEMENT if 1: def startPrefixMapping(self, prefix, uri): @@ -1222,7 +1226,7 @@ if 1: PullDOM.startPrefixMapping = startPrefixMapping - def startElementNS(self, name, tagName , attrs): + def startElementNS(self, name, tagName, attrs): # Retrieve xml namespace declaration attributes. xmlns_uri = 'http://www.w3.org/2000/xmlns/' xmlns_attrs = getattr(self, '_xmlns_attrs', None) @@ -1253,7 +1257,7 @@ if 1: else: node = self.buildDocument(None, localname) - for aname,value in attrs.items(): + for aname, value in attrs.items(): a_uri, a_localname = aname if a_uri == xmlns_uri: if a_localname == 'xmlns': @@ -1294,7 +1298,7 @@ if 1: # ('http://www.w3.org/2000/xmlns/', 'xmlns') # # xml.dom.minidom.Attr.nodeName = xmlns:xsd -# xml.dom.minidom.Attr.value = = http://www.w3.org/2001/XMLSchema +# xml.dom.minidom.Attr.value = = http://www.w3.org/2001/XMLSchema if 1: def _clone_node(node, deep, newOwnerDocument): @@ -1385,4 +1389,3 @@ if 1: return clone xml.dom.minidom._clone_node = _clone_node - diff --git a/src/wstools/WSDLTools.py b/src/wstools/WSDLTools.py index 3dd651a..2dc7fe2 100755 --- a/src/wstools/WSDLTools.py +++ b/src/wstools/WSDLTools.py @@ -20,7 +20,7 @@ class WSDLReader: """A WSDLReader creates WSDL instances from urls and xml data.""" # Custom subclasses of WSDLReader may wish to implement a caching - # strategy or other optimizations. Because application needs vary + # strategy or other optimizations. Because application needs vary # so widely, we don't try to provide any caching by default. def loadFromStream(self, stream, name=None): @@ -55,6 +55,7 @@ class WSDLReader: file.close() return wsdl + class WSDL: """A WSDL object models a WSDL service description. WSDL objects may be created manually or loaded from an xml representation @@ -82,7 +83,7 @@ class WSDL: version = '1.1' def addService(self, name, documentation='', targetNamespace=None): - if self.services.has_key(name): + if name in self.services: raise WSDLError( 'Duplicate service element: %s' % name ) @@ -93,7 +94,7 @@ class WSDL: return item def addMessage(self, name, documentation='', targetNamespace=None): - if self.messages.has_key(name): + if name in self.messages: raise WSDLError( 'Duplicate message element: %s.' % name ) @@ -104,7 +105,7 @@ class WSDL: return item def addPortType(self, name, documentation='', targetNamespace=None): - if self.portTypes.has_key(name): + if name in self.portTypes: raise WSDLError( 'Duplicate portType element: name' ) @@ -115,7 +116,7 @@ class WSDL: return item def addBinding(self, name, type, documentation='', targetNamespace=None): - if self.bindings.has_key(name): + if name in self.bindings: raise WSDLError( 'Duplicate binding element: %s' % name ) @@ -133,11 +134,11 @@ class WSDL: def toDom(self): """ Generate a DOM representation of the WSDL instance. Not dealing with generating XML Schema, thus the targetNamespace - of all XML Schema elements or types used by WSDL message parts + of all XML Schema elements or types used by WSDL message parts needs to be specified via import information items. """ namespaceURI = DOM.GetWSDLUri(self.version) - self.document = DOM.createDocument(namespaceURI ,'wsdl:definitions') + self.document = DOM.createDocument(namespaceURI, 'wsdl:definitions') # Set up a couple prefixes for easy reading. child = DOM.getElement(self.document, None) @@ -146,12 +147,12 @@ class WSDL: child.setAttributeNS(XMLNS.BASE, 'xmlns:xsd', 'http://www.w3.org/1999/XMLSchema') child.setAttributeNS(XMLNS.BASE, 'xmlns:soap', 'http://schemas.xmlsoap.org/wsdl/soap/') child.setAttributeNS(XMLNS.BASE, 'xmlns:tns', self.targetNamespace) - + if self.name: child.setAttributeNS(None, 'name', self.name) # wsdl:import - for item in self.imports: + for item in self.imports: item.toDom() # wsdl:message for item in self.messages: @@ -186,13 +187,13 @@ class WSDL: self.name = DOM.getAttr(definitions, 'name', None, None) self.documentation = GetDocumentation(definitions) - # + # # Retrieve all 's, append all children of imported - # document to main document. First iteration grab all original - # 's from document, second iteration grab all - # "imported" from document, etc break out when + # document to main document. First iteration grab all original + # 's from document, second iteration grab all + # "imported" from document, etc break out when # no more 's. - # + # imported = [] base_location = self.location do_it = True @@ -203,7 +204,7 @@ class WSDL: if base_location is not None: location = basejoin(base_location, location) - + if location not in imported: do_it = True self._import(document, element, base_location) @@ -213,10 +214,10 @@ class WSDL: base_location = None - # - # No more 's, now load up all other + # + # No more 's, now load up all other # WSDL information items. - # + # for element in DOM.getElements(definitions, None, None): targetNamespace = DOM.getAttr(element, 'targetNamespace') localName = element.localName @@ -296,15 +297,15 @@ class WSDL: def _import(self, document, element, base_location=None): '''Algo take element's children, clone them, - and add them to the main document. Support for relative + and add them to the main document. Support for relative locations is a bit complicated. The orig document context is lost, so we need to store base location in DOM elements - representing , by creating a special temporary + representing , by creating a special temporary "base-location" attribute, and , by resolving the relative "location" and storing it as "location". - + document -- document we are loading - element -- DOM Element representing + element -- DOM Element representing base_location -- location of document from which this was gleaned. ''' @@ -344,7 +345,7 @@ class WSDL: parent = element.parentNode parent.removeChild(element) - + for node in imported_nodes: if node.nodeType != node.ELEMENT_NODE: continue @@ -369,6 +370,7 @@ class WSDL: importdoc.unlink() return location + class Element: """A class that provides common functions for WSDL element classes.""" def __init__(self, name=None, documentation=''): @@ -379,7 +381,7 @@ class Element: def addExtension(self, item): item.parent = weakref.ref(self) self.extensions.append(item) - + def getWSDL(self): """Return the WSDL object that contains this information item.""" parent = self @@ -387,9 +389,11 @@ class Element: # skip any collections if isinstance(parent, WSDL): return parent - try: parent = parent.parent() - except: break - + try: + parent = parent.parent() + except: + break + return None @@ -413,7 +417,8 @@ class ImportElement(Element): class Types(Collection): - default = lambda self,k: k.targetNamespace + default = lambda self, k: k.targetNamespace + def __init__(self, parent): Collection.__init__(self, parent) self.documentation = '' @@ -434,7 +439,7 @@ class Message(Element): self.parts = Collection(self) def addPart(self, name, type=None, element=None): - if self.parts.has_key(name): + if name in self.parts: raise WSDLError( 'Duplicate message part element: %s' % name ) @@ -510,13 +515,13 @@ class MessagePart(Element): def getTypeDefinition(self): wsdl = self.getWSDL() - nsuri,name = self.type + nsuri, name = self.type schema = wsdl.types.get(nsuri, {}) return schema.get(name) def getElementDeclaration(self): wsdl = self.getWSDL() - nsuri,name = self.element + nsuri, name = self.element schema = wsdl.types.get(nsuri, {}) return schema.get(name) @@ -528,13 +533,13 @@ class MessagePart(Element): epc.setAttributeNS(None, 'name', self.name) if self.element is not None: - ns,name = self.element + ns, name = self.element prefix = epc.getPrefix(ns) - epc.setAttributeNS(None, 'element', '%s:%s'%(prefix,name)) + epc.setAttributeNS(None, 'element', '%s:%s' % (prefix, name)) elif self.type is not None: - ns,name = self.type + ns, name = self.type prefix = epc.getPrefix(ns) - epc.setAttributeNS(None, 'type', '%s:%s'%(prefix,name)) + epc.setAttributeNS(None, 'type', '%s:%s' % (prefix, name)) class PortType(Element): @@ -598,7 +603,8 @@ class PortType(Element): message = ParseQName(msgref, item) for WSA in WSA_LIST + WSAW_LIST: action = DOM.getAttr(item, 'Action', WSA.ADDRESS, None) - if action: break + if action: + break operation.setInput(message, name, docs, action) item = DOM.getElement(element, 'output', None, None) @@ -609,7 +615,8 @@ class PortType(Element): message = ParseQName(msgref, item) for WSA in WSA_LIST + WSAW_LIST: action = DOM.getAttr(item, 'Action', WSA.ADDRESS, None) - if action: break + if action: + break operation.setOutput(message, name, docs, action) for item in DOM.getElements(element, 'fault', None): @@ -619,9 +626,10 @@ class PortType(Element): message = ParseQName(msgref, item) for WSA in WSA_LIST + WSAW_LIST: action = DOM.getAttr(item, 'Action', WSA.ADDRESS, None) - if action: break + if action: + break operation.addFault(message, name, docs, action) - + def toDom(self): wsdl = self.getWSDL() @@ -629,16 +637,15 @@ class PortType(Element): epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), 'portType') epc.setAttributeNS(None, 'name', self.name) if self.resourceProperties: - ns,name = self.resourceProperties + ns, name = self.resourceProperties prefix = epc.getPrefix(ns) - epc.setAttributeNS(WSRF.PROPERTIES.LATEST, 'ResourceProperties', - '%s:%s'%(prefix,name)) + epc.setAttributeNS(WSRF.PROPERTIES.LATEST, 'ResourceProperties', + '%s:%s' % (prefix, name)) for op in self.operations: op.toDom(epc._getNode()) - class Operation(Element): def __init__(self, name, documentation='', parameterOrder=None): Element.__init__(self, name, documentation) @@ -683,7 +690,7 @@ class Operation(Element): return wsdl.messages[self.faults[name].message] def addFault(self, message, name, documentation='', action=None): - if self.faults.has_key(name): + if name in self.faults: raise WSDLError( 'Duplicate fault element: %s' % name ) @@ -709,11 +716,11 @@ class Operation(Element): epc.setAttributeNS(None, 'name', self.name) node = epc._getNode() if self.input: - self.input.toDom(node) + self.input.toDom(node) if self.output: - self.output.toDom(node) + self.output.toDom(node) for fault in self.faults: - fault.toDom(node) + fault.toDom(node) class MessageRole(Element): @@ -722,7 +729,7 @@ class MessageRole(Element): self.message = message self.type = type self.action = action - + def getWSDL(self): """Return the WSDL object that contains this information item.""" parent = self @@ -730,13 +737,15 @@ class MessageRole(Element): # skip any collections if isinstance(parent, WSDL): return parent - try: parent = parent.parent() - except: break - + try: + parent = parent.parent() + except: + break + return None def getMessage(self): - """Return the WSDL object that represents the attribute message + """Return the WSDL object that represents the attribute message (namespaceURI, name) tuple """ wsdl = self.getWSDL() @@ -748,18 +757,18 @@ class MessageRole(Element): ep = ElementProxy(None, node) epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), self.type) if not isinstance(self.message, basestring) and len(self.message) == 2: - ns,name = self.message + ns, name = self.message prefix = epc.getPrefix(ns) - epc.setAttributeNS(None, 'message', '%s:%s' %(prefix,name)) + epc.setAttributeNS(None, 'message', '%s:%s' % (prefix, name)) else: epc.setAttributeNS(None, 'message', self.message) if self.action: epc.setAttributeNS(WSA.ADDRESS, 'Action', self.action) - + if self.name: epc.setAttributeNS(None, 'name', self.name) - + class Binding(Element): def __init__(self, name, type, documentation=''): @@ -782,7 +791,7 @@ class Binding(Element): return None def findBindings(self, kind): - return [ item for item in self.extensions if isinstance(item, kind) ] + return [item for item in self.extensions if isinstance(item, kind)] def addOperationBinding(self, name, documentation=''): item = OperationBinding(name, documentation) @@ -844,9 +853,9 @@ class Binding(Element): epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), 'binding') epc.setAttributeNS(None, 'name', self.name) - ns,name = self.type + ns, name = self.type prefix = epc.getPrefix(ns) - epc.setAttributeNS(None, 'type', '%s:%s' %(prefix,name)) + epc.setAttributeNS(None, 'type', '%s:%s' % (prefix, name)) node = epc._getNode() for ext in self.extensions: @@ -866,7 +875,6 @@ class OperationBinding(Element): # """Return the WSDL object that contains this binding.""" # return self.parent().parent().parent().parent() - def getBinding(self): """Return the parent Binding object of the operation binding.""" return self.parent().parent() @@ -874,7 +882,7 @@ class OperationBinding(Element): def getOperation(self): """Return the abstract Operation associated with this binding.""" return self.getBinding().getPortType().operations[self.name] - + def findBinding(self, kind): for item in self.extensions: if isinstance(item, kind): @@ -882,7 +890,7 @@ class OperationBinding(Element): return None def findBindings(self, kind): - return [ item for item in self.extensions if isinstance(item, kind) ] + return [item for item in self.extensions if isinstance(item, kind)] def addInputBinding(self, binding): if self.input is None: @@ -951,7 +959,7 @@ class MessageRoleBinding(Element): return None def findBindings(self, kind): - return [ item for item in self.extensions if isinstance(item, kind) ] + return [item for item in self.extensions if isinstance(item, kind)] def load_ex(self, elements): for e in elements: @@ -1038,7 +1046,8 @@ class MessageRoleBinding(Element): node = epc._getNode() for item in self.extensions: - if item: item.toDom(node) + if item: + item.toDom(node) class Service(Element): @@ -1138,9 +1147,9 @@ class Port(Element): epc = ep.createAppendElement(DOM.GetWSDLUri(wsdl.version), "port") epc.setAttributeNS(None, "name", self.name) - ns,name = self.binding + ns, name = self.binding prefix = epc.getPrefix(ns) - epc.setAttributeNS(None, "binding", "%s:%s" %(prefix,name)) + epc.setAttributeNS(None, "binding", "%s:%s" % (prefix, name)) node = epc._getNode() for ext in self.extensions: @@ -1164,6 +1173,7 @@ class SoapBinding: if self.style: epc.setAttributeNS(None, "style", self.style) + class SoapAddressBinding: def __init__(self, location): self.location = location @@ -1230,10 +1240,10 @@ class SoapFaultBinding: self.namespace = namespace self.name = name self.use = use - + def getWSDL(self): return self.parent().getWSDL() - + def toDom(self, node): wsdl = self.getWSDL() ep = ElementProxy(None, node) @@ -1260,6 +1270,7 @@ class SoapHeaderBinding: tagname = 'header' + class SoapHeaderFaultBinding(SoapHeaderBinding): tagname = 'headerfault' @@ -1268,6 +1279,7 @@ class HttpBinding: def __init__(self, verb): self.verb = verb + class HttpAddressBinding: def __init__(self, location): self.location = location @@ -1277,6 +1289,7 @@ class HttpOperationBinding: def __init__(self, location): self.location = location + class HttpUrlReplacementBinding: pass @@ -1346,12 +1359,12 @@ class WSDLError(Exception): pass - def DeclareNSPrefix(writer, prefix, nsuri): if writer.hasNSPrefix(nsuri): return writer.declareNSPrefix(prefix, nsuri) + def ParseTypeRef(value, element): parts = value.split(':', 1) if len(parts) == 1: @@ -1359,6 +1372,7 @@ def ParseTypeRef(value, element): nsuri = DOM.findNamespaceURI(parts[0], element) return (nsuri, parts[1]) + def ParseQName(value, element): nameref = value.split(':', 1) if len(nameref) == 2: @@ -1366,18 +1380,21 @@ def ParseQName(value, element): name = nameref[-1] else: nsuri = DOM.findTargetNS(element) - name = nameref[-1] + name = nameref[-1] return nsuri, name + def GetDocumentation(element): docnode = DOM.getElement(element, 'documentation', None, None) if docnode is not None: return DOM.getElementText(docnode) return '' + def GetExtensions(element): - return [ item for item in DOM.getElements(element, None, None) - if item.namespaceURI != DOM.NS_WSDL ] + return [item for item in DOM.getElements(element, None, None) + if item.namespaceURI != DOM.NS_WSDL] + def GetWSAActionFault(operation, name): """Find wsa:Action attribute, and return value or WSA.FAULT @@ -1388,6 +1405,7 @@ def GetWSAActionFault(operation, name): return attr return WSA.FAULT + def GetWSAActionInput(operation): """Find wsa:Action attribute, and return value or the default.""" attr = operation.input.action @@ -1400,8 +1418,9 @@ def GetWSAActionInput(operation): if not msgName: msgName = operation.name + 'Request' if targetNamespace.endswith('/'): - return '%s%s/%s' %(targetNamespace, ptName, msgName) - return '%s/%s/%s' %(targetNamespace, ptName, msgName) + return '%s%s/%s' % (targetNamespace, ptName, msgName) + return '%s/%s/%s' % (targetNamespace, ptName, msgName) + def GetWSAActionOutput(operation): """Find wsa:Action attribute, and return value or the default.""" @@ -1414,26 +1433,28 @@ def GetWSAActionOutput(operation): if not msgName: msgName = operation.name + 'Response' if targetNamespace.endswith('/'): - return '%s%s/%s' %(targetNamespace, ptName, msgName) - return '%s/%s/%s' %(targetNamespace, ptName, msgName) + return '%s%s/%s' % (targetNamespace, ptName, msgName) + return '%s/%s/%s' % (targetNamespace, ptName, msgName) + def FindExtensions(object, kind, t_type=type(())): if isinstance(kind, t_type): result = [] namespaceURI, name = kind - return [ item for item in object.extensions + return [item for item in object.extensions if hasattr(item, 'nodeType') \ and DOM.nsUriMatch(namespaceURI, item.namespaceURI) \ - and item.name == name ] - return [ item for item in object.extensions if isinstance(item, kind) ] + and item.name == name] + return [item for item in object.extensions if isinstance(item, kind)] + def FindExtension(object, kind, t_type=type(())): if isinstance(kind, t_type): namespaceURI, name = kind for item in object.extensions: if hasattr(item, 'nodeType') \ - and DOM.nsUriMatch(namespaceURI, item.namespaceURI) \ - and item.name == name: + and DOM.nsUriMatch(namespaceURI, item.namespaceURI) \ + and item.name == name: return item else: for item in object.extensions: @@ -1443,7 +1464,7 @@ def FindExtension(object, kind, t_type=type(())): class SOAPCallInfo: - """SOAPCallInfo captures the important binding information about a + """SOAPCallInfo captures the important binding information about a SOAP operation, in a structure that is easier to work with than raw WSDL structures.""" @@ -1557,12 +1578,12 @@ def callInfoFromWSDL(port, name): addrbinding = port.getAddressBinding() if not isinstance(addrbinding, SoapAddressBinding): - raise ValueError, 'Unsupported binding type.' + raise ValueError('Unsupported binding type.') callinfo.location = addrbinding.location soapbinding = binding.findBinding(SoapBinding) if soapbinding is None: - raise ValueError, 'Missing soap:binding element.' + raise ValueError('Missing soap:binding element.') callinfo.transport = soapbinding.transport callinfo.style = soapbinding.style or 'document' @@ -1579,7 +1600,7 @@ def callInfoFromWSDL(port, name): mime = msgrole.findBinding(MimeMultipartRelatedBinding) if mime is not None: - raise ValueError, 'Mime bindings are not supported.' + raise ValueError('Mime bindings are not supported.') else: for item in msgrole.findBindings(SoapHeaderBinding): part = messages[item.message].parts[item.part] @@ -1587,13 +1608,13 @@ def callInfoFromWSDL(port, name): part.name, part.element or part.type, item.namespace, - element_type = part.element and 1 or 0 + element_type=part.element and 1 or 0 ) header.encodingStyle = item.encodingStyle body = msgrole.findBinding(SoapBodyBinding) if body is None: - raise ValueError, 'Missing soap:body binding.' + raise ValueError('Missing soap:body binding.') callinfo.encodingStyle = body.encodingStyle callinfo.namespace = body.namespace callinfo.use = body.use @@ -1609,7 +1630,7 @@ def callInfoFromWSDL(port, name): callinfo.addInParameter( part.name, part.element or part.type, - element_type = part.element and 1 or 0 + element_type=part.element and 1 or 0 ) if operation.output is not None: @@ -1626,12 +1647,12 @@ def callInfoFromWSDL(port, name): "Recieved message not defined in the WSDL schema.", \ "Adding it." print "Message:", operation.output.message - + msgrole = opbinding.output mime = msgrole.findBinding(MimeMultipartRelatedBinding) if mime is not None: - raise ValueError, 'Mime bindings are not supported.' + raise ValueError('Mime bindings are not supported.') else: for item in msgrole.findBindings(SoapHeaderBinding): part = messages[item.message].parts[item.part] @@ -1639,13 +1660,13 @@ def callInfoFromWSDL(port, name): part.name, part.element or part.type, item.namespace, - element_type = part.element and 1 or 0 + element_type=part.element and 1 or 0 ) header.encodingStyle = item.encodingStyle body = msgrole.findBinding(SoapBodyBinding) if body is None: - raise ValueError, 'Missing soap:body binding.' + raise ValueError('Missing soap:body binding.') callinfo.encodingStyle = body.encodingStyle callinfo.namespace = body.namespace callinfo.use = body.use @@ -1662,7 +1683,7 @@ def callInfoFromWSDL(port, name): callinfo.addOutParameter( part.name, part.element or part.type, - element_type = part.element and 1 or 0 + element_type=part.element and 1 or 0 ) return callinfo diff --git a/src/wstools/XMLSchema.py b/src/wstools/XMLSchema.py index ff696f0..1f23ea4 100755 --- a/src/wstools/XMLSchema.py +++ b/src/wstools/XMLSchema.py @@ -1,7 +1,8 @@ +#!/usr/bin/env python # Copyright (c) 2003, The Regents of the University of California, # through Lawrence Berkeley National Laboratory (subject to receipt of # any required approvals from the U.S. Dept. of Energy). All rights -# reserved. +# reserved. # # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. # @@ -14,7 +15,11 @@ ident = "$Id$" -import types, weakref, sys, warnings +import types +import weakref +import sys +import warnings +import logging from Namespaces import SCHEMA, XMLNS, SOAP, APACHE from Utility import DOM, DOMException, Collection, SplitQName, basejoin from StringIO import StringIO @@ -26,18 +31,20 @@ except ImportError: class RLock: def acquire(): pass + def release(): pass -# +# # Collections in XMLSchema class -# +# TYPES = 'types' ATTRIBUTE_GROUPS = 'attr_groups' ATTRIBUTES = 'attr_decl' ELEMENTS = 'elements' MODEL_GROUPS = 'model_groups' -BUILT_IN_NAMESPACES = [SOAP.ENC,] + SCHEMA.XSD_LIST + [APACHE.AXIS_NS] +BUILT_IN_NAMESPACES = [SOAP.ENC, ] + SCHEMA.XSD_LIST + [APACHE.AXIS_NS] + def GetSchema(component): """convience function for finding the parent XMLSchema instance. @@ -46,13 +53,14 @@ def GetSchema(component): while not isinstance(parent, XMLSchema): parent = parent._parent() return parent - + + class SchemaReader: """A SchemaReader creates XMLSchema objects from urls and xml data. """ - + namespaceToSchema = {} - + def __init__(self, domReader=None, base_url=None): """domReader -- class must implement DOMAdapterInterface base_url -- base url string @@ -68,16 +76,16 @@ class SchemaReader: """Add dictionary of imports to schema instance. schema -- XMLSchema instance """ - for ns,val in schema.imports.items(): - if self._imports.has_key(ns): + for ns, val in schema.imports.items(): + if ns in self._imports: schema.addImportSchema(self._imports[ns]) def __setIncludes(self, schema): """Add dictionary of includes to schema instance. schema -- XMLSchema instance """ - for schemaLocation, val in schema.includes.items(): - if self._includes.has_key(schemaLocation): + for schemaLocation, val in schema.includes.items(): + if schemaLocation in self._includes: schema.addIncludeSchema(schemaLocation, self._imports[schemaLocation]) def addSchemaByLocation(self, location, schema): @@ -101,7 +109,7 @@ class SchemaReader: schema.setBaseUrl(self.__base_url) schema.load(reader) return schema - + def loadFromStream(self, file, url=None): """Return an XMLSchema instance loaded from a file object. file -- file object @@ -111,7 +119,7 @@ class SchemaReader: reader.loadDocument(file) schema = XMLSchema() if url is not None: - schema.setBaseUrl(url) + schema.setBaseUrl(url) schema.load(reader) self.__setIncludes(schema) self.__setImports(schema) @@ -130,7 +138,7 @@ class SchemaReader: """ reader = self.__readerClass() if self.__base_url: - url = basejoin(self.__base_url,url) + url = basejoin(self.__base_url, url) reader.loadFromURL(url) schema = schema or XMLSchema() @@ -145,7 +153,7 @@ class SchemaReader: filename -- name of file to open """ if self.__base_url: - filename = basejoin(self.__base_url,filename) + filename = basejoin(self.__base_url, filename) file = open(filename, 'rb') try: schema = self.loadFromStream(file, filename) @@ -155,67 +163,67 @@ class SchemaReader: return schema -class SchemaError(Exception): +class SchemaError(Exception): pass -class NoSchemaLocationWarning(Exception): + +class NoSchemaLocationWarning(Exception): pass ########################### -# DOM Utility Adapters +# DOM Utility Adapters ########################## class DOMAdapterInterface: def hasattr(self, attr, ns=None): - """return true if node has attribute + """return true if node has attribute attr -- attribute to check for ns -- namespace of attribute, by default None """ - raise NotImplementedError, 'adapter method not implemented' + raise NotImplementedError('adapter method not implemented') def getContentList(self, *contents): """returns an ordered list of child nodes *contents -- list of node names to return """ - raise NotImplementedError, 'adapter method not implemented' + raise NotImplementedError('adapter method not implemented') def setAttributeDictionary(self, attributes): """set attribute dictionary """ - raise NotImplementedError, 'adapter method not implemented' + raise NotImplementedError('adapter method not implemented') def getAttributeDictionary(self): """returns a dict of node's attributes """ - raise NotImplementedError, 'adapter method not implemented' + raise NotImplementedError('adapter method not implemented') def getNamespace(self, prefix): """returns namespace referenced by prefix. """ - raise NotImplementedError, 'adapter method not implemented' + raise NotImplementedError('adapter method not implemented') def getTagName(self): """returns tagName of node """ - raise NotImplementedError, 'adapter method not implemented' - + raise NotImplementedError('adapter method not implemented') def getParentNode(self): """returns parent element in DOMAdapter or None """ - raise NotImplementedError, 'adapter method not implemented' + raise NotImplementedError('adapter method not implemented') def loadDocument(self, file): """load a Document from a file object file -- """ - raise NotImplementedError, 'adapter method not implemented' + raise NotImplementedError('adapter method not implemented') def loadFromURL(self, url): """load a Document from an url url -- URL to dereference """ - raise NotImplementedError, 'adapter method not implemented' + raise NotImplementedError('adapter method not implemented') class DOMAdapter(DOMAdapterInterface): @@ -233,16 +241,16 @@ class DOMAdapter(DOMAdapterInterface): def getNode(self): return self.__node - + def hasattr(self, attr, ns=None): - """attr -- attribute + """attr -- attribute ns -- optional namespace, None means unprefixed attribute. """ if not self.__attributes: self.setAttributeDictionary() if ns: - return self.__attributes.get(ns,{}).has_key(attr) - return self.__attributes.has_key(attr) + return attr in self.__attributes.get(ns, {}) + return attr in self.__attributes def getContentList(self, *contents): nodes = [] @@ -283,11 +291,10 @@ class DOMAdapter(DOMAdapterInterface): namespace = DOM.findNamespaceURI(prefix, self.__node) except DOMException, ex: if prefix != 'xml': - raise SchemaError, '%s namespace not declared for %s'\ - %(prefix, self.__node._get_tagName()) + raise SchemaError('%s namespace not declared for %s' % (prefix, self.__node._get_tagName())) namespace = XMLNS.XML return namespace - + def loadDocument(self, file): self.__node = DOM.loadDocument(file) if hasattr(self.__node, 'documentElement'): @@ -298,10 +305,10 @@ class DOMAdapter(DOMAdapterInterface): if hasattr(self.__node, 'documentElement'): self.__node = self.__node.documentElement - -class XMLBase: + +class XMLBase: """ These class variables are for string indentation. - """ + """ tag = None __indent = 0 __rlock = RLock() @@ -310,106 +317,124 @@ class XMLBase: XMLBase.__rlock.acquire() XMLBase.__indent += 1 tmp = "<" + str(self.__class__) + '>\n' - for k,v in self.__dict__.items(): - tmp += "%s* %s = %s\n" %(XMLBase.__indent*' ', k, v) - XMLBase.__indent -= 1 + for k, v in self.__dict__.items(): + tmp += "%s* %s = %s\n" % (XMLBase.__indent * ' ', k, v) + XMLBase.__indent -= 1 XMLBase.__rlock.release() return tmp -"""Marker Interface: can determine something about an instances properties by using +"""Marker Interface: can determine something about an instances properties by using the provided convenience functions. """ -class DefinitionMarker: + + +class DefinitionMarker: """marker for definitions """ pass -class DeclarationMarker: + +class DeclarationMarker: """marker for declarations """ pass -class AttributeMarker: + +class AttributeMarker: """marker for attributes """ pass -class AttributeGroupMarker: + +class AttributeGroupMarker: """marker for attribute groups """ pass -class WildCardMarker: + +class WildCardMarker: """marker for wildcards """ pass -class ElementMarker: + +class ElementMarker: """marker for wildcards """ pass -class ReferenceMarker: + +class ReferenceMarker: """marker for references """ pass -class ModelGroupMarker: + +class ModelGroupMarker: """marker for model groups """ pass -class AllMarker(ModelGroupMarker): + +class AllMarker(ModelGroupMarker): """marker for all model group """ pass -class ChoiceMarker(ModelGroupMarker): + +class ChoiceMarker(ModelGroupMarker): """marker for choice model group """ pass -class SequenceMarker(ModelGroupMarker): + +class SequenceMarker(ModelGroupMarker): """marker for sequence model group """ pass -class ExtensionMarker: + +class ExtensionMarker: """marker for extensions """ pass -class RestrictionMarker: + +class RestrictionMarker: """marker for restrictions """ facets = ['enumeration', 'length', 'maxExclusive', 'maxInclusive',\ 'maxLength', 'minExclusive', 'minInclusive', 'minLength',\ 'pattern', 'fractionDigits', 'totalDigits', 'whiteSpace'] -class SimpleMarker: + +class SimpleMarker: """marker for simple type information """ pass -class ListMarker: + +class ListMarker: """marker for simple type list """ pass -class UnionMarker: + +class UnionMarker: """marker for simple type Union """ pass -class ComplexMarker: +class ComplexMarker: """marker for complex type information """ pass -class LocalMarker: + +class LocalMarker: """marker for complex type information """ pass @@ -476,7 +501,7 @@ class MarkerInterface: ######################################################### class XMLSchemaComponent(XMLBase, MarkerInterface): """ - class variables: + class variables: required -- list of required attributes attributes -- dict of default attribute values, including None. Value can be a function for runtime dependencies. @@ -507,29 +532,29 @@ class XMLSchemaComponent(XMLBase, MarkerInterface): and not (type(self.__class__.required) == type(XMLSchemaComponent.required)\ and type(self.__class__.attributes) == type(XMLSchemaComponent.attributes)\ and type(self.__class__.contents) == type(XMLSchemaComponent.contents)): - raise RuntimeError, 'Bad type for a class variable in %s' %self.__class__ + raise RuntimeError('Bad type for a class variable in %s' % self.__class__) def getItemTrace(self): """Returns a node trace up to the item. """ item, path, name, ref = self, [], 'name', 'ref' - while not isinstance(item,XMLSchema) and not isinstance(item,WSDLToolsAdapter): + while not isinstance(item, XMLSchema) and not isinstance(item, WSDLToolsAdapter): attr = item.getAttribute(name) if not attr: attr = item.getAttribute(ref) if not attr: - path.append('<%s>' %(item.tag)) - else: - path.append('<%s ref="%s">' %(item.tag, attr)) + path.append('<%s>' % (item.tag)) + else: + path.append('<%s ref="%s">' % (item.tag, attr)) else: - path.append('<%s name="%s">' %(item.tag,attr)) + path.append('<%s name="%s">' % (item.tag, attr)) item = item._parent() try: tns = item.getTargetNamespace() - except: + except: tns = '' - path.append('<%s targetNamespace="%s">' %(item.tag, tns)) + path.append('<%s targetNamespace="%s">' % (item.tag, tns)) path.reverse() return ''.join(path) @@ -585,7 +610,7 @@ class XMLSchemaComponent(XMLBase, MarkerInterface): return obj = self.getSchemaItem(collection, tdc.getTargetNamespace(), tdc.getName()) - if obj: + if obj: return obj # raise SchemaError, 'No schema item "%s" in collection %s' %(tdc, collection) @@ -595,7 +620,7 @@ class XMLSchemaComponent(XMLBase, MarkerInterface): """returns object instance representing namespace, name, or if does not exist return None if built-in, else raise SchemaError. - + namespace -- namespace item defined in. name -- name of item. collection -- collection in parent Schema instance to search. @@ -605,48 +630,45 @@ class XMLSchemaComponent(XMLBase, MarkerInterface): try: obj = getattr(parent, collection)[name] except KeyError, ex: - raise KeyError, 'targetNamespace(%s) collection(%s) has no item(%s)'\ - %(namespace, collection, name) - + raise KeyError('targetNamespace(%s) collection(%s) has no item(%s)' % (namespace, collection, name)) + return obj - - if not parent.imports.has_key(namespace): - if namespace in BUILT_IN_NAMESPACES: + + if not namespace in parent.imports: + if namespace in BUILT_IN_NAMESPACES: # built-in just return # WARNING: expecting import if "redefine" or add to built-in namespace. return - - raise SchemaError, 'schema "%s" does not import namespace "%s"' %( - parent.targetNamespace, namespace) - + + raise SchemaError('schema "%s" does not import namespace "%s"' % ( + parent.targetNamespace, namespace)) + # Lazy Eval schema = parent.imports[namespace] if not isinstance(schema, XMLSchema): - schema = schema.getSchema() + schema = schema.getSchema() if schema is not None: parent.imports[namespace] = schema - + if schema is None: if namespace in BUILT_IN_NAMESPACES: # built-in just return return - - raise SchemaError, 'no schema instance for imported namespace (%s).'\ - %(namespace) - + + raise SchemaError('no schema instance for imported namespace (%s).' % (namespace)) + if not isinstance(schema, XMLSchema): - raise TypeError, 'expecting XMLSchema instance not "%r"' %schema - + raise TypeError('expecting XMLSchema instance not "%r"' % schema) + try: obj = getattr(schema, collection)[name] except KeyError, ex: - raise KeyError, 'targetNamespace(%s) collection(%s) has no item(%s)'\ - %(namespace, collection, name) - + raise KeyError('targetNamespace(%s) collection(%s) has no item(%s)' % (namespace, collection, name)) + return obj def getXMLNS(self, prefix=None): - """deference prefix or by default xmlns, returns namespace. + """deference prefix or by default xmlns, returns namespace. """ if prefix == XMLSchemaComponent.xml: return XMLNS.XML @@ -660,7 +682,7 @@ class XMLSchemaComponent(XMLBase, MarkerInterface): if not ns and isinstance(parent, WSDLToolsAdapter): if prefix is None: return '' - raise SchemaError, 'unknown prefix %s' %prefix + raise SchemaError('unknown prefix %s' % prefix) return ns def getAttribute(self, attribute): @@ -668,7 +690,7 @@ class XMLSchemaComponent(XMLBase, MarkerInterface): """ if type(attribute) in (list, tuple): if len(attribute) != 2: - raise LookupError, 'To access attributes must use name or (namespace,name)' + raise LookupError('To access attributes must use name or (namespace,name)') ns_dict = self.attributes.get(attribute[0]) if ns_dict is None: @@ -679,7 +701,7 @@ class XMLSchemaComponent(XMLBase, MarkerInterface): return self.attributes.get(attribute) def getAttributeQName(self, attribute): - """return requested attribute value as (namespace,name) or None + """return requested attribute value as (namespace,name) or None """ qname = self.getAttribute(attribute) if isinstance(qname, TypeDescriptionComponent) is True: @@ -687,48 +709,46 @@ class XMLSchemaComponent(XMLBase, MarkerInterface): if qname is None: return None - prefix,ncname = SplitQName(qname) + prefix, ncname = SplitQName(qname) namespace = self.getXMLNS(prefix) - return TypeDescriptionComponent((namespace,ncname)) + return TypeDescriptionComponent((namespace, ncname)) def getAttributeName(self): """return attribute name or None """ return self.getAttribute('name') - + def setAttributes(self, node): - """Sets up attribute dictionary, checks for required attributes and - sets default attribute values. attr is for default attribute values + """Sets up attribute dictionary, checks for required attributes and + sets default attribute values. attr is for default attribute values determined at runtime. - + structure of attributes dictionary ['xmlns'][xmlns_key] -- xmlns namespace - ['xmlns'][prefix] -- declared namespace prefix + ['xmlns'][prefix] -- declared namespace prefix [namespace][prefix] -- attributes declared in a namespace [attribute] -- attributes w/o prefix, default namespaces do - not directly apply to attributes, ie Name can't collide + not directly apply to attributes, ie Name can't collide with QName. """ - self.attributes = {XMLSchemaComponent.xmlns:{}} - for k,v in node.getAttributeDictionary().items(): - prefix,value = SplitQName(k) + self.attributes = {XMLSchemaComponent.xmlns: {}} + for k, v in node.getAttributeDictionary().items(): + prefix, value = SplitQName(k) if value == XMLSchemaComponent.xmlns: self.attributes[value][prefix or XMLSchemaComponent.xmlns_key] = v elif prefix: ns = node.getNamespace(prefix) - if not ns: - raise SchemaError, 'no namespace for attribute prefix %s'\ - %prefix - if not self.attributes.has_key(ns): + if not ns: + raise SchemaError('no namespace for attribute prefix %s' % prefix) + if not ns in self.attributes: self.attributes[ns] = {} - elif self.attributes[ns].has_key(value): - raise SchemaError, 'attribute %s declared multiple times in %s'\ - %(value, ns) + elif value in self.attributes[ns]: + raise SchemaError('attribute %s declared multiple times in %s' % (value, ns)) self.attributes[ns][value] = v - elif not self.attributes.has_key(value): + elif not value in self.attributes: self.attributes[value] = v else: - raise SchemaError, 'attribute %s declared multiple times' %value + raise SchemaError('attribute %s declared multiple times' % value) if not isinstance(self, WSDLToolsAdapter): self.__checkAttributes() @@ -736,14 +756,14 @@ class XMLSchemaComponent(XMLBase, MarkerInterface): #set QNames for k in ['type', 'element', 'base', 'ref', 'substitutionGroup', 'itemType']: - if self.attributes.has_key(k): + if k in self.attributes: prefix, value = SplitQName(self.attributes.get(k)) self.attributes[k] = \ TypeDescriptionComponent((self.getXMLNS(prefix), value)) - #Union, memberTypes is a whitespace separated list of QNames + #Union, memberTypes is a whitespace separated list of QNames for k in ['memberTypes']: - if self.attributes.has_key(k): + if k in self.attributes: qnames = self.attributes[k] self.attributes[k] = [] for qname in qnames.split(): @@ -762,8 +782,8 @@ class XMLSchemaComponent(XMLBase, MarkerInterface): class variable representing attribute is None, then it must be defined as an instance variable. """ - for k,v in self.__class__.attributes.items(): - if v is not None and self.attributes.has_key(k) is False: + for k, v in self.__class__.attributes.items(): + if v is not None and k in self.attributes is False: if isinstance(v, types.FunctionType): self.attributes[k] = v(self) else: @@ -772,33 +792,30 @@ class XMLSchemaComponent(XMLBase, MarkerInterface): def __checkAttributes(self): """Checks that required attributes have been defined, attributes w/default cannot be required. Checks - all defined attributes are legal, attribute + all defined attributes are legal, attribute references are not subject to this test. """ for a in self.__class__.required: - if not self.attributes.has_key(a): - raise SchemaError,\ - 'class instance %s, missing required attribute %s'\ - %(self.__class__, a) - for a,v in self.attributes.items(): + if not a in self.attributes: + raise SchemaError('class instance %s, missing required attribute %s' % (self.__class__, a)) + for a, v in self.attributes.items(): # attribute #other, ie. not in empty namespace if type(v) is dict: continue - + # predefined prefixes xmlns, xml if a in (XMLSchemaComponent.xmlns, XMLNS.XML): continue - + if (a not in self.__class__.attributes.keys()) and not\ (self.isAttribute() and self.isReference()): - raise SchemaError, '%s, unknown attribute(%s,%s)' \ - %(self.getItemTrace(), a, self.attributes[a]) + raise SchemaError('%s, unknown attribute(%s, %s)' % (self.getItemTrace(), a, self.attributes[a])) class WSDLToolsAdapter(XMLSchemaComponent): """WSDL Adapter to grab the attributes from the wsdl document node. """ - attributes = {'name':None, 'targetNamespace':None} + attributes = {'name': None, 'targetNamespace': None} tag = 'definitions' def __init__(self, wsdl): @@ -824,8 +841,8 @@ class Notation(XMLSchemaComponent): annotation? """ required = ['name', 'public'] - attributes = {'id':None, 'name':None, 'public':None, 'system':None} - contents = {'xsd':('annotation')} + attributes = {'id': None, 'name': None, 'public': None, 'system': None} + contents = {'xsd': ('annotation')} tag = 'notation' def __init__(self, parent): @@ -842,7 +859,7 @@ class Notation(XMLSchemaComponent): self.annotation = Annotation(self) self.annotation.fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) class Annotation(XMLSchemaComponent): @@ -857,8 +874,8 @@ class Annotation(XMLSchemaComponent): contents: (documentation | appinfo)* """ - attributes = {'id':None} - contents = {'xsd':('documentation', 'appinfo')} + attributes = {'id': None} + contents = {'xsd': ('documentation', 'appinfo')} tag = 'annotation' def __init__(self, parent): @@ -879,10 +896,9 @@ class Annotation(XMLSchemaComponent): #print_debug('class %s, appinfo skipped' %self.__class__, 5) continue else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) self.content = tuple(content) - class Documentation(XMLSchemaComponent): """ parent: @@ -893,8 +909,8 @@ class Annotation(XMLSchemaComponent): contents: mixed, any """ - attributes = {'source':None, 'xml:lang':None} - contents = {'xsd':('mixed', 'any')} + attributes = {'source': None, 'xml: lang': None} + contents = {'xsd': ('mixed', 'any')} tag = 'documentation' def __init__(self, parent): @@ -915,10 +931,9 @@ class Annotation(XMLSchemaComponent): #print_debug('class %s, any skipped' %self.__class__, 5) continue else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) self.content = tuple(content) - class Appinfo(XMLSchemaComponent): """ parent: @@ -928,8 +943,8 @@ class Annotation(XMLSchemaComponent): contents: mixed, any """ - attributes = {'source':None, 'anyURI':None} - contents = {'xsd':('mixed', 'any')} + attributes = {'source': None, 'anyURI': None} + contents = {'xsd': ('mixed', 'any')} tag = 'appinfo' def __init__(self, parent): @@ -950,7 +965,7 @@ class Annotation(XMLSchemaComponent): #print_debug('class %s, any skipped' %self.__class__, 5) continue else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) self.content = tuple(content) @@ -960,6 +975,7 @@ class XMLSchemaFake: self.targetNamespace = DOM.getAttr(element, 'targetNamespace') self.element = element + class XMLSchema(XMLSchemaComponent): """A schema is a collection of schema components derived from one or more schema documents, that is, one or more element @@ -976,21 +992,21 @@ class XMLSchema(XMLSchemaComponent): targetNamespace -- anyURI attributeFormDefault -- 'qualified' | 'unqualified', 'unqualified' elementFormDefault -- 'qualified' | 'unqualified', 'unqualified' - blockDefault -- '#all' | list of + blockDefault -- '#all' | list of ('substitution | 'extension' | 'restriction') - finalDefault -- '#all' | list of + finalDefault -- '#all' | list of ('extension' | 'restriction' | 'list' | 'union') - + contents: - ((include | import | redefine | annotation)*, - (attribute, attributeGroup, complexType, element, group, + ((include | import | redefine | annotation)*, + (attribute, attributeGroup, complexType, element, group, notation, simpleType)*, annotation*)* attributes -- schema attributes imports -- import statements includes -- include statements - redefines -- + redefines -- types -- global simpleType, complexType definitions elements -- global element declarations attr_decl -- global attribute declarations @@ -998,28 +1014,28 @@ class XMLSchema(XMLSchemaComponent): model_groups -- model Groups notations -- global notations """ - attributes = {'id':None, - 'version':None, - 'xml:lang':None, - 'targetNamespace':None, - 'attributeFormDefault':'unqualified', - 'elementFormDefault':'unqualified', - 'blockDefault':None, - 'finalDefault':None} - contents = {'xsd':('include', 'import', 'redefine', 'annotation', + attributes = {'id': None, + 'version': None, + 'xml: lang': None, + 'targetNamespace': None, + 'attributeFormDefault': 'unqualified', + 'elementFormDefault': 'unqualified', + 'blockDefault': None, + 'finalDefault': None} + contents = {'xsd': ('include', 'import', 'redefine', 'annotation', 'attribute', 'attributeGroup', 'complexType', 'element', 'group', 'notation', 'simpleType', 'annotation')} empty_namespace = '' tag = 'schema' - def __init__(self, parent=None): - """parent -- + def __init__(self, parent=None): + """parent -- instance variables: targetNamespace -- schema's declared targetNamespace, or empty string. - _imported_schemas -- namespace keyed dict of schema dependencies, if + _imported_schemas -- namespace keyed dict of schema dependencies, if a schema is provided instance will not resolve import statement. - _included_schemas -- schemaLocation keyed dict of component schemas, + _included_schemas -- schemaLocation keyed dict of component schemas, if schema is provided instance will not resolve include statement. _base_url -- needed for relative URLs support, only works with URLs relative to initial document. @@ -1051,47 +1067,48 @@ class XMLSchema(XMLSchemaComponent): self._imported_schemas = {} self._included_schemas = {} self._base_url = None + self.logger = logging.getLogger('wstools') def getNode(self): """ Interacting with the underlying DOM tree. """ return self.__node - + def addImportSchema(self, schema): """for resolving import statements in Schema instance schema -- schema instance - _imported_schemas + _imported_schemas """ if not isinstance(schema, XMLSchema): - raise TypeError, 'expecting a Schema instance' + raise TypeError('expecting a Schema instance') if schema.targetNamespace != self.targetNamespace: self._imported_schemas[schema.targetNamespace] = schema else: - raise SchemaError, 'import schema bad targetNamespace' + raise SchemaError('import schema bad targetNamespace') def addIncludeSchema(self, schemaLocation, schema): """for resolving include statements in Schema instance schemaLocation -- schema location schema -- schema instance - _included_schemas + _included_schemas """ if not isinstance(schema, XMLSchema): - raise TypeError, 'expecting a Schema instance' + raise TypeError('expecting a Schema instance') if not schema.targetNamespace or\ schema.targetNamespace == self.targetNamespace: self._included_schemas[schemaLocation] = schema else: - raise SchemaError, 'include schema bad targetNamespace' - + raise SchemaError('include schema bad targetNamespace') + def setImportSchemas(self, schema_dict): - """set the import schema dictionary, which is used to + """set the import schema dictionary, which is used to reference depedent schemas. """ self._imported_schemas = schema_dict def getImportSchemas(self): - """get the import schema dictionary, which is used to + """get the import schema dictionary, which is used to reference depedent schemas. """ return self._imported_schemas @@ -1103,26 +1120,26 @@ class XMLSchema(XMLSchemaComponent): return tuple(self.includes.keys()) def setIncludeSchemas(self, schema_dict): - """set the include schema dictionary, which is keyed with - schemaLocation (uri). - This is a means of providing + """set the include schema dictionary, which is keyed with + schemaLocation (uri). + This is a means of providing schemas to the current schema for content inclusion. """ self._included_schemas = schema_dict def getIncludeSchemas(self): - """get the include schema dictionary, which is keyed with - schemaLocation (uri). + """get the include schema dictionary, which is keyed with + schemaLocation (uri). """ return self._included_schemas def getBaseUrl(self): - """get base url, used for normalizing all relative uri's + """get base url, used for normalizing all relative uri's """ return self._base_url def setBaseUrl(self, url): - """set base url, used for normalizing all relative uri's + """set base url, used for normalizing all relative uri's """ self._base_url = url @@ -1145,7 +1162,7 @@ class XMLSchema(XMLSchemaComponent): return self.attributes.get('blockDefault') def getFinalDefault(self): - """return finalDefault attribute + """return finalDefault attribute """ return self.attributes.get('finalDefault') @@ -1160,8 +1177,8 @@ class XMLSchema(XMLSchemaComponent): self.setAttributes(pnode) attributes.update(self.attributes) self.setAttributes(node) - for k,v in attributes['xmlns'].items(): - if not self.attributes['xmlns'].has_key(k): + for k, v in attributes['xmlns'].items(): + if not k in self.attributes['xmlns']: self.attributes['xmlns'][k] = v else: self.setAttributes(node) @@ -1171,7 +1188,7 @@ class XMLSchema(XMLSchemaComponent): self.targetNamespace = self.getTargetNamespace() for childNode in self.getContents(node): component = SplitQName(childNode.getTagName())[1] - + if component == 'include': tp = self.__class__.Include(self) tp.fromDom(childNode) @@ -1179,7 +1196,7 @@ class XMLSchema(XMLSchemaComponent): sl = tp.attributes['schemaLocation'] schema = tp.getSchema() - if not self.getIncludeSchemas().has_key(sl): + if sl not in self.getIncludeSchemas(): self.addIncludeSchema(sl, schema) self.includes[sl] = tp @@ -1189,16 +1206,16 @@ class XMLSchema(XMLSchemaComponent): for child in schema.getNode().getNode().childNodes: pn.appendChild(child.cloneNode(1)) - for collection in ['imports','elements','types', - 'attr_decl','attr_groups','model_groups', + for collection in ['imports', 'elements', 'types', + 'attr_decl', 'attr_groups', 'model_groups', 'notations']: - for k,v in getattr(schema,collection).items(): - if not getattr(self,collection).has_key(k): + for k, v in getattr(schema, collection).items(): + if k not in getattr(self, collection): v._parent = weakref.ref(self) - getattr(self,collection)[k] = v + getattr(self, collection)[k] = v else: warnings.warn("Not keeping schema component.") - + elif component == 'import': slocd = SchemaReader.namespaceToSchema tp = self.__class__.Import(self) @@ -1214,8 +1231,7 @@ class XMLSchema(XMLSchemaComponent): except NoSchemaLocationWarning, ex: # Dependency declaration, hopefully implementation # is aware of this namespace (eg. SOAP,WSDL,?) - #print "IMPORT: ", import_ns - #print ex + self.logger.debug("IMPORT: %s : %s" % (import_ns, ex)) del slocd[import_ns] continue except SchemaError, ex: @@ -1223,8 +1239,9 @@ class XMLSchema(XMLSchemaComponent): # ', %s'\ # %(import_ns, 'failed to load schema instance') #) - print ex + self.logger.debug(ex) del slocd[import_ns] + class _LazyEvalImport(str): '''Lazy evaluation of import, replace entry in self.imports.''' #attributes = dict(namespace=import_ns) @@ -1244,19 +1261,19 @@ class XMLSchema(XMLSchemaComponent): self.imports[import_ns] = _LazyEvalImport(import_ns) continue - else: + else: tp._schema = schema - - if self.getImportSchemas().has_key(import_ns): + + if import_ns in self.getImportSchemas(): warnings.warn(\ 'Detected multiple imports of the namespace "%s" '\ - %import_ns) - + % import_ns) + self.addImportSchema(schema) # spec says can have multiple imports of same namespace # but purpose of import is just dependency declaration. self.imports[import_ns] = tp - + elif component == 'redefine': warnings.warn('redefine is ignored') elif component == 'annotation': @@ -1293,7 +1310,7 @@ class XMLSchema(XMLSchemaComponent): break class Import(XMLSchemaComponent): - """ + """ parent: schema attributes: @@ -1303,10 +1320,10 @@ class XMLSchema(XMLSchemaComponent): contents: annotation? """ - attributes = {'id':None, - 'namespace':None, - 'schemaLocation':None} - contents = {'xsd':['annotation']} + attributes = {'id': None, + 'namespace': None, + 'schemaLocation': None} + contents = {'xsd': ['annotation']} tag = 'import' def __init__(self, parent): @@ -1319,7 +1336,7 @@ class XMLSchema(XMLSchemaComponent): contents = self.getContents(node) if self.attributes['namespace'] == self.getTargetNamespace(): - raise SchemaError, 'namespace of schema and import match' + raise SchemaError('namespace of schema and import match') for i in contents: component = SplitQName(i.getTagName())[1] @@ -1327,12 +1344,12 @@ class XMLSchema(XMLSchemaComponent): self.annotation = Annotation(self) self.annotation.fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) def getSchema(self): """if schema is not defined, first look for a Schema class instance in parent Schema. Else if not defined resolve schemaLocation - and create a new Schema class instance, and keep a hard reference. + and create a new Schema class instance, and keep a hard reference. """ if not self._schema: ns = self.attributes['namespace'] @@ -1343,14 +1360,14 @@ class XMLSchema(XMLSchemaComponent): if not schema: url = self.attributes.get('schemaLocation') if not url: - raise SchemaError, 'namespace(%s) is unknown' %ns + raise SchemaError('namespace(%s) is unknown' % ns) base_url = self._parent().getBaseUrl() reader = SchemaReader(base_url=base_url) reader._imports = self._parent().getImportSchemas() reader._includes = self._parent().getIncludeSchemas() self._schema = reader.loadFromURL(url) return self._schema or schema - + def loadSchema(self, schema): """ """ @@ -1360,12 +1377,11 @@ class XMLSchema(XMLSchemaComponent): reader._includes = self._parent().getIncludeSchemas() self._schema = schema - if not self.attributes.has_key('schemaLocation'): + if not 'schemaLocation' in self.attributes: raise NoSchemaLocationWarning('no schemaLocation attribute in import') reader.loadFromURL(self.attributes.get('schemaLocation'), schema) - class Include(XMLSchemaComponent): """ parent: @@ -1377,9 +1393,9 @@ class XMLSchema(XMLSchemaComponent): annotation? """ required = ['schemaLocation'] - attributes = {'id':None, - 'schemaLocation':None} - contents = {'xsd':['annotation']} + attributes = {'id': None, + 'schemaLocation': None} + contents = {'xsd': ['annotation']} tag = 'include' def __init__(self, parent): @@ -1397,25 +1413,23 @@ class XMLSchema(XMLSchemaComponent): self.annotation = Annotation(self) self.annotation.fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) def getSchema(self): """if schema is not defined, first look for a Schema class instance in parent Schema. Else if not defined resolve schemaLocation - and create a new Schema class instance. + and create a new Schema class instance. """ if not self._schema: schema = self._parent() - self._schema = schema.getIncludeSchemas().get(\ - self.attributes['schemaLocation'] - ) + self._schema = schema.getIncludeSchemas().get(self.attributes['schemaLocation']) if not self._schema: url = self.attributes['schemaLocation'] reader = SchemaReader(base_url=schema.getBaseUrl()) reader._imports = schema.getImportSchemas() reader._includes = schema.getIncludeSchemas() - - # create schema before loading so chameleon include + + # create schema before loading so chameleon include # will evalute targetNamespace correctly. self._schema = XMLSchema(schema) reader.loadFromURL(url, self._schema) @@ -1427,7 +1441,7 @@ class AttributeDeclaration(XMLSchemaComponent,\ AttributeMarker,\ DeclarationMarker): """ - parent: + parent: schema attributes: id -- ID @@ -1439,12 +1453,12 @@ class AttributeDeclaration(XMLSchemaComponent,\ annotation?, simpleType? """ required = ['name'] - attributes = {'id':None, - 'name':None, - 'type':None, - 'default':None, - 'fixed':None} - contents = {'xsd':['annotation','simpleType']} + attributes = {'id': None, + 'name': None, + 'type': None, + 'default': None, + 'fixed': None} + contents = {'xsd': ['annotation', 'simpleType']} tag = 'attribute' def __init__(self, parent): @@ -1467,7 +1481,7 @@ class AttributeDeclaration(XMLSchemaComponent,\ self.content = AnonymousSimpleType(self) self.content.fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) class LocalAttributeDeclaration(AttributeDeclaration,\ @@ -1475,7 +1489,7 @@ class LocalAttributeDeclaration(AttributeDeclaration,\ LocalMarker,\ DeclarationMarker): """ - parent: + parent: complexType, restriction, extension, attributeGroup attributes: id -- ID @@ -1489,14 +1503,14 @@ class LocalAttributeDeclaration(AttributeDeclaration,\ annotation?, simpleType? """ required = ['name'] - attributes = {'id':None, - 'name':None, - 'type':None, - 'form':lambda self: GetSchema(self).getAttributeFormDefault(), - 'use':'optional', - 'default':None, - 'fixed':None} - contents = {'xsd':['annotation','simpleType']} + attributes = {'id': None, + 'name': None, + 'type': None, + 'form': lambda self: GetSchema(self).getAttributeFormDefault(), + 'use': 'optional', + 'default': None, + 'fixed': None} + contents = {'xsd': ['annotation', 'simpleType']} def __init__(self, parent): AttributeDeclaration.__init__(self, parent) @@ -1516,7 +1530,7 @@ class LocalAttributeDeclaration(AttributeDeclaration,\ self.content = AnonymousSimpleType(self) self.content.fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) class AttributeWildCard(XMLSchemaComponent,\ @@ -1524,20 +1538,20 @@ class AttributeWildCard(XMLSchemaComponent,\ DeclarationMarker,\ WildCardMarker): """ - parents: + parents: complexType, restriction, extension, attributeGroup attributes: id -- ID - namespace -- '##any' | '##other' | + namespace -- '##any' | '##other' | (anyURI* | '##targetNamespace' | '##local'), ##any processContents -- 'lax' | 'skip' | 'strict', strict contents: annotation? """ - attributes = {'id':None, - 'namespace':'##any', - 'processContents':'strict'} - contents = {'xsd':['annotation']} + attributes = {'id': None, + 'namespace': '##any', + 'processContents': 'strict'} + contents = {'xsd': ['annotation']} tag = 'anyAttribute' def __init__(self, parent): @@ -1554,14 +1568,14 @@ class AttributeWildCard(XMLSchemaComponent,\ self.annotation = Annotation(self) self.annotation.fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) class AttributeReference(XMLSchemaComponent,\ AttributeMarker,\ ReferenceMarker): """ - parents: + parents: complexType, restriction, extension, attributeGroup attributes: id -- ID @@ -1573,12 +1587,12 @@ class AttributeReference(XMLSchemaComponent,\ annotation? """ required = ['ref'] - attributes = {'id':None, - 'ref':None, - 'use':'optional', - 'default':None, - 'fixed':None} - contents = {'xsd':['annotation']} + attributes = {'id': None, + 'ref': None, + 'use': 'optional', + 'default': None, + 'fixed': None} + contents = {'xsd': ['annotation']} tag = 'attribute' def __init__(self, parent): @@ -1598,14 +1612,14 @@ class AttributeReference(XMLSchemaComponent,\ self.annotation = Annotation(self) self.annotation.fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) class AttributeGroupDefinition(XMLSchemaComponent,\ AttributeGroupMarker,\ DefinitionMarker): """ - parents: + parents: schema, redefine attributes: id -- ID @@ -1614,9 +1628,9 @@ class AttributeGroupDefinition(XMLSchemaComponent,\ annotation?, (attribute | attributeGroup)*, anyAttribute? """ required = ['name'] - attributes = {'id':None, - 'name':None} - contents = {'xsd':['annotation', 'attribute', 'attributeGroup', 'anyAttribute']} + attributes = {'id': None, + 'name': None} + contents = {'xsd': ['annotation', 'attribute', 'attributeGroup', 'anyAttribute']} tag = 'attributeGroup' def __init__(self, parent): @@ -1643,26 +1657,27 @@ class AttributeGroupDefinition(XMLSchemaComponent,\ elif contents[indx].hasattr('ref'): content.append(AttributeReference(self)) else: - raise SchemaError, 'Unknown attribute type' + raise SchemaError('Unknown attribute type') content[-1].fromDom(contents[indx]) elif component == 'attributeGroup': content.append(AttributeGroupReference(self)) content[-1].fromDom(contents[indx]) elif component == 'anyAttribute': - if len(contents) != indx+1: - raise SchemaError, 'anyAttribute is out of order in %s' %self.getItemTrace() + if len(contents) != indx + 1: + raise SchemaError('anyAttribute is out of order in %s' % self.getItemTrace()) content.append(AttributeWildCard(self)) content[-1].fromDom(contents[indx]) else: - raise SchemaError, 'Unknown component (%s)' %(contents[indx].getTagName()) + raise SchemaError('Unknown component (%s)' % (contents[indx].getTagName())) self.attr_content = tuple(content) + class AttributeGroupReference(XMLSchemaComponent,\ AttributeGroupMarker,\ ReferenceMarker): """ - parents: + parents: complexType, restriction, extension, attributeGroup attributes: id -- ID @@ -1671,9 +1686,9 @@ class AttributeGroupReference(XMLSchemaComponent,\ annotation? """ required = ['ref'] - attributes = {'id':None, - 'ref':None} - contents = {'xsd':['annotation']} + attributes = {'id': None, + 'ref': None} + contents = {'xsd': ['annotation']} tag = 'attributeGroup' def __init__(self, parent): @@ -1696,15 +1711,14 @@ class AttributeGroupReference(XMLSchemaComponent,\ self.annotation = Annotation(self) self.annotation.fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) - + raise SchemaError('Unknown component (%s)' % (i.getTagName())) ###################################################### # Elements ##################################################### class IdentityConstrants(XMLSchemaComponent): - """Allow one to uniquely identify nodes in a document and ensure the + """Allow one to uniquely identify nodes in a document and ensure the integrity of references between them. attributes -- dictionary of attributes @@ -1737,12 +1751,11 @@ class IdentityConstrants(XMLSchemaComponent): fields[-1].fromDom(i) continue else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) self.fields = tuple(fields) - class Constraint(XMLSchemaComponent): def __init__(self, parent): XMLSchemaComponent.__init__(self, parent) @@ -1759,13 +1772,13 @@ class IdentityConstrants(XMLSchemaComponent): self.annotation = Annotation(self) self.annotation.fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) class Selector(Constraint): """ - parent: + parent: unique, key, keyref attributes: id -- ID @@ -1774,14 +1787,14 @@ class IdentityConstrants(XMLSchemaComponent): annotation? """ required = ['xpath'] - attributes = {'id':None, - 'xpath':None} - contents = {'xsd':['annotation']} + attributes = {'id': None, + 'xpath': None} + contents = {'xsd': ['annotation']} tag = 'selector' - class Field(Constraint): + class Field(Constraint): """ - parent: + parent: unique, key, keyref attributes: id -- ID @@ -1790,16 +1803,16 @@ class IdentityConstrants(XMLSchemaComponent): annotation? """ required = ['xpath'] - attributes = {'id':None, - 'xpath':None} - contents = {'xsd':['annotation']} + attributes = {'id': None, + 'xpath': None} + contents = {'xsd': ['annotation']} tag = 'field' class Unique(IdentityConstrants): """ Enforce fields are unique w/i a specified scope. - parent: + parent: element attributes: id -- ID @@ -1808,9 +1821,9 @@ class Unique(IdentityConstrants): annotation?, selector, field+ """ required = ['name'] - attributes = {'id':None, - 'name':None} - contents = {'xsd':['annotation', 'selector', 'field']} + attributes = {'id': None, + 'name': None} + contents = {'xsd': ['annotation', 'selector', 'field']} tag = 'unique' @@ -1819,7 +1832,7 @@ class Key(IdentityConstrants): field values are present w/i document. Fields cannot be nillable. - parent: + parent: element attributes: id -- ID @@ -1828,16 +1841,16 @@ class Key(IdentityConstrants): annotation?, selector, field+ """ required = ['name'] - attributes = {'id':None, - 'name':None} - contents = {'xsd':['annotation', 'selector', 'field']} + attributes = {'id': None, + 'name': None} + contents = {'xsd': ['annotation', 'selector', 'field']} tag = 'key' class KeyRef(IdentityConstrants): - """ Ensure a match between two sets of values in an + """ Ensure a match between two sets of values in an instance. - parent: + parent: element attributes: id -- ID @@ -1847,10 +1860,10 @@ class KeyRef(IdentityConstrants): annotation?, selector, field+ """ required = ['name', 'refer'] - attributes = {'id':None, - 'name':None, - 'refer':None} - contents = {'xsd':['annotation', 'selector', 'field']} + attributes = {'id': None, + 'name': None, + 'refer': None} + contents = {'xsd': ['annotation', 'selector', 'field']} tag = 'keyref' @@ -1869,26 +1882,26 @@ class ElementDeclaration(XMLSchemaComponent,\ nillable -- boolean, false abstract -- boolean, false substitutionGroup -- QName - block -- ('#all' | ('substition' | 'extension' | 'restriction')*), - schema.blockDefault - final -- ('#all' | ('extension' | 'restriction')*), - schema.finalDefault + block -- ('#all' | ('substition' | 'extension' | 'restriction')*), + schema.blockDefault + final -- ('#all' | ('extension' | 'restriction')*), + schema.finalDefault contents: annotation?, (simpleType,complexType)?, (key | keyref | unique)* - + """ required = ['name'] - attributes = {'id':None, - 'name':None, - 'type':None, - 'default':None, - 'fixed':None, - 'nillable':0, - 'abstract':0, - 'substitutionGroup':None, - 'block':lambda self: self._parent().getBlockDefault(), - 'final':lambda self: self._parent().getFinalDefault()} - contents = {'xsd':['annotation', 'simpleType', 'complexType', 'key',\ + attributes = {'id': None, + 'name': None, + 'type': None, + 'default': None, + 'fixed': None, + 'nillable': 0, + 'abstract': 0, + 'substitutionGroup': None, + 'block': lambda self: self._parent().getBlockDefault(), + 'final': lambda self: self._parent().getFinalDefault()} + contents = {'xsd': ['annotation', 'simpleType', 'complexType', 'key',\ 'keyref', 'unique']} tag = 'element' @@ -1902,36 +1915,37 @@ class ElementDeclaration(XMLSchemaComponent,\ """Global elements are always qualified. """ return True - + def getAttribute(self, attribute): """return attribute. - If attribute is type and it's None, and no simple or complex content, + If attribute is type and it's None, and no simple or complex content, return the default type "xsd:anyType" """ value = XMLSchemaComponent.getAttribute(self, attribute) if attribute != 'type' or value is not None: return value - + if self.content is not None: return None - + parent = self while 1: nsdict = parent.attributes[XMLSchemaComponent.xmlns] - for k,v in nsdict.items(): - if v not in SCHEMA.XSD_LIST: continue + for k, v in nsdict.items(): + if v not in SCHEMA.XSD_LIST: + continue return TypeDescriptionComponent((v, 'anyType')) - + if isinstance(parent, WSDLToolsAdapter)\ or not hasattr(parent, '_parent'): break - + parent = parent._parent() - - raise SchemaError, 'failed to locate the XSD namespace' - + + raise SchemaError('failed to locate the XSD namespace') + def getElementDeclaration(self, attribute): - raise Warning, 'invalid operation for <%s>' %self.tag + raise Warning('invalid operation for <%s>' % self.tag) def getTypeDefinition(self, attribute=None): """If attribute is None, "type" is assumed, return the corresponding @@ -1948,6 +1962,7 @@ class ElementDeclaration(XMLSchemaComponent,\ def getConstraints(self): return self._constraints + def setConstraints(self, constraints): self._constraints = tuple(constraints) constraints = property(getConstraints, setConstraints, None, "tuple of key, keyref, unique constraints") @@ -1978,9 +1993,9 @@ class ElementDeclaration(XMLSchemaComponent,\ constraints.append(Unique(self)) constraints[-1].fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) self.constraints = constraints @@ -2000,23 +2015,23 @@ class LocalElementDeclaration(ElementDeclaration,\ default -- string fixed -- string nillable -- boolean, false - block -- ('#all' | ('extension' | 'restriction')*), schema.blockDefault + block -- ('#all' | ('extension' | 'restriction')*), schema.blockDefault contents: annotation?, (simpleType,complexType)?, (key | keyref | unique)* """ required = ['name'] - attributes = {'id':None, - 'name':None, - 'form':lambda self: GetSchema(self).getElementFormDefault(), - 'type':None, - 'minOccurs':'1', - 'maxOccurs':'1', - 'default':None, - 'fixed':None, - 'nillable':0, - 'abstract':0, - 'block':lambda self: GetSchema(self).getBlockDefault()} - contents = {'xsd':['annotation', 'simpleType', 'complexType', 'key',\ + attributes = {'id': None, + 'name': None, + 'form': lambda self: GetSchema(self).getElementFormDefault(), + 'type': None, + 'minOccurs': '1', + 'maxOccurs': '1', + 'default': None, + 'fixed': None, + 'nillable': 0, + 'abstract': 0, + 'block': lambda self: GetSchema(self).getBlockDefault()} + contents = {'xsd': ['annotation', 'simpleType', 'complexType', 'key',\ 'keyref', 'unique']} def isQualified(self): @@ -2030,14 +2045,14 @@ Local elements can be qualified or unqualifed according return True if form == 'unqualified': return False - raise SchemaError, 'Bad form (%s) for element: %s' %(form, self.getItemTrace()) + raise SchemaError('Bad form (%s) for element: %s' % (form, self.getItemTrace())) class ElementReference(XMLSchemaComponent,\ ElementMarker,\ ReferenceMarker): """ - parents: + parents: all, choice, sequence attributes: id -- ID @@ -2048,11 +2063,11 @@ class ElementReference(XMLSchemaComponent,\ annotation? """ required = ['ref'] - attributes = {'id':None, - 'ref':None, - 'minOccurs':'1', - 'maxOccurs':'1'} - contents = {'xsd':['annotation']} + attributes = {'id': None, + 'ref': None, + 'minOccurs': '1', + 'maxOccurs': '1'} + contents = {'xsd': ['annotation']} tag = 'element' def __init__(self, parent): @@ -2067,7 +2082,7 @@ class ElementReference(XMLSchemaComponent,\ if attribute: return XMLSchemaComponent.getElementDeclaration(self, attribute) return XMLSchemaComponent.getElementDeclaration(self, 'ref') - + def fromDom(self, node): self.annotation = None self.setAttributes(node) @@ -2078,30 +2093,30 @@ class ElementReference(XMLSchemaComponent,\ self.annotation = Annotation(self) self.annotation.fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) class ElementWildCard(LocalElementDeclaration, WildCardMarker): """ - parents: + parents: choice, sequence attributes: id -- ID minOccurs -- Whole Number, 1 maxOccurs -- (Whole Number | 'unbounded'), 1 - namespace -- '##any' | '##other' | + namespace -- '##any' | '##other' | (anyURI* | '##targetNamespace' | '##local'), ##any processContents -- 'lax' | 'skip' | 'strict', strict contents: annotation? """ required = [] - attributes = {'id':None, - 'minOccurs':'1', - 'maxOccurs':'1', - 'namespace':'##any', - 'processContents':'strict'} - contents = {'xsd':['annotation']} + attributes = {'id': None, + 'minOccurs': '1', + 'maxOccurs': '1', + 'namespace': '##any', + 'processContents': 'strict'} + contents = {'xsd': ['annotation']} tag = 'any' def __init__(self, parent): @@ -2121,7 +2136,7 @@ class ElementWildCard(LocalElementDeclaration, WildCardMarker): return XMLSchemaComponent.getAttribute(self, attribute) def getTypeDefinition(self, attribute): - raise Warning, 'invalid operation for <%s>' % self.tag + raise Warning('invalid operation for <%s>' % self.tag) def fromDom(self, node): self.annotation = None @@ -2133,7 +2148,7 @@ class ElementWildCard(LocalElementDeclaration, WildCardMarker): self.annotation = Annotation(self) self.annotation.fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) ###################################################### @@ -2142,7 +2157,7 @@ class ElementWildCard(LocalElementDeclaration, WildCardMarker): class Sequence(XMLSchemaComponent,\ SequenceMarker): """ - parents: + parents: complexType, extension, restriction, group, choice, sequence attributes: id -- ID @@ -2152,10 +2167,10 @@ class Sequence(XMLSchemaComponent,\ contents: annotation?, (element | group | choice | sequence | any)* """ - attributes = {'id':None, - 'minOccurs':'1', - 'maxOccurs':'1'} - contents = {'xsd':['annotation', 'element', 'group', 'choice', 'sequence',\ + attributes = {'id': None, + 'minOccurs': '1', + 'maxOccurs': '1'} + contents = {'xsd': ['annotation', 'element', 'group', 'choice', 'sequence',\ 'any']} tag = 'sequence' @@ -2190,17 +2205,17 @@ class Sequence(XMLSchemaComponent,\ elif component == 'any': content.append(ElementWildCard(self)) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) content[-1].fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) self.content = tuple(content) class All(XMLSchemaComponent,\ AllMarker): """ - parents: + parents: complexType, extension, restriction, group attributes: id -- ID @@ -2210,10 +2225,10 @@ class All(XMLSchemaComponent,\ contents: annotation?, element* """ - attributes = {'id':None, - 'minOccurs':'1', - 'maxOccurs':'1'} - contents = {'xsd':['annotation', 'element']} + attributes = {'id': None, + 'minOccurs': '1', + 'maxOccurs': '1'} + contents = {'xsd': ['annotation', 'element']} tag = 'all' def __init__(self, parent): @@ -2239,17 +2254,17 @@ class All(XMLSchemaComponent,\ else: content.append(LocalElementDeclaration(self)) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) content[-1].fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) self.content = tuple(content) class Choice(XMLSchemaComponent,\ ChoiceMarker): """ - parents: + parents: complexType, extension, restriction, group, choice, sequence attributes: id -- ID @@ -2259,10 +2274,10 @@ class Choice(XMLSchemaComponent,\ contents: annotation?, (element | group | choice | sequence | any)* """ - attributes = {'id':None, - 'minOccurs':'1', - 'maxOccurs':'1'} - contents = {'xsd':['annotation', 'element', 'group', 'choice', 'sequence',\ + attributes = {'id': None, + 'minOccurs': '1', + 'maxOccurs': '1'} + contents = {'xsd': ['annotation', 'element', 'group', 'choice', 'sequence',\ 'any']} tag = 'choice' @@ -2297,10 +2312,10 @@ class Choice(XMLSchemaComponent,\ elif component == 'any': content.append(ElementWildCard(self)) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) content[-1].fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) self.content = tuple(content) @@ -2318,9 +2333,9 @@ class ModelGroupDefinition(XMLSchemaComponent,\ annotation?, (all | choice | sequence)? """ required = ['name'] - attributes = {'id':None, - 'name':None} - contents = {'xsd':['annotation', 'all', 'choice', 'sequence']} + attributes = {'id': None, + 'name': None} + contents = {'xsd': ['annotation', 'all', 'choice', 'sequence']} tag = 'group' def __init__(self, parent): @@ -2346,10 +2361,10 @@ class ModelGroupDefinition(XMLSchemaComponent,\ elif component == 'sequence' and not self.content: self.content = Sequence(self) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) self.content.fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) class ModelGroupReference(XMLSchemaComponent,\ @@ -2368,11 +2383,11 @@ class ModelGroupReference(XMLSchemaComponent,\ annotation? """ required = ['ref'] - attributes = {'id':None, - 'ref':None, - 'minOccurs':'1', - 'maxOccurs':'1'} - contents = {'xsd':['annotation']} + attributes = {'id': None, + 'ref': None, + 'minOccurs': '1', + 'maxOccurs': '1'} + contents = {'xsd': ['annotation']} tag = 'group' def __init__(self, parent): @@ -2393,10 +2408,9 @@ class ModelGroupReference(XMLSchemaComponent,\ self.annotation = Annotation(self) self.annotation.fromDom(i) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) - + raise SchemaError('Unknown component (%s)' % (i.getTagName())) class ComplexType(XMLSchemaComponent,\ @@ -2410,21 +2424,21 @@ class ComplexType(XMLSchemaComponent,\ name -- NCName, required mixed -- boolean, false abstract -- boolean, false - block -- ('#all' | ('extension' | 'restriction')*), schema.blockDefault - final -- ('#all' | ('extension' | 'restriction')*), schema.finalDefault + block -- ('#all' | ('extension' | 'restriction')*), schema.blockDefault + final -- ('#all' | ('extension' | 'restriction')*), schema.finalDefault contents: - annotation?, (simpleContent | complexContent | + annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, (attribute | attributeGroup)*, anyAttribute?)) """ required = ['name'] - attributes = {'id':None, - 'name':None, - 'mixed':0, - 'abstract':0, - 'block':lambda self: self._parent().getBlockDefault(), - 'final':lambda self: self._parent().getFinalDefault()} - contents = {'xsd':['annotation', 'simpleContent', 'complexContent',\ + attributes = {'id': None, + 'name': None, + 'mixed': 0, + 'abstract': 0, + 'block': lambda self: self._parent().getBlockDefault(), + 'final': lambda self: self._parent().getFinalDefault()} + contents = {'xsd': ['annotation', 'simpleContent', 'complexContent',\ 'group', 'all', 'choice', 'sequence', 'attribute', 'attributeGroup',\ 'anyAttribute', 'any']} tag = 'complexType' @@ -2437,30 +2451,29 @@ class ComplexType(XMLSchemaComponent,\ def isMixed(self): m = self.getAttribute('mixed') - if m == 0 or m == False: + if not m: return False - if isinstance(m, basestring) is True: + if isinstance(m, basestring): if m in ('false', '0'): return False if m in ('true', '1'): return True - raise SchemaError, 'invalid value for attribute mixed(%s): %s'\ - %(m, self.getItemTrace()) + raise SchemaError('invalid value for attribute mixed(%s): %s' % (m, self.getItemTrace())) def getAttributeContent(self): return self.attr_content def getElementDeclaration(self, attribute): - raise Warning, 'invalid operation for <%s>' %self.tag + raise Warning('invalid operation for <%s>' % self.tag) def getTypeDefinition(self, attribute): - raise Warning, 'invalid operation for <%s>' %self.tag + raise Warning('invalid operation for <%s>' % self.tag) def fromDom(self, node): self.setAttributes(node) contents = self.getContents(node) - + indx = 0 num = len(contents) if not num: @@ -2508,8 +2521,7 @@ class ComplexType(XMLSchemaComponent,\ elif component == 'anyAttribute': self.attr_content.append(AttributeWildCard(self)) else: - raise SchemaError, 'Unknown component (%s): %s' \ - %(contents[indx].getTagName(),self.getItemTrace()) + raise SchemaError('Unknown component (%s): %s' % (contents[indx].getTagName(), self.getItemTrace())) self.attr_content[-1].fromDom(contents[indx]) indx += 1 @@ -2537,9 +2549,9 @@ class ComplexType(XMLSchemaComponent,\ elif component == 'extension' and not self.derivation: self.derivation = self.__class__.Extension(self) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) self.derivation.fromDom(i) self.content = self.derivation @@ -2555,22 +2567,21 @@ class ComplexType(XMLSchemaComponent,\ contents: annotation?, (restriction | extension) """ - attributes = {'id':None, - 'mixed':0} - contents = {'xsd':['annotation', 'restriction', 'extension']} + attributes = {'id': None, + 'mixed': 0} + contents = {'xsd': ['annotation', 'restriction', 'extension']} tag = 'complexContent' def isMixed(self): m = self.getAttribute('mixed') - if m == 0 or m == False: + if not m: return False if isinstance(m, basestring) is True: if m in ('false', '0'): return False if m in ('true', '1'): return True - raise SchemaError, 'invalid value for attribute mixed(%s): %s'\ - %(m, self.getItemTrace()) + raise SchemaError('invalid value for attribute mixed(%s): %s' % (m, self.getItemTrace())) class _DerivationBase(XMLSchemaComponent): """, @@ -2581,13 +2592,13 @@ class ComplexType(XMLSchemaComponent,\ base -- QName, required contents: - annotation?, (group | all | choice | sequence)?, + annotation?, (group | all | choice | sequence)?, (attribute | attributeGroup)*, anyAttribute? """ required = ['base'] - attributes = {'id':None, - 'base':None } - contents = {'xsd':['annotation', 'group', 'all', 'choice',\ + attributes = {'id': None, + 'base': None} + contents = {'xsd': ['annotation', 'group', 'all', 'choice',\ 'sequence', 'attribute', 'attributeGroup', 'anyAttribute']} def __init__(self, parent): @@ -2650,11 +2661,11 @@ class ComplexType(XMLSchemaComponent,\ elif component == 'anyAttribute': self.attr_content.append(AttributeWildCard(self)) else: - raise SchemaError, 'Unknown component (%s)' %(contents[indx].getTagName()) + raise SchemaError('Unknown component (%s)' % (contents[indx].getTagName())) self.attr_content[-1].fromDom(contents[indx]) indx += 1 - class Extension(_DerivationBase, + class Extension(_DerivationBase, ExtensionMarker): """ parents: @@ -2664,7 +2675,7 @@ class ComplexType(XMLSchemaComponent,\ base -- QName, required contents: - annotation?, (group | all | choice | sequence)?, + annotation?, (group | all | choice | sequence)?, (attribute | attributeGroup)*, anyAttribute? """ tag = 'extension' @@ -2679,12 +2690,11 @@ class ComplexType(XMLSchemaComponent,\ base -- QName, required contents: - annotation?, (group | all | choice | sequence)?, + annotation?, (group | all | choice | sequence)?, (attribute | attributeGroup)*, anyAttribute? """ tag = 'restriction' - class SimpleContent(_DerivedType,\ SimpleMarker): """ @@ -2696,8 +2706,8 @@ class ComplexType(XMLSchemaComponent,\ contents: annotation?, (restriction | extension) """ - attributes = {'id':None} - contents = {'xsd':['annotation', 'restriction', 'extension']} + attributes = {'id': None} + contents = {'xsd': ['annotation', 'restriction', 'extension']} tag = 'simpleContent' class Extension(XMLSchemaComponent,\ @@ -2713,9 +2723,9 @@ class ComplexType(XMLSchemaComponent,\ annotation?, (attribute | attributeGroup)*, anyAttribute? """ required = ['base'] - attributes = {'id':None, - 'base':None } - contents = {'xsd':['annotation', 'attribute', 'attributeGroup', + attributes = {'id': None, + 'base': None} + contents = {'xsd': ['annotation', 'attribute', 'attributeGroup', 'anyAttribute']} tag = 'extension' @@ -2723,7 +2733,7 @@ class ComplexType(XMLSchemaComponent,\ XMLSchemaComponent.__init__(self, parent) self.annotation = None self.attr_content = None - + def getAttributeContent(self): return self.attr_content @@ -2741,7 +2751,7 @@ class ComplexType(XMLSchemaComponent,\ self.annotation.fromDom(contents[indx]) indx += 1 component = SplitQName(contents[indx].getTagName())[1] - + content = [] while indx < num: component = SplitQName(contents[indx].getTagName())[1] @@ -2755,13 +2765,11 @@ class ComplexType(XMLSchemaComponent,\ elif component == 'anyAttribute': content.append(AttributeWildCard(self)) else: - raise SchemaError, 'Unknown component (%s)'\ - %(contents[indx].getTagName()) + raise SchemaError('Unknown component (%s)' % (contents[indx].getTagName())) content[-1].fromDom(contents[indx]) indx += 1 self.attr_content = tuple(content) - class Restriction(XMLSchemaComponent,\ RestrictionMarker): """ @@ -2772,16 +2780,16 @@ class ComplexType(XMLSchemaComponent,\ base -- QName, required contents: - annotation?, simpleType?, (enumeration | length | - maxExclusive | maxInclusive | maxLength | minExclusive | - minInclusive | minLength | pattern | fractionDigits | - totalDigits | whiteSpace)*, (attribute | attributeGroup)*, + annotation?, simpleType?, (enumeration | length | + maxExclusive | maxInclusive | maxLength | minExclusive | + minInclusive | minLength | pattern | fractionDigits | + totalDigits | whiteSpace)*, (attribute | attributeGroup)*, anyAttribute? """ required = ['base'] - attributes = {'id':None, - 'base':None } - contents = {'xsd':['annotation', 'simpleType', 'attribute',\ + attributes = {'id': None, + 'base': None} + contents = {'xsd': ['annotation', 'simpleType', 'attribute',\ 'attributeGroup', 'anyAttribute'] + RestrictionMarker.facets} tag = 'restriction' @@ -2790,7 +2798,7 @@ class ComplexType(XMLSchemaComponent,\ self.annotation = None self.content = None self.attr_content = None - + def getAttributeContent(self): return self.attr_content @@ -2824,8 +2832,7 @@ class ComplexType(XMLSchemaComponent,\ self.content.append(AnonymousSimpleType(self)) self.content[-1].fromDom(contents[indx]) else: - raise SchemaError, 'Unknown component (%s)'\ - %(contents[indx].getTagName()) + raise SchemaError('Unknown component (%s)' % (contents[indx].getTagName())) content[-1].fromDom(contents[indx]) indx += 1 self.attr_content = tuple(content) @@ -2841,14 +2848,14 @@ class LocalComplexType(ComplexType,\ mixed -- boolean, false contents: - annotation?, (simpleContent | complexContent | + annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, (attribute | attributeGroup)*, anyAttribute?)) """ required = [] - attributes = {'id':None, - 'mixed':0} + attributes = {'id': None, + 'mixed': 0} tag = 'complexType' - + class SimpleType(XMLSchemaComponent,\ DefinitionMarker,\ @@ -2859,17 +2866,17 @@ class SimpleType(XMLSchemaComponent,\ attributes: id -- ID name -- NCName, required - final -- ('#all' | ('extension' | 'restriction' | 'list' | 'union')*), - schema.finalDefault + final -- ('#all' | ('extension' | 'restriction' | 'list' | 'union')*), + schema.finalDefault contents: annotation?, (restriction | list | union) """ required = ['name'] - attributes = {'id':None, - 'name':None, - 'final':lambda self: self._parent().getFinalDefault()} - contents = {'xsd':['annotation', 'restriction', 'list', 'union']} + attributes = {'id': None, + 'name': None, + 'final': lambda self: self._parent().getFinalDefault()} + contents = {'xsd': ['annotation', 'restriction', 'list', 'union']} tag = 'simpleType' def __init__(self, parent): @@ -2878,10 +2885,10 @@ class SimpleType(XMLSchemaComponent,\ self.content = None def getElementDeclaration(self, attribute): - raise Warning, 'invalid operation for <%s>' %self.tag + raise Warning('invalid operation for <%s>' % self.tag) def getTypeDefinition(self, attribute): - raise Warning, 'invalid operation for <%s>' %self.tag + raise Warning('invalid operation for <%s>' % self.tag) def fromDom(self, node): self.setAttributes(node) @@ -2902,7 +2909,7 @@ class SimpleType(XMLSchemaComponent,\ elif component == 'union': self.content = self.__class__.Union(self) else: - raise SchemaError, 'Unknown component (%s)' %(component) + raise SchemaError('Unknown component (%s)' % (component)) self.content.fromDom(child) class Restriction(XMLSchemaComponent,\ @@ -2915,14 +2922,14 @@ class SimpleType(XMLSchemaComponent,\ base -- QName, required or simpleType child contents: - annotation?, simpleType?, (enumeration | length | - maxExclusive | maxInclusive | maxLength | minExclusive | - minInclusive | minLength | pattern | fractionDigits | + annotation?, simpleType?, (enumeration | length | + maxExclusive | maxInclusive | maxLength | minExclusive | + minInclusive | minLength | pattern | fractionDigits | totalDigits | whiteSpace)* """ - attributes = {'id':None, - 'base':None } - contents = {'xsd':['annotation', 'simpleType']+RestrictionMarker.facets} + attributes = {'id': None, + 'base': None} + contents = {'xsd': ['annotation', 'simpleType'] + RestrictionMarker.facets} tag = 'restriction' def __init__(self, parent): @@ -2939,7 +2946,8 @@ class SimpleType(XMLSchemaComponent,\ def getSimpleTypeContent(self): for el in self.content: - if el.isSimple(): return el + if el.isSimple(): + return el return None def fromDom(self, node): @@ -2960,10 +2968,9 @@ class SimpleType(XMLSchemaComponent,\ elif component in RestrictionMarker.facets: self.facets.append(contents[indx]) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) self.content = tuple(content) - class Union(XMLSchemaComponent, UnionMarker): """ @@ -2976,9 +2983,9 @@ class SimpleType(XMLSchemaComponent,\ contents: annotation?, simpleType* """ - attributes = {'id':None, - 'memberTypes':None } - contents = {'xsd':['annotation', 'simpleType']} + attributes = {'id': None, + 'memberTypes': None} + contents = {'xsd': ['annotation', 'simpleType']} tag = 'union' def __init__(self, parent): @@ -3000,10 +3007,10 @@ class SimpleType(XMLSchemaComponent,\ content.append(AnonymousSimpleType(self)) content[-1].fromDom(contents[indx]) else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) self.content = tuple(content) - class List(XMLSchemaComponent, + class List(XMLSchemaComponent, ListMarker): """ parents: @@ -3015,9 +3022,9 @@ class SimpleType(XMLSchemaComponent,\ contents: annotation?, simpleType? """ - attributes = {'id':None, - 'itemType':None } - contents = {'xsd':['annotation', 'simpleType']} + attributes = {'id': None, + 'itemType': None} + contents = {'xsd': ['annotation', 'simpleType']} tag = 'list' def __init__(self, parent): @@ -3031,7 +3038,7 @@ class SimpleType(XMLSchemaComponent,\ def getTypeDefinition(self, attribute='itemType'): """ return the type refered to by itemType attribute or - the simpleType content. If returns None, then the + the simpleType content. If returns None, then the type refered to by itemType is primitive. """ tp = XMLSchemaComponent.getTypeDefinition(self, attribute) @@ -3052,9 +3059,9 @@ class SimpleType(XMLSchemaComponent,\ self.content.fromDom(contents[indx]) break else: - raise SchemaError, 'Unknown component (%s)' %(i.getTagName()) + raise SchemaError('Unknown component (%s)' % (i.getTagName())) + - class AnonymousSimpleType(SimpleType,\ SimpleMarker,\ LocalMarker): @@ -3068,7 +3075,7 @@ class AnonymousSimpleType(SimpleType,\ annotation?, (restriction | list | union) """ required = [] - attributes = {'id':None} + attributes = {'id': None} tag = 'simpleType' @@ -3092,18 +3099,19 @@ else: import UserTuple tupleClass = UserTuple.UserTuple + class TypeDescriptionComponent(tupleClass): """Tuple of length 2, consisting of a namespace and unprefixed name. - """ + """ def __new__(typ, args): """args -- (namespace, name) Remove the name's prefix, irrelevant. """ if len(args) != 2: - raise TypeError, 'expecting tuple (namespace, name), got %s' %args + raise TypeError('expecting tuple (namespace, name), got %s' % args) elif args[1].find(':') >= 0: - args = (args[0], SplitQName(args[1])[1]) + args = (args[0], SplitQName(args[1])[1]) return tuple.__new__(typ, args) def getTargetNamespace(self): @@ -3111,5 +3119,3 @@ class TypeDescriptionComponent(tupleClass): def getName(self): return self[1] - - diff --git a/src/wstools/XMLname.py b/src/wstools/XMLname.py index eb5ed4c..644e254 100644 --- a/src/wstools/XMLname.py +++ b/src/wstools/XMLname.py @@ -19,59 +19,68 @@ from re import * def _NCNameChar(x): - return x.isalpha() or x.isdigit() or x=="." or x=='-' or x=="_" + return x.isalpha() or x.isdigit() or x == "." or x == '-' or x == "_" def _NCNameStartChar(x): - return x.isalpha() or x=="_" + return x.isalpha() or x == "_" def _toUnicodeHex(x): hexval = hex(ord(x[0]))[2:] hexlen = len(hexval) # Make hexval have either 4 or 8 digits by prepending 0's - if (hexlen==1): hexval = "000" + hexval - elif (hexlen==2): hexval = "00" + hexval - elif (hexlen==3): hexval = "0" + hexval - elif (hexlen==4): hexval = "" + hexval - elif (hexlen==5): hexval = "000" + hexval - elif (hexlen==6): hexval = "00" + hexval - elif (hexlen==7): hexval = "0" + hexval - elif (hexlen==8): hexval = "" + hexval - else: raise Exception, "Illegal Value returned from hex(ord(x))" - - return "_x"+ hexval + "_" + if (hexlen == 1): + hexval = "000" + hexval + elif (hexlen == 2): + hexval = "00" + hexval + elif (hexlen == 3): + hexval = "0" + hexval + elif (hexlen == 4): + hexval = "" + hexval + elif (hexlen == 5): + hexval = "000" + hexval + elif (hexlen == 6): + hexval = "00" + hexval + elif (hexlen == 7): + hexval = "0" + hexval + elif (hexlen == 8): + hexval = "" + hexval + else: + raise Exception("Illegal Value returned from hex(ord(x))") + + return "_x" + hexval + "_" def _fromUnicodeHex(x): - return eval( r'u"\u'+x[2:-1]+'"' ) + return eval(r'u"\u' + x[2:-1] + '"') def toXMLname(string): """Convert string to a XML name.""" - if string.find(':') != -1 : - (prefix, localname) = string.split(':',1) + if string.find(':') != -1: + (prefix, localname) = string.split(':', 1) else: prefix = None localname = string - + T = unicode(localname) N = len(localname) - X = []; - for i in range(N) : - if i< N-1 and T[i]==u'_' and T[i+1]==u'x': + X = [] + for i in range(N): + if i < N - 1 and T[i] == u'_' and T[i + 1] == u'x': X.append(u'_x005F_') - elif i==0 and N >= 3 and \ - ( T[0]==u'x' or T[0]==u'X' ) and \ - ( T[1]==u'm' or T[1]==u'M' ) and \ - ( T[2]==u'l' or T[2]==u'L' ): + elif i == 0 and N >= 3 and \ + (T[0] == u'x' or T[0] == u'X') and \ + (T[1] == u'm' or T[1] == u'M') and \ + (T[2] == u'l' or T[2] == u'L'): X.append(u'_xFFFF_' + T[0]) - elif (not _NCNameChar(T[i])) or (i==0 and not _NCNameStartChar(T[i])): + elif (not _NCNameChar(T[i])) or (i == 0 and not _NCNameStartChar(T[i])): X.append(_toUnicodeHex(T[i])) else: X.append(T[i]) - + if prefix: return "%s:%s" % (prefix, u''.join(X)) return u''.join(X) @@ -80,12 +89,11 @@ def toXMLname(string): def fromXMLname(string): """Convert XML name to unicode string.""" - retval = sub(r'_xFFFF_','', string ) + retval = sub(r'_xFFFF_', '', string) - def fun( matchobj ): - return _fromUnicodeHex( matchobj.group(0) ) + def fun(matchobj): + return _fromUnicodeHex(matchobj.group(0)) + retval = sub(r'_x[0-9A-Fa-f]{4}_', fun, retval) - retval = sub(r'_x[0-9A-Fa-f]{4}_', fun, retval ) - return retval diff --git a/src/wstools/__init__.py b/src/wstools/__init__.py index 5b6f7ef..43417af 100644 --- a/src/wstools/__init__.py +++ b/src/wstools/__init__.py @@ -6,4 +6,3 @@ ident = "$Id$" import WSDLTools import XMLname import logging - diff --git a/src/wstools/c14n.py b/src/wstools/c14n.py index 33305bf..626440a 100755 --- a/src/wstools/c14n.py +++ b/src/wstools/c14n.py @@ -5,10 +5,10 @@ Patches Applied to xml.dom.ext.c14n: http://sourceforge.net/projects/pyxml/ [ 1444526 ] c14n.py: http://www.w3.org/TR/xml-exc-c14n/ fix - -- includes [ 829905 ] c14n.py fix for bug #825115, + -- includes [ 829905 ] c14n.py fix for bug #825115, Date Submitted: 2003-10-24 23:43 - -- include dependent namespace declarations declared in ancestor nodes - (checking attributes and tags), + -- include dependent namespace declarations declared in ancestor nodes + (checking attributes and tags), -- handle InclusiveNamespaces PrefixList parameter This module generates canonical XML of a document or element. @@ -64,30 +64,35 @@ except ImportError: _attrs = lambda E: (E.attributes and E.attributes.values()) or [] _children = lambda E: E.childNodes or [] _IN_XML_NS = lambda n: n.name.startswith("xmlns") -_inclusive = lambda n: n.unsuppressedPrefixes == None +_inclusive = lambda n: n.unsuppressedPrefixes is None # Does a document/PI has lesser/greater document order than the # first element? _LesserElement, _Element, _GreaterElement = range(3) -def _sorter(n1,n2): + +def _sorter(n1, n2): '''_sorter(n1,n2) -> int Sorting predicate for non-NS attributes.''' i = cmp(n1.namespaceURI, n2.namespaceURI) - if i: return i + if i: + return i return cmp(n1.localName, n2.localName) -def _sorter_ns(n1,n2): +def _sorter_ns(n1, n2): '''_sorter_ns((n,v),(n,v)) -> int "(an empty namespace URI is lexicographically least)."''' - if n1[0] == 'xmlns': return -1 - if n2[0] == 'xmlns': return 1 + if n1[0] == 'xmlns': + return -1 + if n2[0] == 'xmlns': + return 1 return cmp(n1[0], n2[0]) + def _utilized(n, node, other_attrs, unsuppressedPrefixes): '''_utilized(n, node, other_attrs, unsuppressedPrefixes) -> boolean Return true if that nodespace is utilized within the node''' @@ -95,32 +100,35 @@ def _utilized(n, node, other_attrs, unsuppressedPrefixes): n = n[6:] elif n.startswith('xmlns'): n = n[5:] - if (n=="" and node.prefix in ["#default", None]) or \ - n == node.prefix or n in unsuppressedPrefixes: + if (n == "" and node.prefix in ["#default", None]) or \ + n == node.prefix or n in unsuppressedPrefixes: return 1 for attr in other_attrs: - if n == attr.prefix: return 1 + if n == attr.prefix: + return 1 # For exclusive need to look at attributes if unsuppressedPrefixes is not None: for attr in _attrs(node): - if n == attr.prefix: return 1 - + if n == attr.prefix: + return 1 + return 0 def _inclusiveNamespacePrefixes(node, context, unsuppressedPrefixes): - '''http://www.w3.org/TR/xml-exc-c14n/ - InclusiveNamespaces PrefixList parameter, which lists namespace prefixes that + '''http://www.w3.org/TR/xml-exc-c14n/ + InclusiveNamespaces PrefixList parameter, which lists namespace prefixes that are handled in the manner described by the Canonical XML Recommendation''' inclusive = [] if node.prefix: - usedPrefixes = ['xmlns:%s' %node.prefix] + usedPrefixes = ['xmlns:%s' % node.prefix] else: usedPrefixes = ['xmlns'] for a in _attrs(node): - if a.nodeName.startswith('xmlns') or not a.prefix: continue - usedPrefixes.append('xmlns:%s' %a.prefix) + if a.nodeName.startswith('xmlns') or not a.prefix: + continue + usedPrefixes.append('xmlns:%s' % a.prefix) unused_namespace_dict = {} for attr in context: @@ -139,7 +147,7 @@ def _inclusiveNamespacePrefixes(node, context, unsuppressedPrefixes): return inclusive, unused_namespace_dict #_in_subset = lambda subset, node: not subset or node in subset -_in_subset = lambda subset, node: subset is None or node in subset # rich's tweak +_in_subset = lambda subset, node: subset is None or node in subset # rich's tweak class _implementation: @@ -155,17 +163,17 @@ class _implementation: self.subset = kw.get('subset') self.comments = kw.get('comments', 0) self.unsuppressedPrefixes = kw.get('unsuppressedPrefixes') - nsdict = kw.get('nsdict', { 'xml': XMLNS.XML, 'xmlns': XMLNS.BASE }) - + nsdict = kw.get('nsdict', {'xml': XMLNS.XML, 'xmlns': XMLNS.BASE}) + # Processing state. - self.state = (nsdict, {'xml':''}, {}, {}) #0422 - + self.state = (nsdict, {'xml': ''}, {}, {}) # 0422 + if node.nodeType == Node.DOCUMENT_NODE: self._do_document(node) elif node.nodeType == Node.ELEMENT_NODE: self.documentOrder = _Element # At document element if not _inclusive(self): - inherited,unused = _inclusiveNamespacePrefixes(node, self._inherit_context(node), + inherited, unused = _inclusiveNamespacePrefixes(node, self._inherit_context(node), self.unsuppressedPrefixes) self._do_element(node, inherited, unused=unused) else: @@ -174,8 +182,7 @@ class _implementation: elif node.nodeType == Node.DOCUMENT_TYPE_NODE: pass else: - raise TypeError, str(node) - + raise TypeError(str(node)) def _inherit_context(self, node): '''_inherit_context(self, node) -> list @@ -197,7 +204,6 @@ class _implementation: parent = parent.parentNode return inherited - def _do_document(self, node): '''_do_document(self, node) -> None Process a document node. documentOrder holds whether the document @@ -209,7 +215,7 @@ class _implementation: if child.nodeType == Node.ELEMENT_NODE: self.documentOrder = _Element # At document element self._do_element(child) - self.documentOrder = _GreaterElement # After document element + self.documentOrder = _GreaterElement # After document element elif child.nodeType == Node.PROCESSING_INSTRUCTION_NODE: self._do_pi(child) elif child.nodeType == Node.COMMENT_NODE: @@ -217,33 +223,35 @@ class _implementation: elif child.nodeType == Node.DOCUMENT_TYPE_NODE: pass else: - raise TypeError, str(child) + raise TypeError(str(child)) handlers[Node.DOCUMENT_NODE] = _do_document - def _do_text(self, node): '''_do_text(self, node) -> None Process a text or CDATA node. Render various special characters as their C14N entity representations.''' - if not _in_subset(self.subset, node): return + if not _in_subset(self.subset, node): + return s = string.replace(node.data, "&", "&") s = string.replace(s, "<", "<") s = string.replace(s, ">", ">") s = string.replace(s, "\015", " ") - if s: self.write(s) + if s: + self.write(s) handlers[Node.TEXT_NODE] = _do_text handlers[Node.CDATA_SECTION_NODE] = _do_text - def _do_pi(self, node): '''_do_pi(self, node) -> None Process a PI node. Render a leading or trailing #xA if the document order of the PI is greater or lesser (respectively) than the document element. ''' - if not _in_subset(self.subset, node): return + if not _in_subset(self.subset, node): + return W = self.write - if self.documentOrder == _GreaterElement: W('\n') + if self.documentOrder == _GreaterElement: + W('\n') W('') - if self.documentOrder == _LesserElement: W('\n') + if self.documentOrder == _LesserElement: + W('\n') handlers[Node.PROCESSING_INSTRUCTION_NODE] = _do_pi - def _do_comment(self, node): '''_do_comment(self, node) -> None Process a comment node. Render a leading or trailing #xA if the document order of the comment is greater or lesser (respectively) than the document element. ''' - if not _in_subset(self.subset, node): return + if not _in_subset(self.subset, node): + return if self.comments: W = self.write - if self.documentOrder == _GreaterElement: W('\n') + if self.documentOrder == _GreaterElement: + W('\n') W('') - if self.documentOrder == _LesserElement: W('\n') + if self.documentOrder == _LesserElement: + W('\n') handlers[Node.COMMENT_NODE] = _do_comment - def _do_attr(self, n, value): ''''_do_attr(self, node) -> None Process an attribute.''' @@ -289,8 +299,7 @@ class _implementation: W(s) W('"') - - def _do_element(self, node, initial_other_attrs = [], unused = None): + def _do_element(self, node, initial_other_attrs=[], unused=None): '''_do_element(self, node, initial_other_attrs = [], unused = {}) -> None Process an element (and its children).''' @@ -300,14 +309,14 @@ class _implementation: # ns_local -- NS declarations relevant to this element # xml_attrs -- Attributes in XML namespace from parent # xml_attrs_local -- Local attributes in XML namespace. - # ns_unused_inherited -- not rendered namespaces, used for exclusive + # ns_unused_inherited -- not rendered namespaces, used for exclusive ns_parent, ns_rendered, xml_attrs = \ - self.state[0], self.state[1].copy(), self.state[2].copy() #0422 - + self.state[0], self.state[1].copy(), self.state[2].copy() # 0422 + ns_unused_inherited = unused if unused is None: ns_unused_inherited = self.state[3].copy() - + ns_local = ns_parent.copy() inclusive = _inclusive(self) xml_attrs_local = {} @@ -318,15 +327,16 @@ class _implementation: for a in initial_other_attrs + _attrs(node): if a.namespaceURI == XMLNS.BASE: n = a.nodeName - if n == "xmlns:": n = "xmlns" # DOM bug workaround + if n == "xmlns:": + n = "xmlns" # DOM bug workaround ns_local[n] = a.nodeValue elif a.namespaceURI == XMLNS.XML: - if inclusive or (in_subset and _in_subset(self.subset, a)): #020925 Test to see if attribute node in subset - xml_attrs_local[a.nodeName] = a #0426 + if inclusive or (in_subset and _in_subset(self.subset, a)): # 020925 Test to see if attribute node in subset + xml_attrs_local[a.nodeName] = a # 0426 else: - if _in_subset(self.subset, a): #020925 Test to see if attribute node in subset + if _in_subset(self.subset, a): # 020925 Test to see if attribute node in subset other_attrs.append(a) - + # # TODO: exclusive, might need to define xmlns:prefix here # if not inclusive and a.prefix is not None and not ns_rendered.has_key('xmlns:%s' %a.prefix): # ns_local['xmlns:%s' %a.prefix] = ?? @@ -336,47 +346,45 @@ class _implementation: # Render the node W, name = self.write, None - if in_subset: + if in_subset: name = node.nodeName if not inclusive: if node.prefix is not None: - prefix = 'xmlns:%s' %node.prefix + prefix = 'xmlns:%s' % node.prefix else: prefix = 'xmlns' - - if not ns_rendered.has_key(prefix) and not ns_local.has_key(prefix): - if not ns_unused_inherited.has_key(prefix): - raise RuntimeError,\ - 'For exclusive c14n, unable to map prefix "%s" in %s' %( - prefix, node) - + + if prefix not in ns_rendered and prefix not in ns_local: + if not prefix in ns_unused_inherited: + raise RuntimeError('For exclusive c14n, unable to map prefix "%s" in %s' % ( + prefix, node)) + ns_local[prefix] = ns_unused_inherited[prefix] del ns_unused_inherited[prefix] - + W('<') W(name) # Create list of NS attributes to render. ns_to_render = [] - for n,v in ns_local.items(): + for n, v in ns_local.items(): # If default namespace is XMLNS.BASE or empty, # and if an ancestor was the same - if n == "xmlns" and v in [ XMLNS.BASE, '' ] \ - and ns_rendered.get('xmlns') in [ XMLNS.BASE, '', None ]: + if n == "xmlns" and v in [XMLNS.BASE, ''] \ + and ns_rendered.get('xmlns') in [XMLNS.BASE, '', None]: continue # "omit namespace node with local name xml, which defines # the xml prefix, if its string value is # http://www.w3.org/XML/1998/namespace." if n in ["xmlns:xml", "xml"] \ - and v in [ 'http://www.w3.org/XML/1998/namespace' ]: + and v in ['http://www.w3.org/XML/1998/namespace']: continue - # If not previously rendered # and it's inclusive or utilized - if (n,v) not in ns_rendered.items(): + if (n, v) not in ns_rendered.items(): if inclusive or _utilized(n, node, other_attrs, self.unsuppressedPrefixes): ns_to_render.append((n, v)) elif not inclusive: @@ -384,14 +392,14 @@ class _implementation: # Sort and render the ns, marking what was rendered. ns_to_render.sort(_sorter_ns) - for n,v in ns_to_render: + for n, v in ns_to_render: self._do_attr(n, v) - ns_rendered[n]=v #0417 + ns_rendered[n] = v # 0417 # If exclusive or the parent is in the subset, add the local xml attributes # Else, add all local and ancestor xml attributes # Sort and render the attributes. - if not inclusive or _in_subset(self.subset,node.parentNode): #0426 + if not inclusive or _in_subset(self.subset, node.parentNode): # 0426 other_attrs.extend(xml_attrs_local.values()) else: other_attrs.extend(xml_attrs.values()) @@ -406,7 +414,8 @@ class _implementation: _implementation.handlers[c.nodeType](self, c) self.state = state - if name: W('' % name) + if name: + W('' % name) handlers[Node.ELEMENT_NODE] = _do_element diff --git a/src/wstools/logging.py b/src/wstools/logging.py index 9c33b00..0991162 100644 --- a/src/wstools/logging.py +++ b/src/wstools/logging.py @@ -1,11 +1,12 @@ # Copyright (c) 2003, The Regents of the University of California, # through Lawrence Berkeley National Laboratory (subject to receipt of # any required approvals from the U.S. Dept. of Energy). All rights -# reserved. +# reserved. # """Logging""" ident = "$Id$" -import os, sys +import os +import sys WARN = 1 DEBUG = 2 @@ -16,80 +17,92 @@ class ILogger: will be used and logging calls are no-ops. ''' level = 0 + def __init__(self, msg): return + def warning(self, *args, **kw): return + def debug(self, *args, **kw): return + def error(self, *args, **kw): return + def setLevel(cls, level): cls.level = level setLevel = classmethod(setLevel) - + debugOn = lambda self: self.level >= DEBUG warnOn = lambda self: self.level >= WARN - + class BasicLogger(ILogger): last = '' - + def __init__(self, msg, out=sys.stdout): self.msg, self.out = msg, out def warning(self, msg, *args, **kw): - if self.warnOn() is False: return + if self.warnOn() is False: + return if BasicLogger.last != self.msg: BasicLogger.last = self.msg print >>self, "---- ", self.msg, " ----" - print >>self, " %s " %self.WARN, - print >>self, msg %args + print >>self, " %s " % self.WARN, + print >>self, msg % args WARN = '[WARN]' + def debug(self, msg, *args, **kw): - if self.debugOn() is False: return + if self.debugOn() is False: + return if BasicLogger.last != self.msg: BasicLogger.last = self.msg print >>self, "---- ", self.msg, " ----" - print >>self, " %s " %self.DEBUG, - print >>self, msg %args + print >>self, " %s " % self.DEBUG, + print >>self, msg % args DEBUG = '[DEBUG]' + def error(self, msg, *args, **kw): if BasicLogger.last != self.msg: BasicLogger.last = self.msg print >>self, "---- ", self.msg, " ----" - print >>self, " %s " %self.ERROR, - print >>self, msg %args + print >>self, " %s " % self.ERROR, + print >>self, msg % args ERROR = '[ERROR]' def write(self, *args): '''Write convenience function; writes strings. ''' - for s in args: self.out.write(s) + for s in args: + self.out.write(s) event = ''.join(*args) _LoggerClass = BasicLogger + class GridLogger(ILogger): def debug(self, msg, *args, **kw): kw['component'] = self.msg - gridLog(event=msg %args, level='DEBUG', **kw) + gridLog(event=msg % args, level='DEBUG', **kw) def warning(self, msg, *args, **kw): kw['component'] = self.msg - gridLog(event=msg %args, level='WARNING', **kw) + gridLog(event=msg % args, level='WARNING', **kw) def error(self, msg, *args, **kw): kw['component'] = self.msg - gridLog(event=msg %args, level='ERROR', **kw) + gridLog(event=msg % args, level='ERROR', **kw) -# +# # Registry of send functions for gridLog -# +# GLRegistry = {} + class GLRecord(dict): """Grid Logging Best Practices Record, Distributed Logging Utilities @@ -97,8 +110,8 @@ class GLRecord(dict): event -- log event name Below is EBNF for the event name part of a log message. - name = ( "." )? - nodot = {RFC3896-chars except "."} + name = ( "." )? + nodot = {RFC3896-chars except "."} Suffixes: start: Immediately before the first action in a task. @@ -108,14 +121,14 @@ class GLRecord(dict): ts -- timestamp level -- logging level (see levels below) status -- integer status code - gid -- global grid identifier + gid -- global grid identifier gid, cgid -- parent/child identifiers prog -- program name More info: http://www.cedps.net/wiki/index.php/LoggingBestPractices#Python - reserved -- list of reserved names, + reserved -- list of reserved names, omitname -- list of reserved names, output only values ('ts', 'event',) levels -- dict of levels and description """ @@ -129,8 +142,7 @@ class GLRecord(dict): NOTICE='Normal but significant condition.', INFO='Informational messages that would be useful to a deployer or administrator.', DEBUG='Lower level information concerning program logic decisions, internal state, etc.', - TRACE='Finest granularity, similar to "stepping through" the component or system.', - ) + TRACE='Finest granularity, similar to "stepping through" the component or system.',) def __init__(self, date=None, **kw): kw['ts'] = date or self.GLDate() @@ -141,31 +153,34 @@ class GLRecord(dict): """ """ from cStringIO import StringIO - s = StringIO(); n = " " - reserved = self.reserved; omitname = self.omitname; levels = self.levels + s = StringIO() + n = " " + reserved = self.reserved + omitname = self.omitname + levels = self.levels - for k in ( list(filter(lambda i: self.has_key(i), reserved)) + + for k in (list(filter(lambda i: i in self, reserved)) + list(filter(lambda i: i not in reserved, self.keys())) ): v = self[k] - if k in omitname: - s.write( "%s " %self.format[type(v)](v) ) + if k in omitname: + s.write("%s " % self.format[type(v)](v)) continue if k == reserved[2] and v not in levels: pass - s.write( "%s=%s " %(k, self.format[type(v)](v) ) ) + s.write("%s=%s " % (k, self.format[type(v)](v))) s.write("\n") return s.getvalue() class GLDate(str): """Grid logging Date Format - all timestamps should all be in the same time zone (UTC). + all timestamps should all be in the same time zone (UTC). Grid timestamp value format that is a highly readable variant of the ISO8601 time standard [1]: - YYYY-MM-DDTHH:MM:SS.SSSSSSZ + YYYY-MM-DDTHH:MM:SS.SSSSSSZ """ def __new__(self, args=None): @@ -173,13 +188,13 @@ class GLRecord(dict): """ import datetime args = args or datetime.datetime.utcnow() - l = (args.year, args.month, args.day, args.hour, args.minute, args.second, + l = (args.year, args.month, args.day, args.hour, args.minute, args.second, args.microsecond, args.tzinfo or 'Z') - return str.__new__(self, "%04d-%02d-%02dT%02d:%02d:%02d.%06d%s" %l) + return str.__new__(self, "%04d-%02d-%02dT%02d:%02d:%02d.%06d%s" % l) - format = { int:str, float:lambda x: "%lf" % x, long:str, str:lambda x:x, - unicode:str, GLDate:str, } + format = {int: str, float: lambda x: "%lf" % x, long: str, str: lambda x: x, + unicode: str, GLDate: str, } def gridLog(**kw): @@ -193,29 +208,32 @@ def gridLog(**kw): """ import os - if not bool( int(os.environ.get('GRIDLOG_ON', 0)) ): + if not bool(int(os.environ.get('GRIDLOG_ON', 0))): return url = os.environ.get('GRIDLOG_DEST') - if url is None: + if url is None: return - ## NOTE: urlparse problem w/customized schemes + ## NOTE: urlparse problem w/customized schemes try: scheme = url[:url.find('://')] send = GLRegistry[scheme] - send( url, str(GLRecord(**kw)), ) + send(url, str(GLRecord(**kw)), ) except Exception, ex: - print >>sys.stderr, "*** gridLog failed -- %s" %(str(kw)) + print >>sys.stderr, "*** gridLog failed -- %s" % (str(kw)) def sendUDP(url, outputStr): from socket import socket, AF_INET, SOCK_DGRAM - idx1 = url.find('://') + 3; idx2 = url.find('/', idx1) - if idx2 < idx1: idx2 = len(url) + idx1 = url.find('://') + 3 + idx2 = url.find('/', idx1) + if idx2 < idx1: + idx2 = len(url) netloc = url[idx1:idx2] - host,port = (netloc.split(':')+[80])[0:2] - socket(AF_INET, SOCK_DGRAM).sendto( outputStr, (host,int(port)), ) + host, port = (netloc.split(':') + [80])[0:2] + socket(AF_INET, SOCK_DGRAM).sendto(outputStr, (host, int(port)), ) + def writeToFile(url, outputStr): print >> open(url.split('://')[1], 'a+'), outputStr @@ -225,32 +243,37 @@ GLRegistry["file"] = writeToFile def setBasicLogger(): - '''Use Basic Logger. + '''Use Basic Logger. ''' setLoggerClass(BasicLogger) BasicLogger.setLevel(0) + def setGridLogger(): '''Use GridLogger for all logging events. ''' setLoggerClass(GridLogger) + def setBasicLoggerWARN(): '''Use Basic Logger. ''' setLoggerClass(BasicLogger) BasicLogger.setLevel(WARN) + def setBasicLoggerDEBUG(): '''Use Basic Logger. ''' setLoggerClass(BasicLogger) BasicLogger.setLevel(DEBUG) + def setLoggerClass(loggingClass): '''Set Logging Class. ''' + def setLoggerClass(loggingClass): '''Set Logging Class. ''' @@ -258,17 +281,18 @@ def setLoggerClass(loggingClass): global _LoggerClass _LoggerClass = loggingClass + def setLevel(level=0): '''Set Global Logging Level. ''' ILogger.level = level + def getLevel(): return ILogger.level + def getLogger(msg): '''Return instance of Logging class. ''' return _LoggerClass(msg) - - diff --git a/src/wstools/tests/test_t1.py b/src/wstools/tests/test_t1.py index 5c33899..7c2790b 100644 --- a/src/wstools/tests/test_t1.py +++ b/src/wstools/tests/test_t1.py @@ -6,15 +6,16 @@ import unittest import test_wsdl import utils + def makeTestSuite(): suite = unittest.TestSuite() suite.addTest(test_wsdl.makeTestSuite("services_by_file")) return suite + def main(): loader = utils.MatchTestLoader(True, None, "makeTestSuite") unittest.main(defaultTest="makeTestSuite", testLoader=loader) -if __name__ == "__main__" : main() - - +if __name__ == "__main__": + main() diff --git a/src/wstools/tests/test_wsdl.py b/src/wstools/tests/test_wsdl.py index 5f617b9..9330565 100644 --- a/src/wstools/tests/test_wsdl.py +++ b/src/wstools/tests/test_wsdl.py @@ -5,7 +5,8 @@ # See LBNLCopyright for copyright notice! ########################################################################### -import sys, unittest +import sys +import unittest import ConfigParser import os from wstools.Utility import DOM @@ -15,6 +16,7 @@ from wstools.TimeoutSocket import TimeoutError from wstools import tests cwd = os.path.dirname(tests.__file__) + class WSDLToolsTestCase(unittest.TestCase): def __init__(self, methodName='runTest'): @@ -28,7 +30,7 @@ class WSDLToolsTestCase(unittest.TestCase): def __str__(self): teststr = unittest.TestCase.__str__(self) if hasattr(self, "path"): - return "%s: %s" % (teststr, self.path ) + return "%s: %s" % (teststr, self.path) else: return "%s" % (teststr) @@ -41,12 +43,12 @@ class WSDLToolsTestCase(unittest.TestCase): for node in DOM.getElements(definition, tag_name, nspname): name = DOM.getAttr(node, key) comp = component[name] - self.failUnlessEqual(eval('comp.%s' %key), name) + self.failUnlessEqual(eval('comp.%s' % key), name) def checkXSDCollection(self, tag_name, component, node, key='name'): for cnode in DOM.getElements(node, tag_name): name = DOM.getAttr(cnode, key) - component[name] + component[name] def test_all(self): try: @@ -88,13 +90,14 @@ class WSDLToolsTestCase(unittest.TestCase): raise try: - self.checkWSDLCollection('import', self.wsdl.imports, key='namespace') + self.checkWSDLCollection('import', self.wsdl.imports, \ + key='namespace') except: self.path = self.path + ": wsdl.imports" raise try: - for key in self.wsdl.types.keys(): + for key in self.wsdl.types.keys(): schema = self.wsdl.types[key] self.failUnlessEqual(key, schema.getTargetNamespace()) @@ -114,8 +117,9 @@ class WSDLToolsTestCase(unittest.TestCase): raise if self.wsdl.extensions: - print 'No check for WSDLTools(%s) Extensions:' %(self.wsdl.name) - for ext in self.wsdl.extensions: print '\t', ext + print 'No check for WSDLTools(%s) Extensions:' % (self.wsdl.name) + for ext in self.wsdl.extensions: + print '\t', ext def schemaAttributesDeclarations(self, schema, node): self.checkXSDCollection('attribute', schema.attr_decl, node) @@ -133,7 +137,7 @@ class WSDLToolsTestCase(unittest.TestCase): def setUpOptions(section): cp = ConfigParser.ConfigParser() - cp.read(cwd+'/config.txt') + cp.read(cwd + '/config.txt') if not cp.sections(): print 'fatal error: configuration file config.txt not present' sys.exit(0) @@ -142,10 +146,12 @@ def setUpOptions(section): sys.exit(0) return cp, len(cp.options(section)) + def getOption(cp, section): for name, value in cp.items(section): yield value - + + def makeTestSuite(section='services_by_file'): global nameGenerator @@ -159,6 +165,7 @@ def makeTestSuite(section='services_by_file'): def main(): unittest.main(defaultTest="makeTestSuite") - -if __name__ == "__main__" : main() + +if __name__ == "__main__": + main() diff --git a/src/wstools/tests/test_wstools.py b/src/wstools/tests/test_wstools.py index 0e0f958..a242a44 100644 --- a/src/wstools/tests/test_wstools.py +++ b/src/wstools/tests/test_wstools.py @@ -5,13 +5,17 @@ # See LBNLCopyright for copyright notice! ########################################################################### -import unittest, tarfile, os, ConfigParser +import unittest +import tarfile +import os +import ConfigParser import test_wsdl -SECTION='files' +SECTION = 'files' CONFIG_FILE = 'config.txt' + def extractFiles(section, option): config = ConfigParser.ConfigParser() config.read(CONFIG_FILE) @@ -20,18 +24,19 @@ def extractFiles(section, option): for file in archives: tar = tarfile.open(file) if not os.access(tar.membernames[0], os.R_OK): - for i in tar.getnames(): + for i in tar.getnames(): tar.extract(i) + def makeTestSuite(): suite = unittest.TestSuite() suite.addTest(test_wsdl.makeTestSuite("services_by_file")) return suite + def main(): extractFiles(SECTION, 'archives') unittest.main(defaultTest="makeTestSuite") -if __name__ == "__main__" : main() - - +if __name__ == "__main__": + main() diff --git a/src/wstools/tests/test_wstools_net.py b/src/wstools/tests/test_wstools_net.py index 880cff3..9c487fe 100644 --- a/src/wstools/tests/test_wstools_net.py +++ b/src/wstools/tests/test_wstools_net.py @@ -7,14 +7,15 @@ import unittest import test_wsdl + def makeTestSuite(): suite = unittest.TestSuite() suite.addTest(test_wsdl.makeTestSuite("services_by_http")) return suite + def main(): unittest.main(defaultTest="makeTestSuite") -if __name__ == "__main__" : main() - - +if __name__ == "__main__": + main()