debuggers.hg

view tools/python/xen/web/SrvDir.py @ 4672:d781b9d08e80

bitkeeper revision 1.1327.2.4 (426918a34Af7gihN8mTkq-P3KrAZXg)

Remove twisted from save/migrate handling.
This needs to use threads, so add thread support for
http server requests.

Signed-off-by: Mike Wray <mike.wray@hp.com>
author mjw@wray-m-3.hpl.hp.com
date Fri Apr 22 15:30:43 2005 +0000 (2005-04-22)
parents a838a908e38e
children 369e382b2f81
line source
1 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
3 import types
5 from xen.xend import sxp
6 from xen.xend import PrettyPrint
7 from xen.xend.Args import ArgError
8 from xen.xend.XendError import XendError
9 #from xen.xend.XendLogging import log
11 import resource
12 import http
14 from xen.web.SrvBase import SrvBase
16 class SrvConstructor:
17 """Delayed constructor for sub-servers.
18 Does not import the sub-server class or create the object until needed.
19 """
21 def __init__(self, klass):
22 """Create a constructor. It is assumed that the class
23 should be imported as 'import klass from klass'.
25 klass name of its class
26 """
27 self.klass = klass
28 self.obj = None
30 def getobj(self):
31 """Get the sub-server object, importing its class and instantiating it if
32 necessary.
33 """
34 if not self.obj:
35 exec 'from xen.xend.server.%s import %s' % (self.klass, self.klass)
36 klassobj = eval(self.klass)
37 self.obj = klassobj()
38 return self.obj
40 class SrvDir(SrvBase):
41 """Base class for directory servlets.
42 """
43 isLeaf = False
45 def __init__(self):
46 SrvBase.__init__(self)
47 self.table = {}
48 self.order = []
50 def noChild(self, msg):
51 return resource.ErrorPage(http.NOT_FOUND, msg=msg)
53 def getChild(self, x, req):
54 if x == '': return self
55 try:
56 val = self.get(x)
57 except XendError, ex:
58 return self.noChild(str(ex))
59 if val is None:
60 return self.noChild('Not found: ' + str(x))
61 else:
62 return val
64 def get(self, x):
65 val = self.table.get(x)
66 if isinstance(val, SrvConstructor):
67 val = val.getobj()
68 return val
70 def add(self, x, v=None):
71 if v is None:
72 v = 'SrvDir'
73 if isinstance(v, types.StringType):
74 v = SrvConstructor(v)
75 self.table[x] = v
76 self.order.append(x)
77 return v
79 def render_GET(self, req):
80 try:
81 if self.use_sxp(req):
82 req.setHeader("Content-type", sxp.mime_type)
83 self.ls(req, 1)
84 else:
85 req.write('<html><head></head><body>')
86 self.print_path(req)
87 self.ls(req)
88 self.form(req)
89 req.write('</body></html>')
90 return ''
91 except Exception, ex:
92 self._perform_err(ex, "GET", req)
94 def ls(self, req, use_sxp=0):
95 url = req.prePathURL()
96 if not url.endswith('/'):
97 url += '/'
98 if use_sxp:
99 req.write('(ls ')
100 for k in self.order:
101 req.write(' ' + k)
102 req.write(')')
103 else:
104 req.write('<ul>')
105 for k in self.order:
106 v = self.get(k)
107 req.write('<li><a href="%s%s">%s</a></li>'
108 % (url, k, k))
109 req.write('</ul>')
111 def form(self, req):
112 pass