move xml files into web/, support hierarchy
[enigma2.git] / lib / python / Plugins / Extensions / WebInterface / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2
3 sessions = [ ]
4
5 def startWebserver():
6         from twisted.internet import reactor
7         from twisted.web2 import server, http, static, resource, stream, http_headers, responsecode
8         from twisted.python import util
9         import webif
10
11         class ScreenPage(resource.Resource):
12                 def __init__(self, path):
13                         self.path = path
14                         
15                 def render(self, req):
16                         global sessions
17                         if sessions == [ ]:
18                                 return http.Response(200, stream="please wait until enigma has booted")
19                         
20                         s = stream.ProducerStream()
21                         webif.renderPage(s, self.path, sessions[0])  # login?
22                         return http.Response(stream=s)
23
24                 def locateChild(self, request, segments):
25                         path = '/'.join(["web"] + segments)
26                         if path[-1:] == "/":
27                                 path += "index"
28                         
29                         path += ".xml"
30                         return ScreenPage(path), ()
31
32         class Toplevel(resource.Resource):
33                 addSlash = True
34                 
35                 def render(self, req):
36                         return http.Response(responsecode.OK, {'Content-type': http_headers.MimeType('text', 'html')},
37                                 stream='Hello! you want probably go to <a href="/test">the test</a> instead.')
38
39                 child_web = ScreenPage("/") # "/web"
40                 child_hdd = static.File("/hdd")
41                 child_webdata = static.File(util.sibpath(__file__, "web-data"))
42
43         site = server.Site(Toplevel())
44         
45         reactor.listenTCP(80, http.HTTPFactory(site))
46
47 def autostart(reason, **kwargs):
48         if "session" in kwargs:
49                 global sessions
50                 sessions.append(kwargs["session"])
51                 return
52
53         if reason == 0:
54                 try:
55                         startWebserver()
56                 except ImportError:
57                         print "twisted not available, not starting web services"
58
59 def Plugins(**kwargs):
60         return PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart)