aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xen/lib/xend/server/SrvDir.py
blob: c49c0b36ba9fd7c1a02ba6106983850423daf28c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright (C) 2004 Mike Wray <mike.wray@hp.com>

from twisted.web import error
from xen.xend import sxp
from SrvBase import SrvBase

class SrvConstructor:
    """Delayed constructor for sub-servers.
    Does not import the sub-server class or create the object until needed.
    """
    
    def __init__(self, klass):
        """Create a constructor. It is assumed that the class
        should be imported as 'import klass from klass'.

        klass	name of its class
        """
        self.klass = klass
        self.obj = None

    def getobj(self):
        """Get the sub-server object, importing its class and instantiating it if
        necessary.
        """
        if not self.obj:
            exec 'from %s import %s' % (self.klass, self.klass)
            klassobj = eval(self.klass)
            self.obj = klassobj()
        return self.obj

class SrvDir(SrvBase):
    """Base class for directory servlets.
    """
    isLeaf = False
    
    def __init__(self):
        SrvBase.__init__(self)
        self.table = {}
        self.order = []

    def getChild(self, x, req):
        if x == '': return self
        val = self.get(x)
        if val is None:
            return error.NoResource('Not found')
        else:
            return val

    def get(self, x):
        val = self.table.get(x)
        if val is not None:
            val = val.getobj()
        return val

    def add(self, x, xclass = None):
        if xclass is None:
            xclass = 'SrvDir'
        self.table[x] = SrvConstructor(xclass)
        self.order.append(x)

    def render_GET(self, req):
        if self.use_sxp(req):
            req.setHeader("Content-type", sxp.mime_type)
            self.ls(req, 1)
        else:
            req.write('<html><head></head><body>')
            self.print_path(req)
            self.ls(req)
            self.form(req)
            req.write('</body></html>')
        return ''
            
    def ls(self, req, use_sxp=0):
        url = req.prePathURL()
        if not url.endswith('/'):
            url += '/'
        if use_sxp:
           req.write('(ls ')
           for k in self.order:
               req.write(' ' + k)
           req.write(')')
        else:
            req.write('<ul>')
            for k in self.order:
                v = self.get(k)
                req.write('<li><a href="%s%s">%s</a></li>'
                          % (url, k, k))
            req.write('</ul>')

    def form(self, req):
        pass