1 # Copyright (c) 2008 Massimo Sandal
4 """Tutorial driver for Hooke.
6 This example driver explains driver construction.
10 Here we define a simple file format that is read by this driver. The
11 file format is as following::
46 that is, two plots with two datasets each.
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
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.
62 """YOU MUST OVERRIDE Driver.__init__.
64 Here you set a value for `name` to identify your driver. It
65 should match the module name.
67 super(TutorialDriver, self).__init__(name='tutorial')
69 def default_settings(self):
70 """Return a list of any configurable settings for your driver.
72 If your driver does not have any configurable settings, there
73 is no need to override this method.
75 section = '%s driver' % self.name
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.'),
83 """YOU MUST OVERRIDE Driver.is_me.
85 RETURNS: Boolean (`True` or `False`)
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
95 f = open(self.filename, 'r')
96 header = f.readline() # we only need the first line
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
105 if header.startswith('TUTORIAL_FILE'):
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
117 self.data = list(self.filedata)
118 f.close() # remember to close the file
121 info = {'filetype':'tutorial', 'experiment':'generic'}