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