Add timestamp offset for block header
[p2pool.git] / SOAPpy / Config.py
1 """
2 ################################################################################
3 # Copyright (c) 2003, Pfizer
4 # Copyright (c) 2001, Cayce Ullman.
5 # Copyright (c) 2001, Brian Matthews.
6 #
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions are met:
11 # Redistributions of source code must retain the above copyright notice, this
12 # list of conditions and the following disclaimer.
13 #
14 # Redistributions in binary form must reproduce the above copyright notice,
15 # this list of conditions and the following disclaimer in the documentation
16 # and/or other materials provided with the distribution.
17 #
18 # Neither the name of actzero, inc. nor the names of its contributors may
19 # be used to endorse or promote products derived from this software without
20 # specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
26 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #
33 ################################################################################
34 """
35
36 ident = '$Id: Config.py 1298 2006-11-07 00:54:15Z sanxiyn $'
37 from version import __version__
38
39 import socket
40 from types import *
41
42 from NS import NS
43
44 ################################################################################
45 # Configuration class
46 ################################################################################
47
48
49 class SOAPConfig:
50     __readonly = ('SSLserver', 'SSLclient', 'GSIserver', 'GSIclient')
51     class SSLconfig:
52         __slots__ = ('key_file', 'cert_file')
53         key_file = None
54         cert_file = None
55
56     def __init__(self, config = None, **kw):
57         d = self.__dict__
58
59         if config:
60             if not isinstance(config, SOAPConfig):
61                 raise AttributeError, \
62                     "initializer must be SOAPConfig instance"
63
64             s = config.__dict__
65
66             for k, v in s.items():
67                 if k[0] != '_':
68                     d[k] = v
69         else:
70             # Setting debug also sets returnFaultInfo,
71             # dumpHeadersIn, dumpHeadersOut, dumpSOAPIn, and dumpSOAPOut
72             self.debug = 0
73             self.dumpFaultInfo = 1
74             # Setting namespaceStyle sets typesNamespace, typesNamespaceURI,
75             # schemaNamespace, and schemaNamespaceURI
76             self.namespaceStyle = '1999'
77             self.strictNamespaces = 0
78             self.typed = 1
79             self.buildWithNamespacePrefix = 1
80             self.returnAllAttrs = 0
81
82             # Strict checking of range for floats and doubles
83             self.strict_range = 0
84
85             # Default encoding for dictionary keys
86             self.dict_encoding = 'ascii'
87
88             # New argument name handling mechanism.  See
89             # README.MethodParameterNaming for details
90             self.specialArgs = 1
91
92             # If unwrap_results=1 and there is only element in the struct,
93             # SOAPProxy will assume that this element is the result
94             # and return it rather than the struct containing it.
95             # Otherwise SOAPproxy will return the struct with all the
96             # elements as attributes.
97             self.unwrap_results = 1
98
99             # Automatically convert SOAP complex types, and
100             # (recursively) public contents into the corresponding
101             # python types. (Private subobjects have names that start
102             # with '_'.)
103             #
104             # Conversions:
105             # - faultType    --> raise python exception
106             # - arrayType    --> array
107             # - compoundType --> dictionary
108             #
109             self.simplify_objects = 0
110
111             # Per-class authorization method.  If this is set, before
112             # calling a any class method, the specified authorization
113             # method will be called.  If it returns 1, the method call
114             # will proceed, otherwise the call will throw with an
115             # authorization error.
116             self.authMethod = None
117
118             # Globus Support if pyGlobus.io available
119             try:
120                 from pyGlobus import io;
121                 d['GSIserver'] = 1
122                 d['GSIclient'] = 1
123             except:
124                 d['GSIserver'] = 0
125                 d['GSIclient'] = 0
126
127
128             # Server SSL support if M2Crypto.SSL available
129             try:
130                 from M2Crypto import SSL
131                 d['SSLserver'] = 1
132             except:
133                 d['SSLserver'] = 0
134
135             # Client SSL support if socket.ssl available
136             try:
137                 from socket import ssl
138                 d['SSLclient'] = 1
139             except:
140                 d['SSLclient'] = 0
141
142             # Cert support
143             if d['SSLclient'] or d['SSLserver']:
144                 d['SSL'] = self.SSLconfig()
145
146         for k, v in kw.items():
147             if k[0] != '_':
148                 setattr(self, k, v)
149
150     def __setattr__(self, name, value):
151         if name in self.__readonly:
152             raise AttributeError, "readonly configuration setting"
153
154         d = self.__dict__
155
156         if name in ('typesNamespace', 'typesNamespaceURI',
157                     'schemaNamespace', 'schemaNamespaceURI'):
158
159             if name[-3:] == 'URI':
160                 base, uri = name[:-3], 1
161             else:
162                 base, uri = name, 0
163
164             if type(value) == StringType:
165                 if NS.NSMAP.has_key(value):
166                     n = (value, NS.NSMAP[value])
167                 elif NS.NSMAP_R.has_key(value):
168                     n = (NS.NSMAP_R[value], value)
169                 else:
170                     raise AttributeError, "unknown namespace"
171             elif type(value) in (ListType, TupleType):
172                 if uri:
173                     n = (value[1], value[0])
174                 else:
175                     n = (value[0], value[1])
176             else:
177                 raise AttributeError, "unknown namespace type"
178
179             d[base], d[base + 'URI'] = n
180
181             try:
182                 d['namespaceStyle'] = \
183                     NS.STMAP_R[(d['typesNamespace'], d['schemaNamespace'])]
184             except:
185                 d['namespaceStyle'] = ''
186
187         elif name == 'namespaceStyle':
188             value = str(value)
189
190             if not NS.STMAP.has_key(value):
191                 raise AttributeError, "unknown namespace style"
192
193             d[name] = value
194             n = d['typesNamespace'] = NS.STMAP[value][0]
195             d['typesNamespaceURI'] = NS.NSMAP[n]
196             n = d['schemaNamespace'] = NS.STMAP[value][1]
197             d['schemaNamespaceURI'] = NS.NSMAP[n]
198
199         elif name == 'debug':
200             d[name]                     = \
201                 d['returnFaultInfo']    = \
202                 d['dumpHeadersIn']      = \
203                 d['dumpHeadersOut']     = \
204                 d['dumpSOAPIn']         = \
205                 d['dumpSOAPOut']        = value
206
207         else:
208             d[name] = value
209
210
211 Config = SOAPConfig()