1 # Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
5 # Hooke is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with Hooke. If not, see <http://www.gnu.org/licenses/>.
18 """The `config` module provides :class:`ConfigPlugin` and several
19 associated :class:`hooke.command.Command`\s for handling
20 :mod:`hooke.config` classes.
24 from StringIO import StringIO
26 from ..command import Command, Argument, Failure
27 from ..interaction import ReloadUserInterfaceConfig
31 class ConfigPlugin (Builtin):
33 super(ConfigPlugin, self).__init__(name='config')
34 self._commands = [GetCommand(self), SetCommand(self),
35 PrintCommand(self), SaveCommand(self),]
38 # Define common or complicated arguments
40 SectionArgument = Argument(
41 name='section', type='string', optional=False,
43 Configuration section to act on.
46 OptionArgument = Argument(
47 name='option', type='string', optional=False,
49 Configuration option to act on.
55 class GetCommand (Command):
56 """Get the current value of a configuration option.
58 def __init__(self, plugin):
59 super(GetCommand, self).__init__(
61 arguments=[SectionArgument, OptionArgument],
62 help=self.__doc__, plugin=plugin)
64 def _run(self, hooke, inqueue, outqueue, params):
65 outqueue.put(hooke.config.get(params['section'], params['option']))
67 class SetCommand (Command):
68 """Set the current value of a configuration option.
70 Currently many config options are read at startup time, and config
71 dicts are passed out to their target classes. This means that changes
72 to the central :attr:`hooke.hooke.Hooke.config` location *will not* be
73 noticed by the target classes unless the configuration is reloaded.
74 This reloading may cause problems in poorly written UIs.
76 def __init__(self, plugin):
77 super(SetCommand, self).__init__(
80 SectionArgument, OptionArgument,
82 name='value', type='string', optional=False,
83 help='Value to set.'),
85 help=self.__doc__, plugin=plugin)
87 def _run(self, hooke, inqueue, outqueue, params):
88 hooke.config.set(params['section'], params['option'], params['value'])
91 hooke.configure_plugins()
92 hooke.configure_drivers()
93 hooke.configure_ui() # for post-HookeRunner Hooke return.
94 # notify UI to update config
95 outqueue.put(ReloadUserInterfaceConfig(hooke.config))
97 class PrintCommand (Command):
98 """Get the current configuration file text.
100 def __init__(self, plugin):
101 super(PrintCommand, self).__init__(
102 name='print config', help=self.__doc__, plugin=plugin)
104 def _run(self, hooke, inqueue, outqueue, params):
106 hooke.config.write(out)
107 outqueue.put(out.getvalue())
110 class SaveCommand (Command):
111 """Save the current configuration options.
113 def __init__(self, plugin):
114 super(SaveCommand, self).__init__(
117 Argument(name='output', type='file',
119 File name for the output configuration. Defaults to overwriting the
120 most local loaded config file.
123 help=self.__doc__, plugin=plugin)
125 def _run(self, hooke, inqueue, outqueue, params):
128 if params['output'] != None:
129 f = open(os.path.expanduser(params['output']), 'w')
130 hooke.config.write(fp=f)