Merged my unitary FFT wrappers (FFT_tools) as hooke.util.fft.
[hooke.git] / hooke / plugin / config.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
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.
9 #
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.
14 #
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/>.
18
19 """The `config` module provides :class:`ConfigPlugin` and several
20 associated :class:`hooke.command.Command`\s for handling
21 :mod:`hooke.config` classes.
22 """
23
24 from StringIO import StringIO
25
26 from ..command import Command, Argument, Failure
27 from ..interaction import ReloadUserInterfaceConfig
28 from ..plugin import Builtin
29
30
31 class ConfigPlugin (Builtin):
32     def __init__(self):
33         super(ConfigPlugin, self).__init__(name='config')
34
35     def commands(self):
36         return [GetCommand(), SetCommand(), PrintCommand()]
37
38
39 # Define common or complicated arguments
40
41 SectionArgument = Argument(
42     name='section', type='string', optional=False,
43     help="""
44 Configuration section to act on.
45 """.strip())
46
47 OptionArgument = Argument(
48     name='option', type='string', optional=False,
49     help="""
50 Configuration option to act on.
51 """.strip())
52
53
54 # Define commands
55
56 class GetCommand (Command):
57     """Get the current value of a configuration option.
58     """
59     def __init__(self):
60         super(GetCommand, self).__init__(
61             name='get config',
62             arguments=[SectionArgument, OptionArgument],
63             help=self.__doc__)
64
65     def _run(self, hooke, inqueue, outqueue, params):
66         outqueue.put(hooke.config.get(params['section'], params['option']))
67
68 class SetCommand (Command):
69     """Set the current value of a configuration option.
70
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.
76     """
77     def __init__(self):
78         super(SetCommand, self).__init__(
79             name='set config',
80             arguments=[
81                 SectionArgument, OptionArgument,
82                 Argument(
83                     name='value', type='string', optional=False,
84                     help='Value to set.'),
85                 ],
86             help=self.__doc__)
87
88     def _run(self, hooke, inqueue, outqueue, params):
89         hooke.config.set(params['section'], params['option'], params['value'])
90         # push config changes
91         hooke.load_plugins()
92         hooke.load_drivers()
93         # notify UI to update config
94         outqueue.put(ReloadUserInterfaceConfig(hooke.config))
95
96 class PrintCommand (Command):
97     """Get the current value of a configuration option.
98     """
99     def __init__(self):
100         super(PrintCommand, self).__init__(
101             name='print config', help=self.__doc__)
102
103     def _run(self, hooke, inqueue, outqueue, params):
104         out = StringIO()
105         hooke.config.write(out)
106         outqueue.put(out.getvalue())