594ffa9d55acaa58b87dbbeb4f4c3dc62bdb4e2a
[hooke.git] / hooke / plugin / config.py
1 """The `config` module provides :class:`ConfigPlugin` and several
2 associated :class:`hooke.command.Command`\s for handling
3 :mod:`hooke.config` classes.
4 """
5
6 from StringIO import StringIO
7
8 from ..command import Command, Argument, Failure
9 from ..interaction import ReloadUserInterfaceConfig
10 from ..plugin import Builtin
11
12
13 class ConfigPlugin (Builtin):
14     def __init__(self):
15         super(ConfigPlugin, self).__init__(name='config')
16
17     def commands(self):
18         return [GetCommand(), SetCommand(), PrintCommand()]
19
20
21 # Define common or complicated arguments
22
23 SectionArgument = Argument(
24     name='section', type='string', optional=False,
25     help="""
26 Configuration section to act on.
27 """.strip())
28
29 OptionArgument = Argument(
30     name='option', type='string', optional=False,
31     help="""
32 Configuration option to act on.
33 """.strip())
34
35
36 # Define commands
37
38 class GetCommand (Command):
39     """Get the current value of a configuration option.
40     """
41     def __init__(self):
42         super(GetCommand, self).__init__(
43             name='get config',
44             arguments=[SectionArgument, OptionArgument],
45             help=self.__doc__)
46
47     def _run(self, hooke, inqueue, outqueue, params):
48         outqueue.put(hooke.config.get(params['section'], params['option']))
49
50 class SetCommand (Command):
51     """Set the current value of a configuration option.
52
53     Currently many config options are read at startup time, and config
54     dicts are passed out to their target classes.  This means that changes
55     to the central :attr:`hooke.hooke.Hooke.config` location *will not* be
56     noticed by the target classes unless the configuration is reloaded.
57     This reloading may cause problems in poorly written UIs.
58     """
59     def __init__(self):
60         super(SetCommand, self).__init__(
61             name='set config',
62             arguments=[
63                 SectionArgument, OptionArgument,
64                 Argument(
65                     name='value', type='string', optional=False,
66                     help='Value to set.'),
67                 ],
68             help=self.__doc__)
69
70     def _run(self, hooke, inqueue, outqueue, params):
71         hooke.config.set(params['section'], params['option'], params['value'])
72         # push config changes
73         hooke.load_plugins()
74         hooke.load_drivers()
75         # notify UI to update config
76         outqueue.put(ReloadUserInterfaceConfig(hooke.config))
77
78 class PrintCommand (Command):
79     """Get the current value of a configuration option.
80     """
81     def __init__(self):
82         super(PrintCommand, self).__init__(
83             name='print config', help=self.__doc__)
84
85     def _run(self, hooke, inqueue, outqueue, params):
86         out = StringIO()
87         hooke.config.write(out)
88         outqueue.put(out.getvalue())