Moved config handling commands from hooke_cli to hooke.plugin.config.
[hooke.git] / hooke / plugin / config.py
diff --git a/hooke/plugin/config.py b/hooke/plugin/config.py
new file mode 100644 (file)
index 0000000..594ffa9
--- /dev/null
@@ -0,0 +1,88 @@
+"""The `config` module provides :class:`ConfigPlugin` and several
+associated :class:`hooke.command.Command`\s for handling
+:mod:`hooke.config` classes.
+"""
+
+from StringIO import StringIO
+
+from ..command import Command, Argument, Failure
+from ..interaction import ReloadUserInterfaceConfig
+from ..plugin import Builtin
+
+
+class ConfigPlugin (Builtin):
+    def __init__(self):
+        super(ConfigPlugin, self).__init__(name='config')
+
+    def commands(self):
+        return [GetCommand(), SetCommand(), PrintCommand()]
+
+
+# Define common or complicated arguments
+
+SectionArgument = Argument(
+    name='section', type='string', optional=False,
+    help="""
+Configuration section to act on.
+""".strip())
+
+OptionArgument = Argument(
+    name='option', type='string', optional=False,
+    help="""
+Configuration option to act on.
+""".strip())
+
+
+# Define commands
+
+class GetCommand (Command):
+    """Get the current value of a configuration option.
+    """
+    def __init__(self):
+        super(GetCommand, self).__init__(
+            name='get config',
+            arguments=[SectionArgument, OptionArgument],
+            help=self.__doc__)
+
+    def _run(self, hooke, inqueue, outqueue, params):
+       outqueue.put(hooke.config.get(params['section'], params['option']))
+
+class SetCommand (Command):
+    """Set the current value of a configuration option.
+
+    Currently many config options are read at startup time, and config
+    dicts are passed out to their target classes.  This means that changes
+    to the central :attr:`hooke.hooke.Hooke.config` location *will not* be
+    noticed by the target classes unless the configuration is reloaded.
+    This reloading may cause problems in poorly written UIs.
+    """
+    def __init__(self):
+        super(SetCommand, self).__init__(
+            name='set config',
+            arguments=[
+                SectionArgument, OptionArgument,
+                Argument(
+                    name='value', type='string', optional=False,
+                    help='Value to set.'),
+                ],
+            help=self.__doc__)
+
+    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()
+        # notify UI to update config
+        outqueue.put(ReloadUserInterfaceConfig(hooke.config))
+
+class PrintCommand (Command):
+    """Get the current value of a configuration option.
+    """
+    def __init__(self):
+        super(PrintCommand, self).__init__(
+            name='print config', help=self.__doc__)
+
+    def _run(self, hooke, inqueue, outqueue, params):
+        out = StringIO()
+        hooke.config.write(out)
+        outqueue.put(out.getvalue())