From: W. Trevor King Date: Sun, 9 May 2010 20:54:30 +0000 (-0400) Subject: Added basic commandline framework to ui.commandline (using cmd.Cmd) X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1cb1c2ffd2f83b2cb4ae893174b2fad87d2e0d29;p=hooke.git Added basic commandline framework to ui.commandline (using cmd.Cmd) --- diff --git a/hooke/hooke_cli.py b/hooke/hooke_cli.py index 69b8018..2bd9628 100644 --- a/hooke/hooke_cli.py +++ b/hooke/hooke_cli.py @@ -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__': diff --git a/hooke/ui/__init__.py b/hooke/ui/__init__.py index 50f6fe8..f46e893 100644 --- a/hooke/ui/__init__.py +++ b/hooke/ui/__init__.py @@ -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)) diff --git a/hooke/ui/commandline.py b/hooke/ui/commandline.py index e1554cd..e4c533f 100644 --- a/hooke/ui/commandline.py +++ b/hooke/ui/commandline.py @@ -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())