Add permission checking to add command(fixing few crashes), sort files by date, place folders before files and ignore files other than .torrent files

This commit is contained in:
Asmageddon 2012-05-26 17:35:29 +02:00
commit fa1a1eb939

View file

@ -40,6 +40,7 @@ import deluge.ui.console.colors as colors
from deluge.ui.client import client from deluge.ui.client import client
import deluge.component as component import deluge.component as component
import deluge.common import deluge.common
from deluge.ui.common import TorrentInfo
from optparse import make_option from optparse import make_option
import os import os
@ -102,6 +103,8 @@ class Command(BaseCommand):
return defer.DeferredList(deferreds) return defer.DeferredList(deferreds)
def complete(self, line): def complete(self, line):
self.console = component.get("ConsoleUI")
line = line.replace("\ ", " ") line = line.replace("\ ", " ")
line = os.path.abspath(os.path.expanduser(line)) line = os.path.abspath(os.path.expanduser(line))
ret = [] ret = []
@ -110,6 +113,7 @@ class Command(BaseCommand):
if os.path.isdir(line): if os.path.isdir(line):
# Directory, so we need to show contents of directory # Directory, so we need to show contents of directory
#ret.extend(os.listdir(line)) #ret.extend(os.listdir(line))
try:
for f in os.listdir(line): for f in os.listdir(line):
# Skip hidden # Skip hidden
if f.startswith("."): if f.startswith("."):
@ -120,16 +124,24 @@ class Command(BaseCommand):
f += "\\" f += "\\"
else: # \o/ Unix else: # \o/ Unix
f += "/" f += "/"
elif not f.endswith(".torrent"):
continue
ret.append(f) ret.append(f)
except OSError:
self.console.write("{!error!}Permission denied: {!info!}%s" % line)
else: else:
try:
# This is a file, but we could be looking for another file that # This is a file, but we could be looking for another file that
# shares a common prefix. # shares a common prefix.
for f in os.listdir(os.path.dirname(line)): for f in os.listdir(os.path.dirname(line)):
if f.startswith(os.path.split(line)[1]): if f.startswith(os.path.split(line)[1]):
ret.append(os.path.join( os.path.dirname(line), f)) ret.append(os.path.join( os.path.dirname(line), f))
except OSError:
self.console.write("{!error!}Permission denied: {!info!}%s" % line)
else: else:
# This path does not exist, so lets do a listdir on it's parent # This path does not exist, so lets do a listdir on it's parent
# and find any matches. # and find any matches.
try:
ret = [] ret = []
if os.path.isdir(os.path.dirname(line)): if os.path.isdir(os.path.dirname(line)):
for f in os.listdir(os.path.dirname(line)): for f in os.listdir(os.path.dirname(line)):
@ -142,8 +154,18 @@ class Command(BaseCommand):
else: # \o/ Unix else: # \o/ Unix
p += "/" p += "/"
ret.append(p) ret.append(p)
except OSError:
self.console.write("{!error!}Permission denied: {!info!}%s" % line)
#Sort by date instead of whatever we might get from os.listdir
ret = sorted(ret, key=lambda p: os.stat(p).st_mtime, reverse=True) ret = sorted(ret, key=lambda p: os.stat(p).st_mtime, reverse=True)
#Directories first
ret = sorted(ret, key=lambda p: os.path.isdir(p), reverse=True)
#Highlight directory names
for i, filename in enumerate(ret):
if os.path.isdir(filename):
ret[i] = "{!cyan!}%s" % filename
for i in range(0, len(ret)): for i in range(0, len(ret)):
ret[i] = ret[i].replace(" ", r"\ ") ret[i] = ret[i].replace(" ", r"\ ")