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