1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
5 # Hooke is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 # Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke. If not, see
17 # <http://www.gnu.org/licenses/>.
19 """Tutorial driver for Hooke.
21 This example driver explains driver construction.
25 Here we define a simple file format that is read by this driver. The
26 file format is as following::
61 that is, two plots with two datasets each.
66 # The following are relative imports. See PEP 328 for details
67 # http://www.python.org/dev/peps/pep-0328/
68 from .. import curve as curve # this module defines data containers.
69 from ..config import Setting # configurable setting class
70 from . import Driver as Driver # this is the Driver base class
72 # The driver must inherit from the parent
73 # :class:`hooke.driver.Driver` (which we have imported as `Driver`).
74 class TutorialDriver (Driver):
75 """Handle simple text data as an example Driver.
78 """YOU MUST OVERRIDE Driver.__init__.
80 Here you set a value for `name` to identify your driver. It
81 should match the module name.
83 super(TutorialDriver, self).__init__(name='tutorial')
85 def default_settings(self):
86 """Return a list of any configurable settings for your driver.
88 If your driver does not have any configurable settings, there
89 is no need to override this method.
92 Setting(section=self.setting_section, help=self.__doc__),
93 Setting(section=self.setting_section, option='x units', value='nm',
94 help='Set the units used for the x data.'),
97 def is_me(self, path):
98 """YOU MUST OVERRIDE Driver.is_me.
100 RETURNS: Boolean (`True` or `False`)
102 This method is a heuristic that looks at the file content and
103 decides if the file can be opened by the driver itself. It
104 returns `True` if the file opened can be interpreted by the
105 current driver, `False` otherwise. Defining this method allows
106 Hooke to understand what kind of files we're looking at
109 if os.path.isdir(path):
113 header = f.readline() # we only need the first line
116 """Our "magic fingerprint" is the TUTORIAL_FILE header. Of
117 course, depending on the data file, you can have interesting
118 headers, or patterns, etc. that you can use to guess the data
119 format. What matters is successful recognition and the boolean
122 if header.startswith('TUTORIAL_FILE'):
126 def read(self, path, info=None):
127 f = open(path,'r') # open the file for reading
128 """In this case, we have a data format that is just a list of
129 ASCII values, so we can just divide that in rows, and generate
130 a list with each item being a row. Of course if your data
131 files are binary, or follow a different approach, do whatever
134 self.data = list(self.filedata)
135 f.close() # remember to close the file