Moved hooke.playlist -> hooke.plugin.playlist and added hooke.plugin.Builtin.
[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 ..util.graph import Node, Graph
11
12
13 DRIVER_MODULES = [
14 #    ('csvdriver', True),
15 #    ('hdf5', True),
16 #    ('hemingclamp', True),
17 #    ('jpk', True),
18 #    ('mcs', True),
19 #    ('mfp1dexport', True),
20 #    ('mfp3d', True),
21 #    ('picoforce', True),
22 #    ('picoforcealt', True),
23     ('tutorial', True),
24 ]
25 """List of driver modules and whether they should be included by
26 default.  TODO: autodiscovery
27 """
28
29
30 class Driver(object):
31     """Base class for file format drivers.
32     
33     :attr:`name` identifies your driver, and should match the module
34     name.
35     """
36     def __init__(self, name):
37         self.name = name
38         self.setting_section = '%s driver' % self.name
39
40     def dependencies(self):
41         """Return a list of :class:`Driver`\s we require."""
42         return []
43
44     def default_settings(self):
45         """Return a list of :class:`hooke.config.Setting`\s for any
46         configurable driver settings.
47
48         The suggested section setting is::
49
50             Setting(section=self.setting_section, help=self.__doc__)
51         """
52         return []
53
54     def is_me(self, path):
55         """Read the file and return True if the filetype can be
56         managed by the driver.  Otherwise return False.
57         """
58         return False
59
60     def read(self, path):
61         """Read data from `path` and return a
62         (:class:`hooke.curve.Data`, `info`) tuple.
63
64         The `info` :class:`dict` must contain values for the keys:
65         'filetype' and 'experiment'.  See :class:`hooke.curve.Curve`
66         for details.
67         """
68         raise NotImplementedError
69
70 # Construct driver dependency graph and load default drivers.
71
72 DRIVERS = {}
73 """(name, instance) :class:`dict` of all possible :class:`Driver`\s.
74 """
75
76 for driver_modname,default_include in DRIVER_MODULES:
77     assert len([mod_name for mod_name,di in DRIVER_MODULES]) == 1, \
78         'Multiple %s entries in DRIVER_MODULES' % mod_name
79     this_mod = __import__(__name__, fromlist=[driver_modname])
80     driver_mod = getattr(this_mod, driver_modname)
81     for objname in dir(driver_mod):
82         obj = getattr(driver_mod, objname)
83         try:
84             subclass = issubclass(obj, Driver)
85         except TypeError:
86             continue
87         if subclass == True and obj != Driver:
88             d = obj()
89             if d.name != driver_modname:
90                 raise Exception('Driver name %s does not match module name %s'
91                                 % (d.name, driver_modname))
92             DRIVERS[d.name] = d
93
94 DRIVER_GRAPH = Graph([Node([DRIVERS[name] for name in d.dependencies()],
95                            data=d)
96                       for d in DRIVERS.values()])
97 DRIVER_GRAPH.topological_sort()
98
99
100 def default_settings():
101     settings = [Setting(
102             'drivers', help='Enable/disable default drivers.')]
103     for dnode in DRIVER_GRAPH:
104         driver = dnode.data
105         default_include = [di for mod_name,di in DRIVER_MODULES
106                            if mod_name == driver.name][0]
107         help = driver.__doc__.split('\n', 1)[0]
108         settings.append(Setting(
109                 section='drivers',
110                 option=driver.name,
111                 value=str(default_include),
112                 help=help,
113                 ))
114     for dnode in DRIVER_GRAPH:
115         driver = dnode.data
116         settings.extend(driver.default_settings())
117     return settings