0081224d073ab84a348ac57ffda5ccfe9b56b83e
[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         return [
76             Setting(section=self.setting_section, help=self.__doc__),
77             Setting(section=self.setting_section, option='x units', value='nm',
78                     help='Set the units used for the x data.'),
79             ]
80
81     def is_me(self):
82         """YOU MUST OVERRIDE Driver.is_me.
83
84         RETURNS: Boolean (`True` or `False`)
85
86         This method is a heuristic that looks at the file content and
87         decides if the file can be opened by the driver itself.  It
88         returns `True` if the file opened can be interpreted by the
89         current driver, `False` otherwise.  Defining this method allows
90         Hooke to understand what kind of files we're looking at
91         automatically.
92         """
93
94         f = open(self.filename, 'r')
95         header = f.readline() # we only need the first line
96         f.close()
97
98         """Our "magic fingerprint" is the TUTORIAL_FILE header. Of
99         course, depending on the data file, you can have interesting
100         headers, or patterns, etc. that you can use to guess the data
101         format. What matters is successful recognition and the boolean
102         (True/False) return.
103         """
104         if header.startswith('TUTORIAL_FILE'):
105             return True
106         return False
107
108     def read(self, path):
109         f = open(path,'r') # open the file for reading
110         """In this case, we have a data format that is just a list of
111         ASCII values, so we can just divide that in rows, and generate
112         a list with each item being a row.  Of course if your data
113         files are binary, or follow a different approach, do whatever
114         you need. :)
115         """
116         self.data = list(self.filedata)
117         f.close() # remember to close the file
118
119         data = curve.Data()
120         info = {'filetype':'tutorial', 'experiment':'generic'}
121         return (data, info)