aboutsummaryrefslogtreecommitdiff
path: root/lib/python/Plugins/Extensions/WebInterface/plugin.py
blob: cc9a546b93ec927d70f148f634ff0a12a8190c1e (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
from Plugins.Plugin import PluginDescriptor

sessions = [ ]

def startWebserver():
	from twisted.internet import reactor
	from twisted.web2 import server, http, static, resource, stream, http_headers, responsecode
	from twisted.python import util
	import webif

	class ScreenPage(resource.Resource):
		def __init__(self, path):
			self.path = path
			
		def render(self, req):
			global sessions
			if sessions == [ ]:
				return http.Response(200, stream="please wait until enigma has booted")
			
			s = stream.ProducerStream()
			webif.renderPage(s, self.path, sessions[0])  # login?
			return http.Response(stream=s)

		def locateChild(self, request, segments):
			path = '/'.join(["web"] + segments)
			if path[-1:] == "/":
				path += "index"
			
			path += ".xml"
			return ScreenPage(path), ()

	class Toplevel(resource.Resource):
		addSlash = True
		
		def render(self, req):
			return http.Response(responsecode.OK, {'Content-type': http_headers.MimeType('text', 'html')},
				stream='Hello! you want probably go to <a href="/test">the test</a> instead.')

		child_web = ScreenPage("/") # "/web"
		child_hdd = static.File("/hdd")
		child_webdata = static.File(util.sibpath(__file__, "web-data"))

	site = server.Site(Toplevel())
	
	reactor.listenTCP(80, http.HTTPFactory(site))

def autostart(reason, **kwargs):
	if "session" in kwargs:
		global sessions
		sessions.append(kwargs["session"])
		return

	if reason == 0:
		try:
			startWebserver()
		except ImportError:
			print "twisted not available, not starting web services"

def Plugins(**kwargs):
	return PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart)