a4af140fd4afde43241ece5ebf00d038c0c15247
[hooke.git] / hooke / plugin / config.py
1 # Copyright
2
3 """The `config` module provides :class:`ConfigPlugin` and several
4 associated :class:`hooke.command.Command`\s for handling
5 :mod:`hooke.config` classes.
6 """
7
8 from StringIO import StringIO
9
10 from ..command import Command, Argument, Failure
11 from ..interaction import ReloadUserInterfaceConfig
12 from ..plugin import Builtin
13
14
15 class ConfigPlugin (Builtin):
16     def __init__(self):
17         super(ConfigPlugin, self).__init__(name='config')
18
19     def commands(self):
20         return [GetCommand(), SetCommand(), PrintCommand()]
21
22
23 # Define common or complicated arguments
24
25 SectionArgument = Argument(
26     name='section', type='string', optional=False,
27     help="""
28 Configuration section to act on.
29 """.strip())
30
31 OptionArgument = Argument(
32     name='option', type='string', optional=False,
33     help="""
34 Configuration option to act on.
35 """.strip())
36
37
38 # Define commands
39
40 class GetCommand (Command):
41     """Get the current value of a configuration option.
42     """
43     def __init__(self):
44         super(GetCommand, self).__init__(
45             name='get config',
46             arguments=[SectionArgument, OptionArgument],
47             help=self.__doc__)
48
49     def _run(self, hooke, inqueue, outqueue, params):
50         outqueue.put(hooke.config.get(params['section'], params['option']))
51
52 class SetCommand (Command):
53     """Set the current value of a configuration option.
54
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.
60     """
61     def __init__(self):
62         super(SetCommand, self).__init__(
63             name='set config',
64             arguments=[
65                 SectionArgument, OptionArgument,
66                 Argument(
67                     name='value', type='string', optional=False,
68                     help='Value to set.'),
69                 ],
70             help=self.__doc__)
71
72     def _run(self, hooke, inqueue, outqueue, params):
73         hooke.config.set(params['section'], params['option'], params['value'])
74         # push config changes
75         hooke.load_plugins()
76         hooke.load_drivers()
77         # notify UI to update config
78         outqueue.put(ReloadUserInterfaceConfig(hooke.config))
79
80 class PrintCommand (Command):
81     """Get the current value of a configuration option.
82     """
83     def __init__(self):
84         super(PrintCommand, self).__init__(
85             name='print config', help=self.__doc__)
86
87     def _run(self, hooke, inqueue, outqueue, params):
88         out = StringIO()
89         hooke.config.write(out)
90         outqueue.put(out.getvalue())