setting sfa-0.9-16 tag
[sfa.git] / sfa / server / modpythonapi / BaseClient.py
1 import xmlrpclib
2
3 from ApiExceptionCodes import *
4
5 VerboseExceptions = False
6
7 def EnableVerboseExceptions(x=True):
8     global VerboseExceptions
9     VerboseExceptions = x
10
11 class ExceptionUnmarshaller(xmlrpclib.Unmarshaller):
12     def close(self):
13         try:\r
14             return xmlrpclib.Unmarshaller.close(self)\r
15         except xmlrpclib.Fault, e:\r
16             # if the server tagged some traceback info onto the end of the\r
17             # exception text, then print it out on the client.\r
18 \r
19             if "\nFAULT_TRACEBACK:" in e.faultString:\r
20                 parts = e.faultString.split("\nFAULT_TRACEBACK:")\r
21                 e.faultString = parts[0]\r
22                 if VerboseExceptions:\r
23                     print "\n|Server Traceback:", "\n|".join(parts[1].split("\n"))\r
24 \r
25             raise e\r
26 \r
27 class ExceptionReportingTransport(xmlrpclib.Transport):
28     def make_connection(self, host):
29         import httplib\r
30         if host.startswith("https:"):\r
31            return httplib.HTTPS(host)\r
32         else:\r
33            return httplib.HTTP(host)\r
34 \r
35     def getparser(self):\r
36         unmarshaller = ExceptionUnmarshaller()\r
37         parser = xmlrpclib.ExpatParser(unmarshaller)\r
38         return parser, unmarshaller
39
40 class BaseClient():
41     def __init__(self, url):
42         self.url = url
43         self.server = xmlrpclib.ServerProxy(self.url, ExceptionReportingTransport())
44
45     def noop(self, value):
46         return self.server.noop(value)