1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
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.
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.
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/>.
19 """The `config` module provides :class:`ConfigPlugin` and several
20 associated :class:`hooke.command.Command`\s for handling
21 :mod:`hooke.config` classes.
24 from StringIO import StringIO
26 from ..command import Command, Argument, Failure
27 from ..interaction import ReloadUserInterfaceConfig
28 from ..plugin import Builtin
31 class ConfigPlugin (Builtin):
33 super(ConfigPlugin, self).__init__(name='config')
34 self._commands = [GetCommand(self), SetCommand(self),
38 # Define common or complicated arguments
40 SectionArgument = Argument(
41 name='section', type='string', optional=False,
43 Configuration section to act on.
46 OptionArgument = Argument(
47 name='option', type='string', optional=False,
49 Configuration option to act on.
55 class GetCommand (Command):
56 """Get the current value of a configuration option.
58 def __init__(self, plugin):
59 super(GetCommand, self).__init__(
61 arguments=[SectionArgument, OptionArgument],
62 help=self.__doc__, plugin=plugin)
64 def _run(self, hooke, inqueue, outqueue, params):
65 outqueue.put(hooke.config.get(params['section'], params['option']))
67 class SetCommand (Command):
68 """Set the current value of a configuration option.
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.
76 def __init__(self, plugin):
77 super(SetCommand, self).__init__(
80 SectionArgument, OptionArgument,
82 name='value', type='string', optional=False,
83 help='Value to set.'),
85 help=self.__doc__, plugin=plugin)
87 def _run(self, hooke, inqueue, outqueue, params):
88 hooke.config.set(params['section'], params['option'], params['value'])
93 hooke.load_ui() # for post-HookeRunner Hooke return.
94 # notify UI to update config
95 outqueue.put(ReloadUserInterfaceConfig(hooke.config))
97 class PrintCommand (Command):
98 """Get the current value of a configuration option.
100 def __init__(self, plugin):
101 super(PrintCommand, self).__init__(
102 name='print config', help=self.__doc__, plugin=plugin)
104 def _run(self, hooke, inqueue, outqueue, params):
106 hooke.config.write(out)
107 outqueue.put(out.getvalue())