a8b59cdec8bbad1191f8f97440ca14e32ae26e08
[hooke.git] / hooke / driver / tutorial.py
1 # Copyright
2
3 """Tutorial driver for Hooke.
4
5 This example driver explains driver construction.
6 """
7
8 """
9 Here we define a simple file format that is read by this driver. The
10 file format is as following::
11
12     TUTORIAL_FILE
13     PLOT1
14     X1
15     n1   <- ?
16     n2
17     ...
18     nN
19     Y1
20     n1
21     n2
22     ...
23     nN
24     X2
25     n1
26     n2
27     ..
28     nN
29     Y2
30     n1
31     n2
32     ..
33     nN
34     PLOT2
35     X1
36     ...
37     Y1
38     ...
39     X2
40     ...
41     Y2
42     ...
43     END
44
45 that is, two plots with two datasets each.
46 """
47
48 # The following are relative imports.  See PEP 328 for details
49 #   http://www.python.org/dev/peps/pep-0328/
50 from .. import curve as curve # this module defines data containers.
51 from .. import experiment as experiment # this module defines expt. types
52 from ..config import Setting # configurable setting class
53 from . import Driver as Driver # this is the Driver base class
54
55 # The driver must inherit from the parent
56 # :class:`hooke.driver.Driver` (which we have imported as `Driver`).
57 class TutorialDriver (Driver):
58     """Handle simple text data as an example Driver.
59     """
60     def __init__(self):
61         """YOU MUST OVERRIDE Driver.__init__.
62
63         Here you set a value for `name` to identify your driver.  It
64         should match the module name.
65         """
66         super(TutorialDriver, self).__init__(name='tutorial')
67
68     def default_settings(self):
69         """Return a list of any configurable settings for your driver.
70
71         If your driver does not have any configurable settings, there
72         is no need to override this method.
73         """
74         return [
75             Setting(section=self.setting_section, help=self.__doc__),
76             Setting(section=self.setting_section, option='x units', value='nm',
77                     help='Set the units used for the x data.'),
78             ]
79
80     def is_me(self):
81         """YOU MUST OVERRIDE Driver.is_me.
82
83         RETURNS: Boolean (`True` or `False`)
84
85         This method is a heuristic that looks at the file content and
86         decides if the file can be opened by the driver itself.  It
87         returns `True` if the file opened can be interpreted by the
88         current driver, `False` otherwise.  Defining this method allows
89         Hooke to understand what kind of files we're looking at
90         automatically.
91         """
92
93         f = open(self.filename, 'r')
94         header = f.readline() # we only need the first line
95         f.close()
96
97         """Our "magic fingerprint" is the TUTORIAL_FILE header. Of
98         course, depending on the data file, you can have interesting
99         headers, or patterns, etc. that you can use to guess the data
100         format. What matters is successful recognition and the boolean
101         (True/False) return.
102         """
103         if header.startswith('TUTORIAL_FILE'):
104             return True
105         return False
106
107     def read(self, path):
108         f = open(path,'r') # open the file for reading
109         """In this case, we have a data format that is just a list of
110         ASCII values, so we can just divide that in rows, and generate
111         a list with each item being a row.  Of course if your data
112         files are binary, or follow a different approach, do whatever
113         you need. :)
114         """
115         self.data = list(self.filedata)
116         f.close() # remember to close the file
117
118         data = curve.Data()
119         info = {'filetype':'tutorial', 'experiment':'generic'}
120         return (data, info)