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