add a path completer for weboorrents command 'getfile'

This commit is contained in:
Romain Bignon 2010-11-14 14:11:10 +01:00
commit 916fc3d98e
2 changed files with 30 additions and 5 deletions

View file

@ -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

View file

@ -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):
"""