Updated 'Configuring Hooke' section of tutorial.txt
[hooke.git] / hooke / plugin / config.py
1 # Copyright (C) 2010 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 `config` module provides :class:`ConfigPlugin` and several
20 associated :class:`hooke.command.Command`\s for handling
21 :mod:`hooke.config` classes.
22 """
23
24 from StringIO import StringIO
25
26 from ..command import Command, Argument, Failure
27 from ..interaction import ReloadUserInterfaceConfig
28 from ..plugin import Builtin
29
30
31 class ConfigPlugin (Builtin):
32     def __init__(self):
33         super(ConfigPlugin, self).__init__(name='config')
34         self._commands = [GetCommand(self), SetCommand(self),
35                           PrintCommand(self)]
36
37
38 # Define common or complicated arguments
39
40 SectionArgument = Argument(
41     name='section', type='string', optional=False,
42     help="""
43 Configuration section to act on.
44 """.strip())
45
46 OptionArgument = Argument(
47     name='option', type='string', optional=False,
48     help="""
49 Configuration option to act on.
50 """.strip())
51
52
53 # Define commands
54
55 class GetCommand (Command):
56     """Get the current value of a configuration option.
57     """
58     def __init__(self, plugin):
59         super(GetCommand, self).__init__(
60             name='get config',
61             arguments=[SectionArgument, OptionArgument],
62             help=self.__doc__, plugin=plugin)
63
64     def _run(self, hooke, inqueue, outqueue, params):
65         outqueue.put(hooke.config.get(params['section'], params['option']))
66
67 class SetCommand (Command):
68     """Set the current value of a configuration option.
69
70     Currently many config options are read at startup time, and config
71     dicts are passed out to their target classes.  This means that changes
72     to the central :attr:`hooke.hooke.Hooke.config` location *will not* be
73     noticed by the target classes unless the configuration is reloaded.
74     This reloading may cause problems in poorly written UIs.
75     """
76     def __init__(self, plugin):
77         super(SetCommand, self).__init__(
78             name='set config',
79             arguments=[
80                 SectionArgument, OptionArgument,
81                 Argument(
82                     name='value', type='string', optional=False,
83                     help='Value to set.'),
84                 ],
85             help=self.__doc__, plugin=plugin)
86
87     def _run(self, hooke, inqueue, outqueue, params):
88         hooke.config.set(params['section'], params['option'], params['value'])
89         # push config changes
90         hooke.load_log()
91         hooke.load_plugins()
92         hooke.load_drivers()
93         hooke.load_ui()  # for post-HookeRunner Hooke return.
94         # notify UI to update config
95         outqueue.put(ReloadUserInterfaceConfig(hooke.config))
96
97 class PrintCommand (Command):
98     """Get the current value of all configuration options.
99     """
100     def __init__(self, plugin):
101         super(PrintCommand, self).__init__(
102             name='print config', help=self.__doc__, plugin=plugin)
103
104     def _run(self, hooke, inqueue, outqueue, params):
105         out = StringIO()
106         hooke.config.write(out)
107         outqueue.put(out.getvalue())