Rearrange almost all of the code.
This commit is contained in:
parent
5f40e030b1
commit
3d26049aeb
|
@ -53,22 +53,31 @@ status_keys = ["state",
|
||||||
"file_progress",
|
"file_progress",
|
||||||
]
|
]
|
||||||
|
|
||||||
def add_torrent(cmd):
|
class Command:
|
||||||
"""Add a torrent."""
|
def __init__(self):
|
||||||
def show_usage():
|
pass
|
||||||
print "Usage: add [-p <save-location>;] <torrent-file>; [<torrent-file>; ...]"
|
|
||||||
print " (Note that a ';' must follow a path)"
|
def execute(self, cmd):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def usage(self):
|
||||||
print ""
|
print ""
|
||||||
|
|
||||||
|
def help(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class CommandAdd(Command):
|
||||||
|
"""Command to add a torrent."""
|
||||||
|
def execute(self, cmd):
|
||||||
if len(cmd) < 2:
|
if len(cmd) < 2:
|
||||||
show_usage()
|
self.usage()
|
||||||
return
|
return
|
||||||
|
|
||||||
save_path = None
|
save_path = None
|
||||||
readpath = False
|
readpath = False
|
||||||
if cmd[1] == '-p':
|
if cmd[1] == '-p':
|
||||||
if len(cmd) < 4:
|
if len(cmd) < 4:
|
||||||
show_usage()
|
self.usage()
|
||||||
return
|
return
|
||||||
del cmd[1]
|
del cmd[1]
|
||||||
readpath = True
|
readpath = True
|
||||||
|
@ -97,12 +106,19 @@ def add_torrent(cmd):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
client.add_torrent_file(paths)
|
client.add_torrent_file(paths)
|
||||||
client.force_call()
|
|
||||||
except Exception, msg:
|
except Exception, msg:
|
||||||
print "*** Error:", str(msg), "\n"
|
print "*** Error:", str(msg), "\n"
|
||||||
|
|
||||||
|
def usage(self):
|
||||||
|
print "Usage: add [-p <save-location>;] <torrent-file>; [<torrent-file>; ...]"
|
||||||
|
print " (Note that a ';' must follow a path)"
|
||||||
|
print ""
|
||||||
|
|
||||||
def show_configs(cmd):
|
def help(self):
|
||||||
|
print "Add a torrent"
|
||||||
|
|
||||||
|
class CommandConfig(Command):
|
||||||
|
def execute(self, cmd):
|
||||||
del cmd[0]
|
del cmd[0]
|
||||||
def _on_get_config(config):
|
def _on_get_config(config):
|
||||||
for key in config:
|
for key in config:
|
||||||
|
@ -110,14 +126,73 @@ def show_configs(cmd):
|
||||||
print "%s: %s" % (key, config[key])
|
print "%s: %s" % (key, config[key])
|
||||||
print ""
|
print ""
|
||||||
client.get_config(_on_get_config)
|
client.get_config(_on_get_config)
|
||||||
client.force_call()
|
|
||||||
|
|
||||||
def show_state(state):
|
def usage(self):
|
||||||
|
print "Usage: config [key1 [key2 ...]]"
|
||||||
|
print ""
|
||||||
|
|
||||||
|
def help(self):
|
||||||
|
print "Show configuration values"
|
||||||
|
|
||||||
|
class CommandExit(Command):
|
||||||
|
def execute(self, cmd):
|
||||||
|
print "Thanks"
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
def help(self):
|
||||||
|
print "Exit from the client."
|
||||||
|
|
||||||
|
class CommandHelp(Command):
|
||||||
|
def execute(self, cmd):
|
||||||
|
if len(cmd) < 2:
|
||||||
|
print "Available commands:"
|
||||||
|
for cmd in sorted(commands.keys()):
|
||||||
|
print "\t*", "%s:" % cmd,
|
||||||
|
command = commands[cmd]
|
||||||
|
command.help()
|
||||||
|
else:
|
||||||
|
for c in cmd[1:]:
|
||||||
|
if c not in commands:
|
||||||
|
print "Unknown command:", c
|
||||||
|
else:
|
||||||
|
print "*", "%s:" % c,
|
||||||
|
command = commands[c]
|
||||||
|
command.help()
|
||||||
|
command.usage()
|
||||||
|
|
||||||
|
def usage(self):
|
||||||
|
print "Usage: help [cmd1 [cmd2 ...]]"
|
||||||
|
print ""
|
||||||
|
|
||||||
|
def help(self):
|
||||||
|
print "Show help"
|
||||||
|
|
||||||
|
class CommandInfo(Command):
|
||||||
|
def execute(self, cmd):
|
||||||
|
torrents = []
|
||||||
|
def _got_session_state(tors):
|
||||||
|
for tor in tors:
|
||||||
|
torrents.append(tor)
|
||||||
|
client.get_session_state(_got_session_state)
|
||||||
|
client.force_call()
|
||||||
|
for tor in torrents:
|
||||||
|
if len(cmd) < 2:
|
||||||
|
self.show_info(tor, True)
|
||||||
|
elif cmd[1] == tor[0:len(cmd[1])]:
|
||||||
|
self.show_info(tor, False)
|
||||||
|
|
||||||
|
def usage(self):
|
||||||
|
print "Usage: info [<torrent-id> [<torrent-id> ...]]"
|
||||||
|
print " You can give the first few characters of a torrent-id to identify the torrent."
|
||||||
|
print ""
|
||||||
|
|
||||||
|
def help(self):
|
||||||
|
print "Show information about the torrents"
|
||||||
|
|
||||||
|
def show_info(self, torrent, brief):
|
||||||
|
def show_state(state):
|
||||||
ts = common.TORRENT_STATE
|
ts = common.TORRENT_STATE
|
||||||
return ts.keys()[ts.values().index(state)]
|
return ts.keys()[ts.values().index(state)]
|
||||||
|
|
||||||
def show_info(torrent, brief):
|
|
||||||
"""Show information about a torrent."""
|
|
||||||
def _got_torrent_status(state):
|
def _got_torrent_status(state):
|
||||||
print "*** ID:", torrent
|
print "*** ID:", torrent
|
||||||
print "*** Name:", state['name']
|
print "*** Name:", state['name']
|
||||||
|
@ -139,83 +214,71 @@ def show_info(torrent, brief):
|
||||||
for i, file in enumerate(state['files']):
|
for i, file in enumerate(state['files']):
|
||||||
print "\t*", file['path'], "(%s)" % common.fsize(file['size']), "-", "%.1f%% completed" % (state['file_progress'][i] * 100)
|
print "\t*", file['path'], "(%s)" % common.fsize(file['size']), "-", "%.1f%% completed" % (state['file_progress'][i] * 100)
|
||||||
print ""
|
print ""
|
||||||
pr = state['file_priorities']
|
|
||||||
print pr
|
|
||||||
if len(pr) == 0:
|
|
||||||
pr = [1] * len(state['files'])
|
|
||||||
pr[0] = 2
|
|
||||||
print "b", pr
|
|
||||||
client.set_torrent_file_priorities(torrent, pr)
|
|
||||||
client.get_torrent_status(_got_torrent_status, torrent, status_keys)
|
client.get_torrent_status(_got_torrent_status, torrent, status_keys)
|
||||||
|
|
||||||
def info_torrents(cmd):
|
class CommandPause(Command):
|
||||||
"""Show information about the torrents."""
|
def execute(self, cmd):
|
||||||
torrents = []
|
|
||||||
def _got_session_state(tors):
|
|
||||||
for tor in tors:
|
|
||||||
torrents.append(tor)
|
|
||||||
client.get_session_state(_got_session_state)
|
|
||||||
client.force_call()
|
|
||||||
for tor in torrents:
|
|
||||||
if len(cmd) < 2:
|
if len(cmd) < 2:
|
||||||
show_info(tor, True)
|
self.usage()
|
||||||
elif cmd[1] == tor[0:len(cmd[1])]:
|
|
||||||
show_info(tor, False)
|
|
||||||
client.force_call()
|
|
||||||
|
|
||||||
def exit(cmd):
|
|
||||||
"""Terminate."""
|
|
||||||
print "Thanks."
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
def show_help(cmd):
|
|
||||||
"""Show help."""
|
|
||||||
print "Available commands:"
|
|
||||||
for cmd, action, help in commands:
|
|
||||||
print "\t" + cmd + ": " + help
|
|
||||||
|
|
||||||
def pause_torrent(cmd):
|
|
||||||
"""Pause a torrent"""
|
|
||||||
if len(cmd) < 2:
|
|
||||||
print "Usage: pause <torrent-id> [<torrent-id> ...]"
|
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
client.pause_torrent(cmd[1:])
|
client.pause_torrent(cmd[1:])
|
||||||
except Exception, msg:
|
except Exception, msg:
|
||||||
print "Error:", str(msg), "\n"
|
print "Error:", str(msg), "\n"
|
||||||
|
|
||||||
def resume_torrent(cmd):
|
def usage(self):
|
||||||
"""Resume a torrent."""
|
print "Usage: pause <torrent-id> [<torrent-id> ...]"
|
||||||
|
print ""
|
||||||
|
|
||||||
|
def help(self):
|
||||||
|
print "Pause a torrent"
|
||||||
|
|
||||||
|
class CommandResume(Command):
|
||||||
|
def execute(self, cmd):
|
||||||
if len(cmd) < 2:
|
if len(cmd) < 2:
|
||||||
print "Usage: resume <torrent-id> [<torrent-id> ...]"
|
self.usage()
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
client.resume_torrent(cmd[1:])
|
client.resume_torrent(cmd[1:])
|
||||||
except Exception, msg:
|
except Exception, msg:
|
||||||
print "Error:", str(msg), "\n"
|
print "Error:", str(msg), "\n"
|
||||||
|
|
||||||
def remove_torrent(cmd):
|
def usage(self):
|
||||||
"""Remove a torrent."""
|
print "Usage: resume <torrent-id> [<torrent-id> ...]"
|
||||||
if len(cmd) < 2:
|
|
||||||
print "Usage: rm <torrent-id> [<torrent-id> ...]"
|
|
||||||
print " Use 'list' to see the list of torrents."
|
|
||||||
print ""
|
print ""
|
||||||
|
|
||||||
|
def help(self):
|
||||||
|
print "Resume a torrent"
|
||||||
|
|
||||||
|
class CommandRemove(Command):
|
||||||
|
def execute(self, cmd):
|
||||||
|
if len(cmd) < 2:
|
||||||
|
self.usage()
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
client.remove_torrent(cmd[1:])
|
client.remove_torrent(cmd[1:])
|
||||||
except Exception, msg:
|
except Exception, msg:
|
||||||
print "*** Error:", str(msg), "\n"
|
print "*** Error:", str(msg), "\n"
|
||||||
|
|
||||||
commands = (('add', add_torrent, 'Add a torrent'),
|
def usage(self):
|
||||||
('configs', show_configs, 'Show configurations'),
|
print "Usage: rm <torrent-id> [<torrent-id> ...]"
|
||||||
('exit', exit, 'Terminate'),
|
print ""
|
||||||
('help', show_help, 'Show help about a command, or generic help'),
|
|
||||||
('info', info_torrents, 'Show information about the torrents'),
|
def help(self):
|
||||||
('pause', pause_torrent, 'Pause a torrent.'),
|
print "Remove a torrent"
|
||||||
('quit', exit, 'Terminate'),
|
|
||||||
('resume', resume_torrent, 'Resume a torrent.'),
|
commands = {
|
||||||
('rm', remove_torrent, 'Remove a torrent'),
|
'add' : CommandAdd(),
|
||||||
)
|
'configs' : CommandConfig(),
|
||||||
|
'exit' : CommandExit(),
|
||||||
|
'help' : CommandHelp(),
|
||||||
|
'info' : CommandInfo(),
|
||||||
|
'pause' : CommandPause(),
|
||||||
|
'quit' : CommandExit(),
|
||||||
|
'resume' : CommandResume(),
|
||||||
|
'rm' : CommandRemove(),
|
||||||
|
'del' : CommandRemove(),
|
||||||
|
}
|
||||||
|
|
||||||
client.set_core_uri("http://localhost:58846")
|
client.set_core_uri("http://localhost:58846")
|
||||||
|
|
||||||
|
@ -224,6 +287,7 @@ class NullUI:
|
||||||
print "Welcome to deluge-shell. Type 'help' to see a list of available commands."
|
print "Welcome to deluge-shell. Type 'help' to see a list of available commands."
|
||||||
|
|
||||||
readline.read_init_file()
|
readline.read_init_file()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
inp = raw_input("> ")
|
inp = raw_input("> ")
|
||||||
if len(inp) == 0: break
|
if len(inp) == 0: break
|
||||||
|
@ -232,14 +296,12 @@ class NullUI:
|
||||||
print ""
|
print ""
|
||||||
cmd = inp[0]
|
cmd = inp[0]
|
||||||
found = False
|
found = False
|
||||||
for command, action, help in commands:
|
if cmd not in commands:
|
||||||
if command != cmd:
|
|
||||||
continue
|
|
||||||
action(inp)
|
|
||||||
found = True
|
|
||||||
break
|
|
||||||
if not found:
|
|
||||||
print "Invalid command!"
|
print "Invalid command!"
|
||||||
show_help([])
|
commands['help'].execute([])
|
||||||
|
else:
|
||||||
|
command = commands[cmd]
|
||||||
|
command.execute(inp)
|
||||||
|
client.force_call()
|
||||||
|
|
||||||
print "Thanks."
|
print "Thanks."
|
||||||
|
|
Loading…
Reference in New Issue