ia64/xen-unstable

view tools/python/xen/web/SrvBase.py @ 7103:89974610d558

Fix returning of results broken by last change.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@ewan
date Wed Sep 28 15:00:11 2005 +0100 (2005-09-28)
parents 1e288a9316fd
children 1d74fff611c8
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library 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 GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 #============================================================================
18 import types
21 from xen.xend import sxp
22 from xen.xend import PrettyPrint
23 from xen.xend.Args import ArgError
24 from xen.xend.XendError import XendError
25 from xen.xend.XendLogging import log
27 import resource
28 import http
29 import httpserver
31 def uri_pathlist(p):
32 """Split a path into a list.
33 p path
34 return list of path elements
35 """
36 l = []
37 for x in p.split('/'):
38 if x == '': continue
39 l.append(x)
40 return l
42 class SrvBase(resource.Resource):
43 """Base class for services.
44 """
47 def use_sxp(self, req):
48 return req.useSxp()
50 def get_op_method(self, op):
51 """Get the method for an operation.
52 For operation 'foo' looks for 'op_foo'.
54 op operation name
55 returns method or None
56 """
57 op_method_name = 'op_' + op
58 return getattr(self, op_method_name, None)
60 def perform(self, req):
61 """General operation handler for posted operations.
62 For operation 'foo' looks for a method op_foo and calls
63 it with op_foo(op, req). Replies with code 500 if op_foo
64 is not found.
66 The method must return a list when req.use_sxp is true
67 and an HTML string otherwise (or list).
68 Methods may also return a ThreadRequest (for incomplete processing).
70 req request
71 """
72 op = req.args.get('op')
73 if op is None or len(op) != 1:
74 req.setResponseCode(http.NOT_ACCEPTABLE, "Invalid request")
75 return ''
76 op = op[0]
77 op_method = self.get_op_method(op)
78 if op_method is None:
79 req.setResponseCode(http.NOT_IMPLEMENTED, "Operation not implemented: " + op)
80 req.setHeader("Content-Type", "text/plain")
81 req.write("Operation not implemented: " + op)
82 return ''
83 else:
84 try:
85 return op_method(op, req)
86 except Exception, exn:
87 log.exception("Request %s failed.", op)
88 if req.useSxp():
89 return ['xend.err', "Exception: " + str(exn)]
90 else:
91 return "<p>%s</p>" % str(exn)
93 def print_path(self, req):
94 """Print the path with hyperlinks.
95 """
96 req.printPath()