From 61c29b42437ed3166b025fa1a44b54b16267249a Mon Sep 17 00:00:00 2001 From: Josh Davis Date: Sat, 31 May 2008 23:30:25 +1000 Subject: [PATCH] beginning to refract columns. initial attempt at file priority --- torc/torc.py | 100 ++++++++++++++++++++++++++++++++--------------------------- tord/tord.py | 76 +++++++++++++++++++++++++++++---------------- 2 files changed, 105 insertions(+), 71 deletions(-) diff --git a/torc/torc.py b/torc/torc.py index 71631c0..41ab92a 100755 --- a/torc/torc.py +++ b/torc/torc.py @@ -83,7 +83,6 @@ class Torrents: self.name = 'torrents' self.keys = ['state', 'download_payload_rate', 'upload_payload_rate', 'progress', 'eta', 'total_wanted', 'num_peers', 'name'] - self._bindings = { 'f' : lambda l,x: l.view_change('files', x), 'c' : lambda l,x: l.view_change('config_core', x), @@ -108,60 +107,57 @@ class Torrents: class Files: def __init__(self): self.name = "files" - self.keys = ['files_size', 'files_progress', 'files_path'] + self.keys = ['files_priority', 'files_size', 'files_progress', 'files_path'] self._bindings = { - 'u' : lambda l,x: l.refresh(update=True), - 'y' : lambda l,x: l.columns_swap('files_progress', '_files_progress'), + 'u': lambda l,x: l.refresh(update=True), + 'y': lambda l,x: l.columns_swap('files_progress', '_files_progress'), + '-': lambda l,x: self.prioritize(l.core, x, -1), + '=': lambda l,x: self.prioritize(l.core, x, 1) } - #FIXME: put in torrent id in returned data to uniquely ident file def func(self, c, k, i, a, s): files = [] - for id, data in c.get_torrents_status(i, 'files_id', k): - files += [(i, [d[i] for d in data]) for i in id] - return files + for id, data in c.get_torrents_status(i, 'files_id', k): + files += [((h,i), [d[i] for d in data]) for h, i in id] -class ConfigCore: - def __init__(self): - self.name = 'config_core' - self.keys = ['key', 'value'] - self.func = lambda c, k, i, a, s: c.get_config() - self._bindings = { - } + return files -class Column: - def __init__(self, func, title, t_width, nt_width, align, resize): - self.func = func - self.title = title - self.t_width = t_width - self.nt_width = nt_width - self.align = align - self.resize = resize - - def format(self, text, width): - if self.align == 0: - return text.rjust(width) - if self.align == 1: - return text.ljust(width) - - def format_data(self, data, width): - return self.format(self.func(data), width) - - def format_title(self, width): - return self.format(self.title, width) - - def format_width(self, title): - if title: return self.t_width - return self.nt_width + def prioritize(self, core, id, x): + core.prioritize(id[0], file=id[1], delta=x) class Columns: + class Column: + def __init__(self, func, title, t_width, nt_width, align, resize): + self.func = func + self.title = title + self.t_width = t_width + self.nt_width = nt_width + self.align = align + self.resize = resize + + def format(self, text, width): + if self.align == 0: + return text.rjust(width) + if self.align == 1: + return text.ljust(width) + + def format_data(self, data, width): + return self.format(self.func(data), width) + + def format_title(self, width): + return self.format(self.title, width) + + def format_width(self, title): + if title: return self.t_width + return self.nt_width + def __init__(self): self.cols = {} def add_column(self, key, func, title, t_width, nt_width, align, resize): - self.cols[key] = Column(func, title, t_width, nt_width, align, resize) + self.cols[key] = self.Column(func, title, t_width, nt_width, align, resize) def columns_swap(self, a, b): self.cols[a], self.cols[b] = self.cols[b], self.cols[a] @@ -320,24 +316,38 @@ class TXTor: l = List(self.core, self.get_core, pytx.FOCUS|pytx.SCROLL|pytx.SEPARATOR|pytx.TITLES|pytx.HIGHLIGHT) l.view_add(Torrents()) l.view_add(Files()) - l.view_add(ConfigCore()) - #key, func, title, t_width, nt_width, align, resize): - l.add_column("state", state, 'state', 5, 2, 0, False) + ''' + l.add_map("name", str, 0, 1, True) + l.add_map("progress_bar", bar, 12, 0, False) + l.add_map("progress_num", fpcnt, 8, 0, False) + l.add_map("rate", fspeed, 6, 0, False) + l.add_map("size", fsize, 6, 0, False) + l.add_map("eta", ftime, 7, 0, False) + l.add_map("priority", priority, 1, 0, True) + ''' + + l.add_column("name", str, 'name', 0, 0, 1, True) + l.add_column("progress_bar", bar, 'progress', 12, 12, 0, False) + l.add_column("progress_num", fpcnt, 'progress', 8, 8, 0, False) + l.add_column("rate", fspeed, 'up', 6, 6, 0, False) + l.add_column("size", fsize, 'size', 6, 6, 0, False) l.add_column("eta", ftime, 'eta', 7, 7, 0, False) + + + l.add_column("state", state, 'state', 5, 2, 0, False) l.add_column("progress", bar, 'progress', 12, 12, 0, False) l.add_column("_progress", fpcnt, 'progress', 8, 8, 0, False) l.add_column("upload_payload_rate", fspeed, 'up', 6, 6, 0, False) l.add_column("download_payload_rate", fspeed, 'down', 6, 6, 0, False) l.add_column("total_wanted", fsize, 'size', 6, 6, 0, False) l.add_column("num_peers", peers, 'peers', 5, 3, 0, False) - l.add_column("name", str, 'name', 0, 0, 1, True) + l.add_column("files_priority", priority, 'priority', 8, 1, 0, True) l.add_column("files_path", str, 'path', 0, 0, 1, True) l.add_column("files_size", fsize, 'size', 6, 6, 0, False) l.add_column("files_id", str, 'id', 15, 15, 0, False) l.add_column("files_progress", bar, 'progress', 12, 12, 0, False) l.add_column("_files_progress", fpcnt, 'progress', 8, 8, 0, False) - l.add_column("priority", priority, 'priority', 8, 1, 0, True) l.add_column("path", str, 'path', 0, 0, 1, True) l.add_column("id", str, 'id', 3, 3, 0, True) l.add_column("queue", str, 'queue', 5, 3, 0, True) diff --git a/tord/tord.py b/tord/tord.py index a5cc845..89dd44d 100644 --- a/tord/tord.py +++ b/tord/tord.py @@ -62,10 +62,12 @@ class Watched: except: pass class Torrent: - def __init__(self, handle, save_path, file): + def __init__(self, hash, handle, save_path, file, files_priority): self.handle = handle self.save_path = save_path self.file = file + self.hash = hash + self.files_priority = files_priority self.info = handle.get_torrent_info() self.name = self.info.name() @@ -76,7 +78,7 @@ class Torrent: self.files_offset = [] for id, f in enumerate(self.info.files()): - self.files_id += [id] + self.files_id += [(hash, id)] self.files_path += [f.path] self.files_size += [f.size] self.files_offset += [f.offset] @@ -142,7 +144,9 @@ class CoreTorrent: resume_info = handle.write_resume_data() resume_info["save_path"] = self.torrents[resume_file].save_path resume_info["file"] = self.torrents[resume_file].file + resume_info["files_priority"] = self.torrents[resume_file].files_priority resume_info["paused"] = handle.status().paused + resume_data = lt.bencode(resume_info) resume_dir = self.config['path_fastresume'] @@ -156,37 +160,51 @@ class CoreTorrent: h = t.handle s = h.status() - #if active and not (s.num_peers or s.download_payload_rate or s.upload_payload_rate or (s.state == 1)): - # return None - - custom = { + values = { 'eta': eta(s.total_wanted, s.total_done, s.download_payload_rate), 'state': (int(s.state), s.paused), 'name': t.name, - 'torrent_hash': hash, + 'torrent_hash': t.hash, 'files_path': t.files_path, 'files_size': t.files_size, + 'files_priority': t.files_priority, 'files_id': t.files_id, - 'files_progress': h.file_progress() - } + } + + funcs = { + 'files_progress': h.file_progress + } - data = [self.get_stat(s, f, custom) for f in fields] - id = self.get_stat(s, id_field, custom) + data = [self.get_stat(s, f, values, funcs) for f in fields] + id = self.get_stat(s, id_field, values, funcs) return (id, data) - return [get_status(t) for t in self.torrents.keys()] + return [get_status(t) for t in torrents or self.torrents.keys()] - @test def set_torrents_state(self, attr, torrents = None, msg = None): for t in torrents or self.torrents.keys(): try: getattr(self.torrents[t].handle, attr)() except AttributeError: getattr(self, attr)(t, msg) + finally: self.alert_handle(attr, t) + def toggle(self, t, msg): h = self.torrents[t].handle if h.status().paused: h.resume() else: h.pause() - + + def prioritize(self, t, mask = [], file = None, delta = 0): + if not mask: + mask = self.torrents[t].files_priority + + if file and delta: + mask[file] = (mask[file] + delta) % 10 + self.torrents[t].files_priority = mask + + self.torrents[t].handle.prioritize_files(mask) + # perhaps do an alert + self.publish('update', ("prioritize", t)) + def update_cb(self, alert, ids): self.publish('update', (alert, ids)) @@ -219,7 +237,6 @@ class CoreSession: self.config.register_set_function("max_connections_global", self.set_max_connections_global) self.config.register_set_function("max_upload_slots_global", self.set_max_upload_slots_global) - @test def add(self, torrent_dir, torrent_file = None): def add(torrent_dir, torrent_file): log.info("Adding torrent: %s", torrent_file) @@ -233,6 +250,7 @@ class CoreSession: default_resume = { 'save_path': self.config["path_downloads_current"], 'file': torrent_file, + 'files_priority': [1]*torrent_info.num_files(), 'paused': False, } @@ -249,14 +267,21 @@ class CoreSession: } torrent_handle = self.session.add_torrent(param) - self.torrents[torrent_hash] = Torrent(torrent_handle, resume_data['save_path'], resume_data['file']) - - self.torrents[torrent_hash].handle.set_max_connections(30) - - self.alert_handle("torrent_added_alert", torrent_hash) + self.torrents[torrent_hash] = Torrent( + torrent_hash, + torrent_handle, + resume_data['save_path'], + resume_data['file'], + resume_data['files_priority'] + ) + #set state + self.torrents[torrent_hash].handle.set_max_connections(30) + self.prioritize(torrent_hash, resume_data['files_priority']) + + self.alert_handle("torrent_added_alert", torrent_hash) ''' add a directory ''' if not torrent_file: @@ -268,7 +293,6 @@ class CoreSession: ''' add a single file ''' add(torrent_dir, torrent_file) - @test def remove(self, t, force): log.info("Removing torrent: %s", t) h = self.torrents[t].handle @@ -289,15 +313,14 @@ class CoreSession: del self.torrents[t] self.alert_handle("torrent_remove_alert", t) - @test def get_session_status(self, fields): s = self.session.status() - custom = { + values = { "download_limit": int(self.session.download_rate_limit()), "upload_limit": int(self.session.upload_rate_limit()), } - return [self.get_stat(s, f, custom) for f in fields] + return [self.get_stat(s, f, values) for f in fields] def set_listen_ports(self, key, value): log.debug("listen port range set to %s-%s", value[0], value[1]) @@ -383,8 +406,9 @@ class Core(Alerts, Watched, CoreSession, CoreTorrent): self.alert_handle(alert_type, alert) alert = self.session.pop_alert() - def get_stat(self, obj, k, c): - if k in c: return c[k] + def get_stat(self, obj, k, v = {}, f = {}): + if k in v: return v[k] + if k in f: return f[k]() return getattr(obj, k) def get_stats(self, items, func): -- 2.11.4.GIT