Use *_SETTING_SECTION in hooke.plugin/.driver/.ui for section name
[hooke.git] / hooke / driver / __init__.py
1 """The driver module provides :class:`Driver`\s for identifying and
2 reading data files.
3
4 This allows Hooke to be data-file agnostic.  Drivers for various
5 commercial force spectroscopy microscopes are provided, and it's easy
6 to write your own to handle your lab's specific format.
7 """
8
9 from ..config import Setting
10 from ..plugin import construct_graph, IsSubclass
11
12 DRIVER_MODULES = [
13 #    ('csvdriver', True),
14 #    ('hdf5', True),
15 #    ('hemingclamp', True),
16 #    ('jpk', True),
17 #    ('mcs', True),
18 #    ('mfp1dexport', True),
19 #    ('mfp3d', True),
20 #    ('picoforce', True),
21 #    ('picoforcealt', True),
22     ('tutorial', True),
23 ]
24 """List of driver modules and whether they should be included by
25 default.  TODO: autodiscovery
26 """
27
28 DRIVER_SETTING_SECTION = 'drivers'
29 """Name of the config section which controls driver selection.
30 """
31
32
33 class Driver(object):
34     """Base class for file format drivers.
35     
36     :attr:`name` identifies your driver, and should match the module
37     name.
38     """
39     def __init__(self, name):
40         self.name = name
41         self.setting_section = '%s driver' % self.name
42
43     def dependencies(self):
44         """Return a list of :class:`Driver`\s we require."""
45         return []
46
47     def default_settings(self):
48         """Return a list of :class:`hooke.config.Setting`\s for any
49         configurable driver settings.
50
51         The suggested section setting is::
52
53             Setting(section=self.setting_section, help=self.__doc__)
54         """
55         return []
56
57     def is_me(self, path):
58         """Read the file and return True if the filetype can be
59         managed by the driver.  Otherwise return False.
60         """
61         return False
62
63     def read(self, path):
64         """Read data from `path` and return a
65         (:class:`hooke.curve.Data`, `info`) tuple.
66
67         The `info` :class:`dict` must contain values for the keys:
68         'filetype' and 'experiment'.  See :class:`hooke.curve.Curve`
69         for details.
70         """
71         raise NotImplementedError
72
73 # Construct driver dependency graph and load default drivers.
74
75 DRIVER_GRAPH = construct_graph(
76     this_modname=__name__,
77     submodnames=[name for name,include in DRIVER_MODULES],
78     class_selector=IsSubclass(Driver, blacklist=[Driver]))
79 """Topologically sorted list of all possible :class:`Driver`\s.
80 """
81
82 def default_settings():
83     settings = [Setting(DRIVER_SETTING_SECTION,
84                         help='Enable/disable default drivers.')]
85     for dnode in DRIVER_GRAPH:
86         driver = dnode.data
87         default_include = [di for mod_name,di in DRIVER_MODULES
88                            if mod_name == driver.name][0]
89         help = driver.__doc__.split('\n', 1)[0]
90         settings.append(Setting(
91                 section=DRIVER_SETTING_SECTION,
92                 option=driver.name,
93                 value=str(default_include),
94                 help=help,
95                 ))
96     for dnode in DRIVER_GRAPH:
97         driver = dnode.data
98         settings.extend(driver.default_settings())
99     return settings