debuggers.hg

view tools/python/xen/web/SrvDir.py @ 19975:e845326ae203

python: Remove tab indents.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jul 14 14:43:19 2009 +0100 (2009-07-14)
parents 728df5693cd2
children
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
20 from xen.xend import sxp
21 from xen.xend import PrettyPrint
22 from xen.xend.Args import ArgError
23 from xen.xend.XendError import XendError, XendInvalidDomain
24 #from xen.xend.XendLogging import log
26 import resource
27 import http
29 from xen.web.SrvBase import SrvBase
31 class SrvConstructor:
32 """Delayed constructor for sub-servers.
33 Does not import the sub-server class or create the object until needed.
34 """
36 def __init__(self, klass):
37 """Create a constructor. It is assumed that the class
38 should be imported as 'from xen.xend.server.klass import klass'.
40 klass name of its class
41 """
42 self.klass = klass
43 self.obj = None
45 def getobj(self):
46 """Get the sub-server object, importing its class and instantiating it if
47 necessary.
48 """
49 if not self.obj:
50 exec 'from xen.xend.server.%s import %s' % (self.klass, self.klass)
51 klassobj = eval(self.klass)
52 self.obj = klassobj()
53 return self.obj
55 class SrvDir(SrvBase):
56 """Base class for directory servlets.
57 """
58 isLeaf = False
60 def __init__(self):
61 SrvBase.__init__(self)
62 self.table = {}
63 self.order = []
65 def noChild(self, msg):
66 return resource.ErrorPage(http.NOT_FOUND, msg=msg)
68 def getChild(self, x, req):
69 if x == '': return self
70 try:
71 val = self.get(x)
72 except XendError, ex:
73 return self.noChild(str(ex))
74 except XendInvalidDomain, ex:
75 return self.noChild(str(ex))
76 if val is None:
77 return self.noChild('Not found: ' + str(x))
78 else:
79 return val
81 def get(self, x):
82 val = self.table.get(x)
83 if isinstance(val, SrvConstructor):
84 val = val.getobj()
85 return val
87 def add(self, x, v=None):
88 if v is None:
89 v = 'SrvDir'
90 if isinstance(v, types.StringType):
91 v = SrvConstructor(v)
92 self.table[x] = v
93 self.order.append(x)
94 return v
96 def render_GET(self, req):
97 if self.use_sxp(req):
98 req.setHeader("Content-type", sxp.mime_type)
99 self.ls(req, 1)
100 else:
101 req.write('<html><head></head><body>')
102 self.print_path(req)
103 self.ls(req)
104 self.form(req)
105 req.write('</body></html>')
106 return ''
108 def ls(self, req, use_sxp=0):
109 url = req.prePathURL()
110 if not url.endswith('/'):
111 url += '/'
112 if use_sxp:
113 req.write('(ls ')
114 for k in self.order:
115 req.write(' ' + k)
116 req.write(')')
117 else:
118 req.write('<ul>')
119 for k in self.order:
120 v = self.get(k)
121 req.write('<li><a href="%s%s">%s</a></li>'
122 % (url, k, k))
123 req.write('</ul>')
125 def form(self, req):
126 pass