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
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 # Public License for more details.
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/>.
19 """The `config` module provides :class:`ConfigPlugin` and several
20 associated :class:`hooke.command.Command`\s for handling
21 :mod:`hooke.config` classes.
25 from StringIO import StringIO
27 from ..command import Command, Argument, Failure
28 from ..interaction import ReloadUserInterfaceConfig
32 class ConfigPlugin (Builtin):
34 super(ConfigPlugin, self).__init__(name='config')
35 self._commands = [GetCommand(self), SetCommand(self),
36 PrintCommand(self), SaveCommand(self),]
39 # Define common or complicated arguments
41 SectionArgument = Argument(
42 name='section', type='string', optional=False,
44 Configuration section to act on.
47 OptionArgument = Argument(
48 name='option', type='string', optional=False,
50 Configuration option to act on.
56 class GetCommand (Command):
57 """Get the current value of a configuration option.
59 def __init__(self, plugin):
60 super(GetCommand, self).__init__(
62 arguments=[SectionArgument, OptionArgument],
63 help=self.__doc__, plugin=plugin)
65 def _run(self, hooke, inqueue, outqueue, params):
66 outqueue.put(hooke.config.get(params['section'], params['option']))
68 class SetCommand (Command):
69 """Set the current value of a configuration option.
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.
77 def __init__(self, plugin):
78 super(SetCommand, self).__init__(
81 SectionArgument, OptionArgument,
83 name='value', type='string', optional=False,
84 help='Value to set.'),
86 help=self.__doc__, plugin=plugin)
88 def _run(self, hooke, inqueue, outqueue, params):
89 hooke.config.set(params['section'], params['option'], params['value'])
92 hooke.configure_plugins()
93 hooke.configure_drivers()
94 hooke.configure_ui() # for post-HookeRunner Hooke return.
95 # notify UI to update config
96 outqueue.put(ReloadUserInterfaceConfig(hooke.config))
98 class PrintCommand (Command):
99 """Get the current configuration file text.
101 def __init__(self, plugin):
102 super(PrintCommand, self).__init__(
103 name='print config', help=self.__doc__, plugin=plugin)
105 def _run(self, hooke, inqueue, outqueue, params):
107 hooke.config.write(out)
108 outqueue.put(out.getvalue())
111 class SaveCommand (Command):
112 """Save the current configuration options.
114 def __init__(self, plugin):
115 super(SaveCommand, self).__init__(
118 Argument(name='output', type='file',
120 File name for the output configuration. Defaults to overwriting the
121 most local loaded config file.
124 help=self.__doc__, plugin=plugin)
126 def _run(self, hooke, inqueue, outqueue, params):
129 if params['output'] != None:
130 f = open(os.path.expanduser(params['output']), 'w')
131 hooke.config.write(fp=f)