Added basic commandline framework to ui.commandline (using cmd.Cmd)
authorW. Trevor King <wking@drexel.edu>
Sun, 9 May 2010 20:54:30 +0000 (16:54 -0400)
committerW. Trevor King <wking@drexel.edu>
Sun, 9 May 2010 20:54:30 +0000 (16:54 -0400)
hooke/hooke_cli.py
hooke/ui/__init__.py
hooke/ui/commandline.py

index 69b8018a0cdcdc4729194384185520eef89c2b49..2bd9628019448f65361f8c7ea172f3e87a1b18b2 100644 (file)
@@ -980,10 +980,6 @@ Syntax: quit
         else:
             return
 
-    def help_quit(self):
-        self.help_exit()
-    def do_quit(self,args):
-        self.do_exit(args)
 
 
 if __name__ == '__main__':
index 50f6fe84d8f001bf75578dd135525ca58c47acd8..f46e8937e54d5d3c7cf2dd18b23ed1481cabce3a 100644 (file)
@@ -3,6 +3,7 @@
 
 import ConfigParser as configparser
 
+from .. import version
 from ..compat.odict import odict
 from ..config import Setting
 from ..plugin import IsSubclass
@@ -53,7 +54,17 @@ class UserInterface (object):
     def run(self, hooke, ui_to_command_queue, command_to_ui_queue):
         return
 
-    def playlist_status(self, playlist):
+    # Assorted useful tidbits for subclasses
+
+    def _splash_text(self):
+        return ("""
+This is Hooke, version %s
+
+COPYRIGHT
+----
+""" % version()).strip()
+
+    def _playlist_status(self, playlist):
         if playlist.has_curves():
             return '%s (%s/%s)' % (playlist.name, playlist._index + 1,
                                    len(playlist))
index e1554cdf9636c6c1ae5a9413d0b411ac137af03c..e4c533ff54277aefd77ae6a75ceb77ae67765a7a 100644 (file)
@@ -1,7 +1,87 @@
+"""Defines :class:`CommandLine` for driving Hooke from the command
+line.
+"""
+
+import cmd
+import readline
+
 from ..ui import UserInterface
 
+
+class HookeCmd (cmd.Cmd):
+    def __init__(self, hooke):
+        cmd.Cmd.__init__(self)
+        self.hooke = hooke
+        self.prompt = 'hooke> '
+        self._add_do_methods()
+
+    def _safe_name(self, name):
+        return name.lower().replace(' ', '_')
+
+    def _add_do_methods(self):
+        for command in self.hooke.commands:
+            for name in [command.name] + command.aliases:
+                name = self._safe_name(name)
+                def do_fn(self, args):
+                    pass
+                setattr(self.__class__, 'do_%s' % name, do_fn)
+                def help_fn(self):
+                    pass
+                setattr(self.__class__, 'help_%s' % name, help_fn)
+                def complete_fn(self, text, line, begidx, endidx):
+                    pass
+                setattr(self.__class__, 'complete_%s' % name, complete_fn)
+
+    def _help(self, name, aliases, message, usage_args=None):
+        if len(aliases) == 0:
+            alias_string = ''
+        else:
+            alias_string = ' (%s)' % ', '.join(aliases)
+        if usage_args == None:
+            usage_string = name
+        else:
+            usage_string = '%s %s' % (name, usage_args)
+        self.stdout.write(("""
+%s%s
+
+%s
+------
+Usage: %s
+""" % (name, alias_string, message.strip(), usage_string)).lstrip())
+
+    def help_help(self):
+        return self._help('help', [], """
+Called with an argument, prints that command's documentation.
+
+With no argument, lists all available help topics as well as any
+undocumented commands.
+""",
+                          '[command]')
+
+    def do_exit(self, args):
+        return True # the True return stops .cmdloop execution
+
+    def help_exit(self):
+        return self._help('exit', ['quit', 'EOF'], "Exit Hooke cleanly.")
+
+    def do_quit(self, args):
+        return self.do_exit(args)
+
+    def help_quit(self):
+        return self.help_exit()
+
+    def do_EOF(self, args):
+        return self.do_exit(args) 
+
+    def help_EOF(self):
+        return self.help_exit()
+
 class CommandLine (UserInterface):
     """Command line interface.  Simple and powerful.
     """
     def __init__(self):
         super(CommandLine, self).__init__(name='command line')
+
+    def run(self, hooke, ui_to_command_queue, command_to_ui_queue):
+        cmd = HookeCmd(hooke)
+        cmd.cmdloop(self._splash_text())