cleanup Screengrab a bit
[enigma2-plugins.git] / webinterface / src / WebChilds / Screengrab.py
1 from enigma import eConsoleAppContainer
2
3 from twisted.web import resource, http, http_headers, server
4
5 from os import path as os_path, remove as os_remove
6
7 class GrabResource(resource.Resource):
8         '''
9                 this is a interface to Seddis AiO Dreambox Screengrabber
10         '''
11         GRAB_BIN = '/usr/bin/grab'
12         SPECIAL_ARGS = ('format', 'filename', 'save')
13
14         def render(self, request):
15                 args = []
16                 append = args.append
17
18                 # some presets
19                 filename = 'screenshot'
20                 imageformat = 'bmp'
21                 osdOnly = False
22                 videoOnly = False
23                 save = False
24
25                 for key, value in request.args.items():
26                         if key in GrabResource.SPECIAL_ARGS:
27                                 if key == 'format':
28                                         format = value[0]
29
30                                         if format == 'png':
31                                                 #-p produce png files instead of bmp
32                                                 imageformat = format
33                                                 append('-p')
34                                         elif format == 'jpg':
35                                                 #-j (quality) produce jpg files instead of bmp
36
37                                                 imageformat = format
38                                                 append('-j')
39                                                 #Quality Setting
40                                                 if 'jpgquali' in request.args:
41                                                         append("%s" % (request.args["jpgquali"][0]) )
42                                                         del request.args['jpgquali']
43                                                 else:
44                                                         append('80')
45
46                                 elif key == 'filename':
47                                         filename = value[0]
48
49                                 elif key == 'save':
50                                         save = True
51                         else:
52                                 if key == "o" and videoOnly is True:
53                                         continue
54                                 if key == "v" and osdOnly is True:
55                                         continue
56
57                                 append("-%s" %key )
58
59                                 if value:
60                                         if value[0]:
61                                                 append("%s" % (value[0]))
62
63                 if not os_path.exists(self.GRAB_BIN):
64                         request.setResponseCode(http.OK)
65                         request.write('Grab is not installed at %s. Please install package aio-grab.' %self.GRAB_BIN)
66                         request.finish()
67
68                 else:
69                         request.setHeader('Content-Disposition', 'inline; filename=screenshot.%s;' %imageformat)
70                         request.setHeader('Content-Type','image/%s' %imageformat)
71
72                         filename = filename+imageformat
73                         append(filename)
74                         cmd = ['/usr/bin/grab', '/usr/bin/grab'] + args
75
76                         GrabStream(request, cmd, filename, save)
77
78                 return server.NOT_DONE_YET
79
80 class GrabStream:
81         '''
82                 used to start the grab-bin in the console in the background
83                 while this takes some time, the browser must wait until the grabis finished
84         '''
85         def __init__(self, request, cmd, target=None, save=False):
86                 self.target = target
87                 self.save = save
88                 self.output = ''
89                 self.request = request
90
91                 self.container = eConsoleAppContainer()
92                 self.container.appClosed.append(self.cmdFinished)
93                 self.container.dataAvail.append(self.dataAvail)
94
95                 print '[Screengrab.py] starting AiO grab with cmdline:', cmd
96                 self.container.execute(*cmd)
97
98         def cmdFinished(self, data):
99                 print '[Screengrab.py] cmdFinished'
100                 if int(data) is 0 and self.target is not None:
101                         try:
102                                 fp = open(self.target)
103                                 self.request.write(fp.read())
104                                 fp.close()
105                                 if self.save is False:
106                                         os_remove(self.target)
107                                         print '[Screengrab.py] %s removed' %self.target
108                         except Exception,e:
109                                 self.request.write('Internal error while reading target file')
110                 elif int(data) is 0 and self.target is None:
111                         self.request.write(self.output)
112                 elif int(data) is 1:
113                         self.request.write(self.output)
114                 else:
115                         self.request.write('Internal error')
116
117                 self.request.finish()
118
119         def dataAvail(self, data):
120                 print '[Screengrab.py] data Available ', data
121