Ran update_copyright.py.
[hooke.git] / hooke / plugin / config.py
1 # Copyright (C) 2010-2011 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 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.
9 #
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.
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 import os.path
25 from StringIO import StringIO
26
27 from ..command import Command, Argument, Failure
28 from ..interaction import ReloadUserInterfaceConfig
29 from . import Builtin
30
31
32 class ConfigPlugin (Builtin):
33     def __init__(self):
34         super(ConfigPlugin, self).__init__(name='config')
35         self._commands = [GetCommand(self), SetCommand(self),
36                           PrintCommand(self), SaveCommand(self),]
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, plugin):
60         super(GetCommand, self).__init__(
61             name='get config',
62             arguments=[SectionArgument, OptionArgument],
63             help=self.__doc__, plugin=plugin)
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, plugin):
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__, plugin=plugin)
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_log()
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))
97
98 class PrintCommand (Command):
99     """Get the current configuration file text.
100     """
101     def __init__(self, plugin):
102         super(PrintCommand, self).__init__(
103             name='print config', help=self.__doc__, plugin=plugin)
104
105     def _run(self, hooke, inqueue, outqueue, params):
106         out = StringIO()
107         hooke.config.write(out)
108         outqueue.put(out.getvalue())
109
110
111 class SaveCommand (Command):
112     """Save the current configuration options.
113     """
114     def __init__(self, plugin):
115         super(SaveCommand, self).__init__(
116             name='save config',
117             arguments=[
118                 Argument(name='output', type='file',
119                          help="""
120 File name for the output configuration.  Defaults to overwriting the
121 most local loaded config file.
122 """.strip()),
123                 ],
124             help=self.__doc__, plugin=plugin)
125
126     def _run(self, hooke, inqueue, outqueue, params):
127         f = None
128         try:
129             if params['output'] != None:
130                 f = open(os.path.expanduser(params['output']), 'w')
131             hooke.config.write(fp=f)
132         finally:
133             if f != None:
134                 f.close()