5397a6e84cdd0edf1df9aec57346535edab8c698
[hooke.git] / hooke / libviewer.py
1 '''
2 Basic Viewer and ascii saver example
3
4 Copyright (C) 2008 Alberto Gomez-Casado (University of Twente).
5
6 This program is released under the GNU General Public License version 2.
7
8 So far outlet is useless.  That's where viewers (well, only one now)
9 do the job. They query outlet for data of some type, process it and
10 give some elaborated output. Again the only thing they have to know
11 about outlet is how to call it and the 'line' format.
12
13 I put in the viewer plugin a list to keep them so it is possible to
14 create many. The basic operations in viewer.py
15    - vwnew: adds a new viewer to the list, and sets it so it defines
16 what type of data it will be interested in
17    - vwaction: tells the viewer to 'do stuff', that is, retrieve data
18 and process it. Just a wrapper call for the specific one of each
19 viewer. 
20 '''
21
22 import liboutlet as lout
23 import libinput as linput
24
25
26 class Viewer(object):
27     source=[]
28     data=[]
29     dtype='all'
30     action=[]  #alias to call the actual viewer function, makes it general
31
32
33     def setdtype(self, dt):
34         #determines wich type of data will be retrieved from outlet
35         self.dtype=dt   
36
37     def show(self):
38         #TODO should print only data matching 'type'
39         self.source.printbuf()
40
41     def getdata(self):
42         #retrieves data from outlet
43         self.data=self.source.read_type(self.dtype)
44
45
46 class Ascii(Viewer):
47 #example viewer, it just retrieves data and writes it to a text file
48 #probably this should be in viewer.py?
49
50         def __init__(self,outref):
51                 self.source=outref
52                 #tells the viewer which outlet has the data (so far only one in hooke)
53                 self.action=self.dump
54                 #this allows to call dump (or any other function, depending on the viewer) from the CLI using 'vwaction'
55
56         def dump(self):
57                 #retrieves and saves data
58                 self.getdata()
59                 destination=linput.safeinput('Enter filename:',['results.txt'])
60                 destfile=open(destination,'w+')
61                 destfile.write('\n'.join(self.data))
62                 destfile.close()