b40c71aea7118555fab59a93b34fe7fd9da25e0a
[hooke.git] / hooke / plugin / debug.py
1 # Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
17
18 """The `debug` module provides :class:`DebugPlugin` and associated
19 :class:`hooke.command.Command`\s which provide useful debugging
20 information.
21 """
22
23 import platform
24 import sys
25
26 from .. import version
27 from ..command import Command, Argument
28 from . import Builtin
29
30
31 class DebugPlugin (Builtin):
32     def __init__(self):
33         super(DebugPlugin, self).__init__(name='debug')
34         self._commands = [VersionCommand(self), DebugCommand(self)]
35
36
37 class VersionCommand (Command):
38     """Get the Hooke version, as well as versions for important Python
39     packages.  Useful for debugging.
40     """
41     def __init__(self, plugin):
42         super(VersionCommand, self).__init__(
43             name='version', help=self.__doc__, plugin=plugin)
44
45     def _run(self, hooke, inqueue, outqueue, params):
46         lines = [
47             'Hooke ' + version(),
48             '----',
49             'Python ' + sys.version]
50         for name,module in sorted(sys.modules.items()):
51             if name == 'hooke':
52                 continue
53             v = getattr(module, '__version__', None)
54             if v != None:
55                 lines.append('%s %s' % (name, v))
56         lines.extend([
57                 '----',
58                 'Platform: %s' % ' '.join(platform.uname()),
59                 'User interface: %s' % hooke.ui.name,
60                 '----',
61                 'Loaded plugins:'])
62         lines.extend([p.name for p in hooke.plugins])
63         lines.extend([
64                 '----',
65                 'Loaded drivers:'])
66         lines.extend([d.name for d in hooke.drivers])
67         outqueue.put('\n'.join(lines))
68
69
70 class DebugCommand (Command):
71     """Get Hooke attributes.  Useful for debugging.
72     """
73     def __init__(self, plugin):
74         super(DebugCommand, self).__init__(
75             name='debug',
76             arguments=[
77                 Argument(name='attribute', help="""
78 Hooke attribute to print.
79 """.strip()),
80                 ],
81             help=self.__doc__, plugin=plugin)
82
83     def _run(self, hooke, inqueue, outqueue, params):
84         if params['attribute'] == None:
85             outqueue.put(hooke)
86         else:
87             outqueue.put(getattr(hooke, params['attribute']))