|
| 1 | +# This program is free software; you can redistribute it and/or modify |
| 2 | +# it under the terms of the (LGPL) GNU Lesser General Public License as |
| 3 | +# published by the Free Software Foundation; either version 3 of the |
| 4 | +# License, or (at your option) any later version. |
| 5 | +# |
| 6 | +# This program is distributed in the hope that it will be useful, |
| 7 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 | +# GNU Library Lesser General Public License for more details at |
| 10 | +# ( http://www.gnu.org/licenses/lgpl.html ). |
| 11 | +# |
| 12 | +# You should have received a copy of the GNU Lesser General Public License |
| 13 | +# along with this program; if not, write to the Free Software |
| 14 | +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 15 | +# written by: Jeff Ortel ( [email protected] ) |
| 16 | + |
| 17 | +""" |
| 18 | +Suds is a lightweight SOAP Python client providing a Web Service proxy. |
| 19 | +""" |
| 20 | + |
| 21 | +import sys |
| 22 | + |
| 23 | + |
| 24 | +# |
| 25 | +# Project properties |
| 26 | +# |
| 27 | + |
| 28 | +from version import __build__, __version__ |
| 29 | + |
| 30 | + |
| 31 | +# |
| 32 | +# Exceptions |
| 33 | +# |
| 34 | + |
| 35 | +class MethodNotFound(Exception): |
| 36 | + def __init__(self, name): |
| 37 | + Exception.__init__(self, u"Method not found: '%s'" % name) |
| 38 | + |
| 39 | +class PortNotFound(Exception): |
| 40 | + def __init__(self, name): |
| 41 | + Exception.__init__(self, u"Port not found: '%s'" % name) |
| 42 | + |
| 43 | +class ServiceNotFound(Exception): |
| 44 | + def __init__(self, name): |
| 45 | + Exception.__init__(self, u"Service not found: '%s'" % name) |
| 46 | + |
| 47 | +class TypeNotFound(Exception): |
| 48 | + def __init__(self, name): |
| 49 | + Exception.__init__(self, u"Type not found: '%s'" % tostr(name)) |
| 50 | + |
| 51 | +class BuildError(Exception): |
| 52 | + msg = """ |
| 53 | + An error occurred while building an instance of (%s). As a result the |
| 54 | + object you requested could not be constructed. It is recommended that |
| 55 | + you construct the type manually using a Suds object. Please open a |
| 56 | + ticket with a description of this error. |
| 57 | + Reason: %s |
| 58 | + """ |
| 59 | + def __init__(self, name, exception): |
| 60 | + Exception.__init__(self, BuildError.msg % (name, exception)) |
| 61 | + |
| 62 | +class SoapHeadersNotPermitted(Exception): |
| 63 | + msg = """ |
| 64 | + Method (%s) was invoked with SOAP headers. The WSDL does not define |
| 65 | + SOAP headers for this method. Retry without the soapheaders keyword |
| 66 | + argument. |
| 67 | + """ |
| 68 | + def __init__(self, name): |
| 69 | + Exception.__init__(self, self.msg % name) |
| 70 | + |
| 71 | +class WebFault(Exception): |
| 72 | + def __init__(self, fault, document): |
| 73 | + if hasattr(fault, 'faultstring'): |
| 74 | + Exception.__init__(self, u"Server raised fault: '%s'" % |
| 75 | + fault.faultstring) |
| 76 | + self.fault = fault |
| 77 | + self.document = document |
| 78 | + |
| 79 | + |
| 80 | +# |
| 81 | +# Logging |
| 82 | +# |
| 83 | + |
| 84 | +class Repr: |
| 85 | + def __init__(self, x): |
| 86 | + self.x = x |
| 87 | + def __str__(self): |
| 88 | + return repr(self.x) |
| 89 | + |
| 90 | + |
| 91 | +# |
| 92 | +# Utility |
| 93 | +# |
| 94 | + |
| 95 | +class null: |
| 96 | + """ |
| 97 | + The I{null} object. |
| 98 | + Used to pass NULL for optional XML nodes. |
| 99 | + """ |
| 100 | + pass |
| 101 | + |
| 102 | +def objid(obj): |
| 103 | + return obj.__class__.__name__ + ':' + hex(id(obj)) |
| 104 | + |
| 105 | +def tostr(object, encoding=None): |
| 106 | + """ get a unicode safe string representation of an object """ |
| 107 | + if isinstance(object, basestring): |
| 108 | + if encoding is None: |
| 109 | + return object |
| 110 | + else: |
| 111 | + return object.encode(encoding) |
| 112 | + if isinstance(object, tuple): |
| 113 | + s = ['('] |
| 114 | + for item in object: |
| 115 | + if isinstance(item, basestring): |
| 116 | + s.append(item) |
| 117 | + else: |
| 118 | + s.append(tostr(item)) |
| 119 | + s.append(', ') |
| 120 | + s.append(')') |
| 121 | + return ''.join(s) |
| 122 | + if isinstance(object, list): |
| 123 | + s = ['['] |
| 124 | + for item in object: |
| 125 | + if isinstance(item, basestring): |
| 126 | + s.append(item) |
| 127 | + else: |
| 128 | + s.append(tostr(item)) |
| 129 | + s.append(', ') |
| 130 | + s.append(']') |
| 131 | + return ''.join(s) |
| 132 | + if isinstance(object, dict): |
| 133 | + s = ['{'] |
| 134 | + for item in object.items(): |
| 135 | + if isinstance(item[0], basestring): |
| 136 | + s.append(item[0]) |
| 137 | + else: |
| 138 | + s.append(tostr(item[0])) |
| 139 | + s.append(' = ') |
| 140 | + if isinstance(item[1], basestring): |
| 141 | + s.append(item[1]) |
| 142 | + else: |
| 143 | + s.append(tostr(item[1])) |
| 144 | + s.append(', ') |
| 145 | + s.append('}') |
| 146 | + return ''.join(s) |
| 147 | + try: |
| 148 | + return unicode(object) |
| 149 | + except: |
| 150 | + return str(object) |
| 151 | + |
| 152 | + |
| 153 | +# |
| 154 | +# Python 3 compatibility |
| 155 | +# |
| 156 | + |
| 157 | +if sys.version_info < (3, 0): |
| 158 | + from cStringIO import StringIO as BytesIO |
| 159 | +else: |
| 160 | + from io import BytesIO |
| 161 | + |
| 162 | +# Idea from 'http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python'. |
| 163 | +class UnicodeMixin(object): |
| 164 | + if sys.version_info >= (3, 0): |
| 165 | + # For Python 3, __str__() and __unicode__() should be identical. |
| 166 | + __str__ = lambda x: x.__unicode__() |
| 167 | + else: |
| 168 | + __str__ = lambda x: unicode(x).encode('utf-8') |
| 169 | + |
| 170 | +# Used instead of byte literals because they are not supported on Python |
| 171 | +# versions prior to 2.6. |
| 172 | +def byte_str(s='', encoding='utf-8', input_encoding='utf-8', errors='strict'): |
| 173 | + """ |
| 174 | + Returns a bytestring version of 's', encoded as specified in 'encoding'. |
| 175 | +
|
| 176 | + Accepts str & unicode objects, interpreting non-unicode strings as byte |
| 177 | + strings encoded using the given input encoding. |
| 178 | +
|
| 179 | + """ |
| 180 | + assert isinstance(s, basestring) |
| 181 | + if isinstance(s, unicode): |
| 182 | + return s.encode(encoding, errors) |
| 183 | + if s and encoding != input_encoding: |
| 184 | + return s.decode(input_encoding, errors).encode(encoding, errors) |
| 185 | + return s |
| 186 | + |
| 187 | +# Class used to represent a byte string. Useful for asserting that correct |
| 188 | +# string types are being passed around where needed. |
| 189 | +if sys.version_info >= (3, 0): |
| 190 | + byte_str_class = bytes |
| 191 | +else: |
| 192 | + byte_str_class = str |
0 commit comments