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