moved childs to seperate dir ./WebChilds
[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 import os
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                     fp = open(self.target)
31                     self.write(fp.read())
32                     fp.close()
33                     if self.save is False:
34                         os.remove(self.target)
35                 elif int(data) is 0 and self.target is None:
36                     self.write(self.output)
37                 else:
38                     self.write("internal error")
39                 self.finish()    
40                     
41             def dataAvail(self,data):
42                 self.output += data
43             
44         if req.args.has_key("filename"):
45             filetarget = req.args['filename'][0]
46         else:
47             filetarget = self.grab_target
48         
49         if req.args.has_key("save"):
50             save_image = True
51         else:
52             save_image = False
53         
54         if os.path.exists(self.grab_bin) is not True:
55             return    http.Response(responsecode.OK,stream="grab is not installed at '%s'. go and fix it."%self.grab_bin)
56         elif req.args.has_key("command"): 
57             cmd = req.args['command'][0].replace("-","")
58             if cmd == "o":
59                 return http.Response(responsecode.OK,stream=GrabStream(self.grab_bin+" -o "+filetarget,target=filetarget,save=save_image))
60             elif cmd == "v":
61                 return http.Response(responsecode.OK,stream=GrabStream(self.grab_bin+" -v "+filetarget,target=filetarget,save=save_image))
62             elif cmd == "":
63                 return http.Response(responsecode.OK,stream=GrabStream(self.grab_bin+" "+filetarget,target=filetarget,save=save_image))
64             else:
65                 return http.Response(responsecode.OK,stream=GrabStream(self.grab_bin+" -h"))
66         else:
67             return http.Response(responsecode.OK,stream=GrabStream(self.grab_bin+" "+filetarget,target=filetarget,save=save_image))