import cleanup
[enigma2-plugins.git] / webinterface / src / WebChilds / Screengrab.py
1 from enigma import eConsoleAppContainer
2
3 from twisted.web2 import resource, stream, responsecode, http
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     grab_target = "/tmp/screenshot.bmp"
13     
14     def render(self, req):
15         class GrabStream(stream.ProducerStream):
16             def __init__(self,cmd,target=None,save=False):
17                 self.cmd = cmd
18                 self.target = target
19                 self.save = save
20                 self.output = ""
21                 stream.ProducerStream.__init__(self)
22                 
23                 self.container = eConsoleAppContainer()
24                 self.container.appClosed.get().append(self.cmdFinished)
25                 self.container.dataAvail.get().append(self.dataAvail)
26                 self.container.execute(cmd)
27
28             def cmdFinished(self,data):
29                 if int(data) is 0 and self.target is not None:
30                     try:
31                         fp = open(self.target)
32                         self.write(fp.read())
33                         fp.close()
34                         if self.save is False:
35                             os_remove(self.target)
36                     except Exception,e:
37                         self.write("internal error while reading target file")                        
38                 elif int(data) is 0 and self.target is None:
39                     self.write(self.output)
40                 elif int(data) is 1:
41                     self.write(self.output)
42                 else:
43                     self.write("internal error")
44                 self.finish()    
45                     
46             def dataAvail(self,data):
47                 self.output += data
48             
49         if req.args.has_key("filename"):
50             filetarget = req.args['filename'][0]
51         else:
52             filetarget = self.grab_target
53         
54         if req.args.has_key("save"):
55             save_image = True
56         else:
57             save_image = False
58         
59         if os_path.exists(self.grab_bin) is not True:
60             return    http.Response(responsecode.OK,stream="grab is not installed at '%s'. go and fix it."%self.grab_bin)
61         elif req.args.has_key("command"): 
62             cmd = req.args['command'][0].replace("-","")
63             if cmd == "o":
64                 return http.Response(responsecode.OK,stream=GrabStream(self.grab_bin+" -o "+filetarget,target=filetarget,save=save_image))
65             elif cmd == "v":
66                 return http.Response(responsecode.OK,stream=GrabStream(self.grab_bin+" -v "+filetarget,target=filetarget,save=save_image))
67             elif cmd == "":
68                 return http.Response(responsecode.OK,stream=GrabStream(self.grab_bin+" "+filetarget,target=filetarget,save=save_image))
69             else:
70                 return http.Response(responsecode.OK,stream=GrabStream(self.grab_bin+" -h"))
71         else:
72             return http.Response(responsecode.OK,stream=GrabStream(self.grab_bin+" "+filetarget,target=filetarget,save=save_image))