3 """The `config` module provides :class:`ConfigPlugin` and several
4 associated :class:`hooke.command.Command`\s for handling
5 :mod:`hooke.config` classes.
8 from StringIO import StringIO
10 from ..command import Command, Argument, Failure
11 from ..interaction import ReloadUserInterfaceConfig
12 from ..plugin import Builtin
15 class ConfigPlugin (Builtin):
17 super(ConfigPlugin, self).__init__(name='config')
20 return [GetCommand(), SetCommand(), PrintCommand()]
23 # Define common or complicated arguments
25 SectionArgument = Argument(
26 name='section', type='string', optional=False,
28 Configuration section to act on.
31 OptionArgument = Argument(
32 name='option', type='string', optional=False,
34 Configuration option to act on.
40 class GetCommand (Command):
41 """Get the current value of a configuration option.
44 super(GetCommand, self).__init__(
46 arguments=[SectionArgument, OptionArgument],
49 def _run(self, hooke, inqueue, outqueue, params):
50 outqueue.put(hooke.config.get(params['section'], params['option']))
52 class SetCommand (Command):
53 """Set the current value of a configuration option.
55 Currently many config options are read at startup time, and config
56 dicts are passed out to their target classes. This means that changes
57 to the central :attr:`hooke.hooke.Hooke.config` location *will not* be
58 noticed by the target classes unless the configuration is reloaded.
59 This reloading may cause problems in poorly written UIs.
62 super(SetCommand, self).__init__(
65 SectionArgument, OptionArgument,
67 name='value', type='string', optional=False,
68 help='Value to set.'),
72 def _run(self, hooke, inqueue, outqueue, params):
73 hooke.config.set(params['section'], params['option'], params['value'])
77 # notify UI to update config
78 outqueue.put(ReloadUserInterfaceConfig(hooke.config))
80 class PrintCommand (Command):
81 """Get the current value of a configuration option.
84 super(PrintCommand, self).__init__(
85 name='print config', help=self.__doc__)
87 def _run(self, hooke, inqueue, outqueue, params):
89 hooke.config.write(out)
90 outqueue.put(out.getvalue())