Replace .config rather than reconstructing plugins, drivers, and UIs.
[hooke.git] / hooke / command.py
index 21e09b8060b51e16c2bd992b91452df4b74a45b5..c694bff28c0d7b2ea8625672017fb186074c1ba5 100644 (file)
@@ -1,5 +1,28 @@
+# 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 `command` module provides :class:`Command`\s and
 :class:`Argument`\s for defining commands.
+
+It also provides :class:`CommandExit` and subclasses for communicating
+command completion information between
+:class:`hooke.engine.CommandEngine`\s and
+:class:`hooke.ui.UserInterface`\s.
 """
 
 import Queue as queue
@@ -57,37 +80,14 @@ class UncaughtException (Failure):
             setattr(self, key, value)
         self.args = (self.traceback + str(self.exception),)
 
-class Interaction (object):
-    """Mid-command inter-process interaction.
-    """
-    pass
-
-class Request (Interaction):
-    """Command engine requests for information from the UI.
-    """
-    def __init__(self, msg, default=None):
-        super(Request, self).__init__()
-        self.msg = msg
-        self.default = default
-
-class Response (Interaction):
-    """UI response to a :class:`Request`.
-    """
-    def __init__(self, value):
-        super(Response, self).__init__()
-        self.value = value
-
-class BooleanRequest (Request):
-    pass
-
-class BooleanResponse (Response):
-    pass
 
 class Command (object):
     """One-line command description here.
 
     >>> c = Command(name='test', help='An example Command.')
-    >>> status = c.run(NullQueue(), PrintQueue(), help=True) # doctest: +REPORT_UDIFF
+    >>> hooke = None
+    >>> status = c.run(hooke, NullQueue(), PrintQueue(),
+    ...                help=True) # doctest: +REPORT_UDIFF
     ITEM:
     Command: test
     <BLANKLINE>
@@ -97,18 +97,21 @@ class Command (object):
     <BLANKLINE>
     An example Command.
     ITEM:
-    Success
+    <BLANKLINE>
     """
-    def __init__(self, name, aliases=None, arguments=[], help=''):
+    def __init__(self, name, aliases=None, arguments=[], help='',
+                 plugin=None):
+        # TODO: see_also=[other,command,instances,...]
         self.name = name
         if aliases == None:
             aliases = []
         self.aliases = aliases
         self.arguments = [
             Argument(name='help', type='bool', default=False, count=1,
-                     callback=StoreValue(True), help='Print a help message.'),
+                     help='Print a help message.'),
             ] + arguments
         self._help = help
+        self.plugin = plugin
 
     def run(self, hooke, inqueue=None, outqueue=None, **kwargs):
         """`Normalize inputs and handle <Argument help> before punting
@@ -138,7 +141,7 @@ class Command (object):
         outqueue.put(e)
         return 0
 
-    def _run(self, inqueue, outqueue, params):
+    def _run(self, hooke, inqueue, outqueue, params):
         """This is where the command-specific magic will happen.
         """
         pass
@@ -162,12 +165,14 @@ class Command (object):
                        '\n  '.join(['%s: %s' % (name,value)
                                     for name,value in sorted(settings)])))
             name,value = settings[0]
-            if name != argument.name:
-                params.remove(name)
+            if num_provided == 0:
                 params[argument.name] = value
+            else:
+                if name != argument.name:
+                    params.remove(name)
+                    params[argument.name] = value
             if argument.callback != None:
-                if num_provided > 0:
-                    value = argument.callback(hooke, self, argument, value)
+                value = argument.callback(hooke, self, argument, value)
                 params[argument.name] = value
             argument.validate(value)
         return params
@@ -245,9 +250,6 @@ class Argument (object):
         """
         pass # TODO: validation
 
-    # TODO: type conversion
-
-# TODO: type extensions?
 
 # Useful callbacks