make container local
[enigma2-plugins.git] / webinterface / src / WebChilds / Screengrab.py
1 from enigma import eConsoleAppContainer
2
3 from twisted.web2 import resource, stream, responsecode, http, http_headers
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, req):
15         self.baseCmd = ['/usr/bin/grab', 'grab']
16         self.args = []
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 req.args.items():
26             if key in GrabResource.SPECIAL_ARGS:                
27                 
28                 if key == 'format':                
29                     format = req.args['format'][0]
30                     
31                     if format == 'png':
32                         #-p produce png files instead of bmp
33                         imageformat = ".%s" %format
34                         self.args.append('-p')
35                     elif format == 'jpg':
36                         #-j (quality) produce jpg files instead of bmp
37                     
38                         imageformat = ".%s" %format
39                         self.args.append('-j')
40                         #Quality Setting                    
41                         if req.args.has_key("jpgquali"):
42                             self.args.append("%s" %(req.args["jpgquali"][0]) )
43                         else:                            
44                             self.args.append('80')
45                 
46                 if key == 'filename':
47                     filename = req.args['filename'][0]
48                 
49                 if key == 'save':
50                     save = True
51                                                 
52             else:
53                 if key == "o" and videoOnly is True:
54                     continue
55                 if key == "v" and osdOnly is True:
56                     continue                                        
57                                 
58                 self.args.append("-%s" %key )
59                 
60                 if value is not None:
61                     if len(value[0]) > 0:
62                         self.args.append("%s" %value[0])
63                 
64
65         if not os_path.exists(self.GRAB_BIN):
66             return http.Response(responsecode.OK,stream='Grab is not installed at %s. Please install package aio-grab.' %self.GRAB_BIN)
67         else:
68             headers = http_headers.Headers()
69             headers.addRawHeader('Content-Disposition', 'inline; filename=screenshot.%s;'%imageformat)
70             headers.addRawHeader('Content-Type','image/%s'%imageformat)
71             
72             filename = filename+imageformat
73             self.args.append(filename)
74             cmd = self.baseCmd + self.args
75             
76             return http.Response(responsecode.OK,headers,stream=GrabStream(cmd, filename, save))
77        
78 class GrabStream(stream.ProducerStream):
79     '''
80         used to start the grab-bin in the console in the background
81         while this takes some time, the browser must wait until the grabis finished
82     '''
83     def __init__(self, cmd, target=None, save=False):
84         self.target = target
85         self.save = save
86         self.output = ''
87         stream.ProducerStream.__init__(self)
88
89         container = eConsoleAppContainer()
90         container.appClosed.append(self.cmdFinished)
91         container.dataAvail.append(self.dataAvail)
92         
93         print '[Screengrab.py] starting AiO grab with cmdline:', cmd
94         container.execute(*cmd)
95
96     def cmdFinished(self, data):
97         if int(data) is 0 and self.target is not None:
98             try:
99                 fp = open(self.target)
100                 self.write(fp.read())
101                 fp.close()
102                 if self.save is False:
103                     os_remove(self.target)
104             except Exception,e:
105                 self.write('Internal error while reading target file')
106         elif int(data) is 0 and self.target is None:
107             self.write(self.output)
108         elif int(data) is 1:
109             self.write(self.output)
110         else:
111             self.write('Internal error')
112         self.finish()
113
114     def dataAvail(self, data):
115         print '[Screengrab.py] data Available ', data