783463efb3df94dcb1f11f775028a09c5191efa3
[plcapi.git] / PLC / API.py
1 #
2 # PLCAPI XML-RPC and SOAP interfaces
3 #
4 # Aaron Klingaman <alk@absarokasoft.com>
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 #
7 # Copyright (C) 2004-2006 The Trustees of Princeton University
8 # $Id: API.py,v 1.6 2006/10/27 18:57:32 mlhuang Exp $
9 #
10
11 import sys
12 import traceback
13 import string
14
15 import xmlrpclib
16
17 # See "2.2 Characters" in the XML specification:
18 #
19 # #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
20 # avoiding
21 # [#x7F-#x84], [#x86-#x9F], [#xFDD0-#xFDDF]
22
23 invalid_xml_ascii = map(chr, range(0x0, 0x8) + [0xB, 0xC] + range(0xE, 0x1F))
24 xml_escape_table = string.maketrans("".join(invalid_xml_ascii), "?" * len(invalid_xml_ascii))
25
26 def xmlrpclib_escape(s, replace = string.replace):
27     """
28     xmlrpclib does not handle invalid 7-bit control characters. This
29     function augments xmlrpclib.escape, which by default only replaces
30     '&', '<', and '>' with entities.
31     """
32
33     # This is the standard xmlrpclib.escape function
34     s = replace(s, "&", "&amp;")
35     s = replace(s, "<", "&lt;")
36     s = replace(s, ">", "&gt;",)
37
38     # Replace invalid 7-bit control characters with '?'
39     return s.translate(xml_escape_table)
40
41 def xmlrpclib_dump(self, value, write):
42     """
43     xmlrpclib cannot marshal instances of subclasses of built-in
44     types. This function overrides xmlrpclib.Marshaller.__dump so that
45     any value that is an instance of one of its acceptable types is
46     marshalled as that type.
47
48     xmlrpclib also cannot handle invalid 7-bit control characters. See
49     above.
50     """
51
52     # Use our escape function
53     args = [self, value, write]
54     if isinstance(value, (str, unicode)):
55         args.append(xmlrpclib_escape)
56
57     try:
58         # Try for an exact match first
59         f = self.dispatch[type(value)]
60     except KeyError:
61         # Try for an isinstance() match
62         for Type, f in self.dispatch.iteritems():
63             if isinstance(value, Type):
64                 f(*args)
65                 return
66         raise TypeError, "cannot marshal %s objects" % type(value)
67     else:
68         f(*args)
69
70 # You can't hide from me!
71 xmlrpclib.Marshaller._Marshaller__dump = xmlrpclib_dump
72
73 # SOAP support is optional
74 try:
75     import SOAPpy
76     from SOAPpy.Parser import parseSOAPRPC
77     from SOAPpy.Types import faultType
78     from SOAPpy.NS import NS
79     from SOAPpy.SOAPBuilder import buildSOAP
80 except ImportError:
81     SOAPpy = None
82
83 from PLC.Config import Config
84 from PLC.Faults import *
85 import PLC.Methods
86
87 class PLCAPI:
88     methods = PLC.Methods.methods
89
90     def __init__(self, config = "/etc/planetlab/plc_config", encoding = "utf-8"):
91         self.encoding = encoding
92
93         # Better just be documenting the API
94         if config is None:
95             return
96
97         # Load configuration
98         self.config = Config(config)
99
100         # Initialize database connection
101         if self.config.PLC_DB_TYPE == "postgresql":
102             from PLC.PostgreSQL import PostgreSQL
103             self.db = PostgreSQL(self)
104         else:
105             raise PLCAPIError, "Unsupported database type " + config.PLC_DB_TYPE
106
107     def callable(self, method):
108         """
109         Return a new instance of the specified method.
110         """
111
112         # Look up method
113         if method not in self.methods:
114             raise PLCInvalidAPIMethod, method
115
116         # Get new instance of method
117         try:
118             classname = method.split(".")[-1]
119             module = __import__("PLC.Methods." + method, globals(), locals(), [classname])
120             return getattr(module, classname)(self)
121         except ImportError, AttributeError:
122             raise PLCInvalidAPIMethod, method
123
124     def call(self, source, method, *args):
125         """
126         Call the named method from the specified source with the
127         specified arguments.
128         """
129
130         function = self.callable(method)
131         function.source = source
132         return function(*args)
133
134     def handle(self, source, data):
135         """
136         Handle an XML-RPC or SOAP request from the specified source.
137         """
138
139         # Parse request into method name and arguments
140         try:
141             interface = xmlrpclib
142             (args, method) = xmlrpclib.loads(data)
143             methodresponse = True
144         except Exception, e:
145             if SOAPpy is not None:
146                 interface = SOAPpy
147                 (r, header, body, attrs) = parseSOAPRPC(data, header = 1, body = 1, attrs = 1)
148                 method = r._name
149                 args = r._aslist()
150                 # XXX Support named arguments
151             else:
152                 raise e
153
154         try:
155             result = self.call(source, method, *args)
156         except PLCFault, fault:
157             # Handle expected faults
158             if interface == xmlrpclib:
159                 result = fault
160                 methodresponse = None
161             elif interface == SOAPpy:
162                 result = faultParameter(NS.ENV_T + ":Server", "Method Failed", method)
163                 result._setDetail("Fault %d: %s" % (fault.faultCode, fault.faultString))
164
165         # Return result
166         if interface == xmlrpclib:
167             if not isinstance(result, PLCFault):
168                 result = (result,)
169             data = xmlrpclib.dumps(result, methodresponse = True, encoding = self.encoding, allow_none = 1)
170         elif interface == SOAPpy:
171             data = buildSOAP(kw = {'%sResponse' % method: {'Result': result}}, encoding = self.encoding)
172
173         return data