ia64/xen-unstable

view tools/python/xen/web/resource.py @ 6538:84ee014ebd41

Merge xen-vtx-unstable.hg
author adsharma@los-vmm.sc.intel.com
date Wed Aug 17 12:34:38 2005 -0800 (2005-08-17)
parents 23979fb12c49 f40c6650152e
children 99914b54f7bf
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) 2005 Mike Wray <mike.wray@hp.com>
16 #============================================================================
18 import http
20 def findResource(resource, request):
21 """Traverse resource tree to find who will handle the request."""
22 while request.postpath and not resource.isLeaf:
23 #print 'findResource:', resource, request.postpath
24 pathElement = request.postpath.pop(0)
25 request.prepath.append(pathElement)
26 next = resource.getPathResource(pathElement, request)
27 if not next: break
28 resource = next
29 return resource
31 class Resource:
33 isLeaf = False
35 def __init__(self):
36 self.children = {}
38 def getRequestResource(self, req):
39 return findResource(self, req)
41 def getChild(self, path, request):
42 return None
44 def getPathResource(self, path, request):
45 #print 'getPathResource>', self, path
46 if self.children.has_key(path):
47 val = self.children[path]
48 else:
49 val = self.getChild(path, request)
50 #print 'getPathResource<', val
51 return val
53 def putChild(self, path, child):
54 self.children[path] = child
55 #child.server = self.server
57 def render(self, req):
58 meth = getattr(self, 'render_' + req.getRequestMethod(), self.unsupported)
59 return meth(req)
61 def supportedMethods(self):
62 l = []
63 s = 'render_'
64 for x in dir(self):
65 if x.startswith(s):
66 l.append(x[len(s):])
67 return l
69 def render_HEAD(self, req):
70 return self.render_GET(req)
72 def render_GET(self, req):
73 req.setContentType("text/plain")
74 req.write("GET")
76 def render_POST(self, req):
77 req.setContentType("text/plain")
78 req.write("POST")
80 def unsupported(self, req):
81 req.setHeader("Accept", ",".join(self.supportedMethods()))
82 req.setResponseCode(http.NOT_IMPLEMENTED)
83 req.setContentType("text/plain")
84 req.write("Request method not supported (%s)" % req.getRequestMethod())
86 class ErrorPage(Resource):
88 isLeaf = True
90 def __init__(self, code, status=None, msg=None):
91 Resource.__init__(self)
92 if status is None:
93 status = http.getStatus(code)
94 if msg is None:
95 msg = status
96 self.code = code
97 self.status = status
98 self.msg = msg
100 def render(self, req):
101 req.setResponseCode(self.code, self.status)
102 req.setContentType("text/plain")
103 req.write(self.msg)