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.
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.'),
82 """YOU MUST OVERRIDE Driver.is_me.
84 RETURNS: Boolean (`True` or `False`)
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
94 f = open(self.filename, 'r')
95 header = f.readline() # we only need the first line
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
104 if header.startswith('TUTORIAL_FILE'):
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
116 self.data = list(self.filedata)
117 f.close() # remember to close the file
120 info = {'filetype':'tutorial', 'experiment':'generic'}