1 """The driver module provides :class:`Driver`\s for identifying and
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.
9 from ..config import Setting
10 from ..util.graph import Node, Graph
14 # ('csvdriver', True),
16 # ('hemingclamp', True),
19 # ('mfp1dexport', True),
21 # ('picoforce', True),
22 # ('picoforcealt', True),
25 """List of driver modules and whether they should be included by
26 default. TODO: autodiscovery
29 class NotRecognized (ValueError):
30 def __init__(self, path):
31 msg = 'Not a recognizable curve format: %s' % self.path
32 ValueError.__init__(self, msg)
36 """Base class for file format drivers.
38 :attr:`name` identifies your driver, and should match the module
41 def __init__(self, name):
44 def dependencies(self):
45 """Return a list of :class:`Driver`\s we require."""
48 def default_settings(self):
49 """Return a list of :class:`hooke.config.Setting`\s for any
50 configurable driver settings.
52 The suggested section setting is::
54 Setting(section='%s driver' % self.name, help=self.__doc__)
58 def is_me(self, path):
59 """Read the file and return True if the filetype can be
60 managed by the driver. Otherwise return False.
65 """Read data from `path` and return a
66 (:class:`hooke.curve.Data`, `info`) tuple.
68 The `info` :class:`dict` must contain values for the keys:
69 'filetype' and 'experiment'. See :class:`hooke.curve.Curve`
72 raise NotImplementedError
74 # Construct driver dependency graph and load default drivers.
77 """(name, instance) :class:`dict` of all possible :class:`Driver`\s.
80 for driver_modname,default_include in DRIVER_MODULES:
81 assert len([mod_name for mod_name,di in DRIVER_MODULES]) == 1, \
82 'Multiple %s entries in DRIVER_MODULES' % mod_name
83 this_mod = __import__(__name__, fromlist=[driver_modname])
84 driver_mod = getattr(this_mod, driver_modname)
85 for objname in dir(driver_mod):
86 obj = getattr(driver_mod, objname)
88 subclass = issubclass(obj, Driver)
91 if subclass == True and obj != Driver:
93 if d.name != driver_modname:
94 raise Exception('Driver name %s does not match module name %s'
95 % (d.name, driver_modname))
98 DRIVER_GRAPH = Graph([Node([DRIVERS[name] for name in d.dependencies()],
100 for d in DRIVERS.values()])
101 DRIVER_GRAPH.topological_sort()
104 def default_settings():
106 'drivers', help='Enable/disable default drivers.')]
107 for dnode in DRIVER_GRAPH:
109 default_include = [di for mod_name,di in DRIVER_MODULES
110 if mod_name == driver.name][0]
111 help = driver.__doc__.split('\n', 1)[0]
112 settings.append(Setting(
115 value=str(default_include),
118 for dnode in DRIVER_GRAPH:
120 settings.extend(driver.default_settings())