add possibility to enable twisted logging
[enigma2-plugins.git] / webinterface / src / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2
3 sessions = [ ]
4
5 # set DEBUG to True, if twisted should write logoutput to a file.
6 DEBUG = False 
7 DEBUGFILE= "/tmp/twisted.log"
8
9 def startWebserver():
10         from twisted.internet import reactor
11         from twisted.web2 import server, channel, static, resource, stream, http_headers, responsecode, http
12         from twisted.python import util
13         import webif
14
15         class ScreenPage(resource.Resource):
16                 def __init__(self, path):
17                         self.path = path
18                         
19                 def render(self, req):
20                         global sessions
21                         if sessions == [ ]:
22                                 return http.Response(200, stream="please wait until enigma has booted")
23
24                         class myProducerStream(stream.ProducerStream):
25                                 closed_callback = None
26
27                                 def close(self):
28                                         if self.closed_callback:
29                                                 self.closed_callback()
30                                         stream.ProducerStream.close(self)
31
32                         s = myProducerStream()
33                         webif.renderPage(s, self.path, req, sessions[0])  # login?
34
35                         return http.Response(stream=s)
36
37                 def locateChild(self, request, segments):
38                         path = '/'.join(["web"] + segments)
39                         if path[-1:] == "/":
40                                 path += "index.html"
41
42                         path += ".xml"
43                         return ScreenPage(path), ()
44
45         class Toplevel(resource.Resource):
46                 addSlash = True
47
48                 def render(self, req):
49                         return http.Response(responsecode.OK, {'Content-type': http_headers.MimeType('text', 'html')},
50                                 stream='Hello! You want go to <a href="/web/">OSD</a> instead.')
51
52                 child_web = ScreenPage("/") # "/web"
53                 child_hdd = static.File("/hdd")
54                 child_webdata = static.File(util.sibpath(__file__, "web-data")) # FIXME: web-data appears as webdata
55
56         site = server.Site(Toplevel())
57
58         reactor.listenTCP(80, channel.HTTPFactory(site))
59
60 def autostart(reason, **kwargs):
61         if "session" in kwargs:
62                 global sessions
63                 sessions.append(kwargs["session"])
64                 return
65
66         if reason == 0:
67                 try:
68                         """
69                          in normal console output, twisted will print only the first Traceback.
70                          is this a bug in twisted or a conflict with enigma2?
71                          with this option enabled, twisted will print all TB to the logfile
72                          use tail -f <file> to view this log
73                         """
74                         if DEBUG:
75                                 from twisted.python.log import startLogging
76                                 print "start twisted logfile, writing to %s" % DEBUGFILE 
77                                 startLogging(open(DEBUGFILE,'w'))
78                         
79                         startWebserver()
80                 except ImportError:
81                         print "twisted not available, not starting web services"
82
83 def Plugins(**kwargs):
84         return PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart)