bd1ec491ad1403611e1a62c8f486b7aab5c24596
[vuplus_dvbapp-plugin] / webinterface / src / usr / lib / enigma2 / 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, channel, static, resource, stream, http_headers, responsecode, http
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                         class myProducerStream(stream.ProducerStream):
21                                 closed_callback = None
22
23                                 def close(self):
24                                         if self.closed_callback:
25                                                 self.closed_callback()
26                                         stream.ProducerStream.close(self)
27
28                         s = myProducerStream()
29                         webif.renderPage(s, self.path, req, sessions[0])  # login?
30
31                         return http.Response(stream=s)
32
33                 def locateChild(self, request, segments):
34                         path = '/'.join(["web"] + segments)
35                         if path[-1:] == "/":
36                                 path += "index.html"
37
38                         path += ".xml"
39                         return ScreenPage(path), ()
40
41         class Toplevel(resource.Resource):
42                 addSlash = True
43
44                 def render(self, req):
45                         return http.Response(responsecode.OK, {'Content-type': http_headers.MimeType('text', 'html')},
46                                 stream='Hello! You want go to <a href="/web/">OSD</a> instead.')
47
48                 child_web = ScreenPage("/") # "/web"
49                 child_hdd = static.File("/hdd")
50                 child_webdata = static.File(util.sibpath(__file__, "web-data")) # FIXME: web-data appears as webdata
51
52         site = server.Site(Toplevel())
53
54         reactor.listenTCP(80, channel.HTTPFactory(site))
55
56 def autostart(reason, **kwargs):
57         if "session" in kwargs:
58                 global sessions
59                 sessions.append(kwargs["session"])
60                 return
61
62         if reason == 0:
63                 try:
64                         startWebserver()
65                 except ImportError:
66                         print "twisted not available, not starting web services"
67
68 def Plugins(**kwargs):
69         return PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart)