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
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General 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')
36 return [GetCommand(), SetCommand(), PrintCommand()]
39 # Define common or complicated arguments
41 SectionArgument = Argument(
42 name='section', type='string', optional=False,
44 Configuration section to act on.
47 OptionArgument = Argument(
48 name='option', type='string', optional=False,
50 Configuration option to act on.
56 class GetCommand (Command):
57 """Get the current value of a configuration option.
60 super(GetCommand, self).__init__(
62 arguments=[SectionArgument, OptionArgument],
65 def _run(self, hooke, inqueue, outqueue, params):
66 outqueue.put(hooke.config.get(params['section'], params['option']))
68 class SetCommand (Command):
69 """Set the current value of a configuration option.
71 Currently many config options are read at startup time, and config
72 dicts are passed out to their target classes. This means that changes
73 to the central :attr:`hooke.hooke.Hooke.config` location *will not* be
74 noticed by the target classes unless the configuration is reloaded.
75 This reloading may cause problems in poorly written UIs.
78 super(SetCommand, self).__init__(
81 SectionArgument, OptionArgument,
83 name='value', type='string', optional=False,
84 help='Value to set.'),
88 def _run(self, hooke, inqueue, outqueue, params):
89 hooke.config.set(params['section'], params['option'], params['value'])
93 # notify UI to update config
94 outqueue.put(ReloadUserInterfaceConfig(hooke.config))
96 class PrintCommand (Command):
97 """Get the current value of a configuration option.
100 super(PrintCommand, self).__init__(
101 name='print config', help=self.__doc__)
103 def _run(self, hooke, inqueue, outqueue, params):
105 hooke.config.write(out)
106 outqueue.put(out.getvalue())