c44ba03a946971bd17ee209189e3024204f6faec
[hooke.git] / hooke / plugin / debug.py
1 """The `debug` module provides :class:`DebugPlugin` and associated
2 :class:`hooke.command.Command`\s which provide useful debugging
3 information.
4 """
5
6 import platform
7 import sys
8
9 from .. import version
10 from ..command import Command, Argument
11 from ..plugin import Builtin
12
13
14 class DebugPlugin (Builtin):
15     def __init__(self):
16         super(DebugPlugin, self).__init__(name='debug')
17
18     def commands(self):
19         return [VersionCommand(), DebugCommand()]
20
21
22 class VersionCommand (Command):
23     """Get the Hooke version, as well as versions for important Python
24     packages.  Useful for debugging.
25     """
26     def __init__(self):
27         super(VersionCommand, self).__init__(
28             name='version', help=self.__doc__)
29
30     def _run(self, hooke, inqueue, outqueue, params):
31         lines = [
32             'Hooke ' + version(),
33             '----',
34             'Python ' + sys.version]
35         for name,module in sorted(sys.modules.items()):
36             if name == 'hooke':
37                 continue
38             v = getattr(module, '__version__', None)
39             if v != None:
40                 lines.append('%s %s' % (name, v))
41         lines.extend([
42                 '----',
43                 'Platform: %s' % ' '.join(platform.uname()),
44                 'User interface: %s' % hooke.ui.name,
45                 '----',
46                 'Loaded plugins:'])
47         lines.extend([p.name for p in hooke.plugins])
48         lines.extend([
49                 '----',
50                 'Loaded drivers:'])
51         lines.extend([d.name for d in hooke.drivers])
52         outqueue.put('\n'.join(lines))
53
54
55 class DebugCommand (Command):
56     """Get Hooke attributes.  Useful for debugging.
57     """
58     def __init__(self):
59         super(DebugCommand, self).__init__(
60             name='debug',
61             arguments=[
62                 Argument(name='attribute', help="""
63 Hooke attribute to print.
64 """.strip()),
65                 ],
66             help=self.__doc__)
67
68     def _run(self, hooke, inqueue, outqueue, params):
69         if params['attribute'] == None:
70             outqueue.put(hooke)
71         else:
72             outqueue.put(getattr(hooke, params['attribute']))