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