From 916fc3d98e3bad266e4b2a8a6b9e5e120e0b1fa2 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sun, 14 Nov 2010 14:11:10 +0100 Subject: [PATCH] add a path completer for weboorrents command 'getfile' --- .../applications/weboorrents/weboorrents.py | 14 +++++++++---- weboob/tools/application/repl.py | 21 ++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/weboob/applications/weboorrents/weboorrents.py b/weboob/applications/weboorrents/weboorrents.py index be9544c5..6219cf24 100644 --- a/weboob/applications/weboorrents/weboorrents.py +++ b/weboob/applications/weboorrents/weboorrents.py @@ -119,10 +119,12 @@ class Weboorrents(ReplApplication): else: self.flush() - def complete_info(self, text, line, *ignored): - args = line.split(' ') + def complete_getfile(self, text, line, *ignored): + args = line.split(' ', 2) if len(args) == 2: return self._complete_id() + elif len(args) >= 3: + return self.path_completer(args[2]) def do_getfile(self, line): """ @@ -141,8 +143,12 @@ class Weboorrents(ReplApplication): if dest == '-': print buf else: - with open(dest, 'w') as f: - f.write(buf) + try: + with open(dest, 'w') as f: + f.write(buf) + except IOError, e: + print >>sys.stderr, 'Unable to write .torrent in "%s": %s' % (dest, e) + return 1 return print >>sys.stderr, 'Torrent "%s" not found' % id diff --git a/weboob/tools/application/repl.py b/weboob/tools/application/repl.py index 209a8ebb..3f011137 100644 --- a/weboob/tools/application/repl.py +++ b/weboob/tools/application/repl.py @@ -567,6 +567,21 @@ class ReplApplication(Cmd, BaseApplication): def completenames(self, text, *ignored): return [name for name in Cmd.completenames(self, text, *ignored) if name not in self.hidden_commands] + def path_completer(self, arg): + dirname = os.path.dirname(arg) + try: + childs = os.listdir(dirname or '.') + except OSError: + return () + l = [] + for child in childs: + path = os.path.join(dirname, child) + if os.path.isdir(path): + child += '/' + l.append(child) + return l + + def complete(self, text, state): """ Override of the Cmd.complete() method to: @@ -584,9 +599,13 @@ class ReplApplication(Cmd, BaseApplication): self.completion_matches = [choice for choice in self.completion_matches if choice.startswith(text)] try: - return '%s ' % self.completion_matches[state] + match = self.completion_matches[state] except IndexError: return None + else: + if match[-1] != '/': + return '%s ' % match + return match def do_backends(self, line): """