Added 'save config'. Adjusted test/tutorial.py (and test/ in general) passes.
[hooke.git] / hooke / plugin / config.py
index a4af140fd4afde43241ece5ebf00d038c0c15247..ea1b0397f9d657ab84008cfab5e5ef87062612d5 100644 (file)
@@ -1,4 +1,20 @@
-# Copyright
+# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
+#
+# This file is part of Hooke.
+#
+# Hooke is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# Hooke is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
+# Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with Hooke.  If not, see
+# <http://www.gnu.org/licenses/>.
 
 """The `config` module provides :class:`ConfigPlugin` and several
 associated :class:`hooke.command.Command`\s for handling
 
 """The `config` module provides :class:`ConfigPlugin` and several
 associated :class:`hooke.command.Command`\s for handling
@@ -9,15 +25,14 @@ from StringIO import StringIO
 
 from ..command import Command, Argument, Failure
 from ..interaction import ReloadUserInterfaceConfig
 
 from ..command import Command, Argument, Failure
 from ..interaction import ReloadUserInterfaceConfig
-from ..plugin import Builtin
+from . import Builtin
 
 
 class ConfigPlugin (Builtin):
     def __init__(self):
         super(ConfigPlugin, self).__init__(name='config')
 
 
 class ConfigPlugin (Builtin):
     def __init__(self):
         super(ConfigPlugin, self).__init__(name='config')
-
-    def commands(self):
-        return [GetCommand(), SetCommand(), PrintCommand()]
+        self._commands = [GetCommand(self), SetCommand(self),
+                          PrintCommand(self), SaveCommand(self),]
 
 
 # Define common or complicated arguments
 
 
 # Define common or complicated arguments
@@ -40,11 +55,11 @@ Configuration option to act on.
 class GetCommand (Command):
     """Get the current value of a configuration option.
     """
 class GetCommand (Command):
     """Get the current value of a configuration option.
     """
-    def __init__(self):
+    def __init__(self, plugin):
         super(GetCommand, self).__init__(
             name='get config',
             arguments=[SectionArgument, OptionArgument],
         super(GetCommand, self).__init__(
             name='get config',
             arguments=[SectionArgument, OptionArgument],
-            help=self.__doc__)
+            help=self.__doc__, plugin=plugin)
 
     def _run(self, hooke, inqueue, outqueue, params):
        outqueue.put(hooke.config.get(params['section'], params['option']))
 
     def _run(self, hooke, inqueue, outqueue, params):
        outqueue.put(hooke.config.get(params['section'], params['option']))
@@ -58,7 +73,7 @@ class SetCommand (Command):
     noticed by the target classes unless the configuration is reloaded.
     This reloading may cause problems in poorly written UIs.
     """
     noticed by the target classes unless the configuration is reloaded.
     This reloading may cause problems in poorly written UIs.
     """
-    def __init__(self):
+    def __init__(self, plugin):
         super(SetCommand, self).__init__(
             name='set config',
             arguments=[
         super(SetCommand, self).__init__(
             name='set config',
             arguments=[
@@ -67,24 +82,52 @@ class SetCommand (Command):
                     name='value', type='string', optional=False,
                     help='Value to set.'),
                 ],
                     name='value', type='string', optional=False,
                     help='Value to set.'),
                 ],
-            help=self.__doc__)
+            help=self.__doc__, plugin=plugin)
 
     def _run(self, hooke, inqueue, outqueue, params):
        hooke.config.set(params['section'], params['option'], params['value'])
         # push config changes
 
     def _run(self, hooke, inqueue, outqueue, params):
        hooke.config.set(params['section'], params['option'], params['value'])
         # push config changes
-        hooke.load_plugins()
-        hooke.load_drivers()
+        hooke.load_log()
+        hooke.configure_plugins()
+        hooke.configure_drivers()
+        hooke.configure_ui()  # for post-HookeRunner Hooke return.
         # notify UI to update config
         outqueue.put(ReloadUserInterfaceConfig(hooke.config))
 
 class PrintCommand (Command):
         # notify UI to update config
         outqueue.put(ReloadUserInterfaceConfig(hooke.config))
 
 class PrintCommand (Command):
-    """Get the current value of a configuration option.
+    """Get the current configuration file text.
     """
     """
-    def __init__(self):
+    def __init__(self, plugin):
         super(PrintCommand, self).__init__(
         super(PrintCommand, self).__init__(
-            name='print config', help=self.__doc__)
+            name='print config', help=self.__doc__, plugin=plugin)
 
     def _run(self, hooke, inqueue, outqueue, params):
         out = StringIO()
         hooke.config.write(out)
         outqueue.put(out.getvalue())
 
     def _run(self, hooke, inqueue, outqueue, params):
         out = StringIO()
         hooke.config.write(out)
         outqueue.put(out.getvalue())
+
+
+class SaveCommand (Command):
+    """Save the current configuration options.
+    """
+    def __init__(self, plugin):
+        super(SaveCommand, self).__init__(
+            name='save config',
+            arguments=[
+                Argument(name='output', type='file',
+                         help="""
+File name for the output configuration.  Defaults to overwriting the
+most local loaded config file.
+""".strip()),
+                ],
+            help=self.__doc__, plugin=plugin)
+
+    def _run(self, hooke, inqueue, outqueue, params):
+        f = None
+        try:
+            if params['output'] != None:
+                f = open(params['output'], 'w')
+            hooke.config.write(fp=f)
+        finally:
+            if f != None:
+                f.close()