woops
[tore.git] / torc / views.py
blob90347059f60875435fce351cdedc9e493bf1c8eb
1 from tord.common import *
2 from list import Columns, Column
4 class Base:
5 def __init__(self):
6 self.ids = {}
7 self.hashs = {}
8 self.restall = False
10 @test
11 def get_id(self, id):
12 if id not in self.ids:
13 self.ids[id] = len(self.ids)
14 self.hashs[self.ids[id]] = id
15 return self.ids[id]
17 @test
18 def get_hash(self, id):
19 return self.hashs[id]
21 @test
22 def get_args(self, args):
23 core, ids = self.core, self.selected
24 if args:
25 new_core, new_ids = args
26 if new_core: core = new_core
27 if new_ids: ids = new_ids
28 return core, ids
30 def id(self, x): return [self.get_hash(x)]
32 def all(self, x): return None
34 class Bindings:
35 def __init__(self):
36 self.bindings = {}
38 @test
39 def add_binding_func(self, key, func, id_func, *args, **kwargs):
40 def true(x): return True
41 self.bindings[key] = lambda l,x: id_func and true(func(id_func(x), *args, **kwargs)) or func(*args, **kwargs)
43 @test
44 def add_binding_view(self, key, view, id_func, store=lambda x: None):
45 self.bindings[key] = lambda l,x: l.change_view(id_func(x), view, store(x))
47 @test
48 def add_binding_list(self, key, attr, func, *args, **kwargs):
49 self.bindings[key] = lambda l,x: func(*args) or getattr(l, attr)(**kwargs)
51 class Torrents(Columns, Base, Bindings):
52 def __init__(self, core):
53 Bindings.__init__(self)
54 Columns.__init__(self)
55 Base.__init__(self)
57 self.name = 'Torrents'
58 self.core = core
60 self.keys = ['state', 'download_payload_rate', 'upload_payload_rate', 'progress', 'eta', 'total_wanted', 'num_peers', 'ratio', 'name']
62 self.add_column('state', Column("Status", fstate, 6, aright))
63 self.add_column('download_payload_rate', Column('Download', fspeed, 7, aright))
64 self.add_column('all_time_upload', Column('upload', fspeed, 7, aright))
65 self.add_column('all_time_download', Column('download', fspeed, 7, aright))
66 self.add_column('upload_payload_rate', Column('Upload', fspeed, 7, aright))
67 self.add_column('progress', Column("Progress", fbar, 12, aright))
68 self.add_column('_progress', Column("Progress", fpcnt, 5, aright))
69 self.add_column('ratio', Column("Ratio", fratio, 5, aright))
70 self.add_column('eta', Column("ETA", ftime, 7, aright))
71 self.add_column('total_wanted', Column("Size", fsize, 6, aright))
72 self.add_column('num_peers', Column("Peers", str, 3, aright))
73 self.add_column('name', Column('Name', str, 0, aleft))
76 self.add_binding_view('f', 'Files', self.id, self.id)
77 self.add_binding_view('F', 'Files', self.all, self.id)
78 self.add_binding_list('y', 'refresh', self.swap_columns, '_progress', 'progress', reset=True, sync=True)
80 self.add_binding_func('A', core.set_torrents_state, self.id, 'queue_position_top', publish='update', all=True)
81 self.add_binding_func('Z', core.set_torrents_state, self.id, 'queue_position_bottom', publish='update', all=True)
82 self.add_binding_func('a', core.set_torrents_state, self.id, 'queue_position_up', publish='update', all=True)
83 self.add_binding_func('z', core.set_torrents_state, self.id, 'queue_position_down', publish='update', all=True)
85 self.add_binding_func('R', core.set_torrents_state, self.all, 'resume')
86 self.add_binding_func('P', core.set_torrents_state, self.all, 'pause')
87 self.add_binding_func('t', core.set_torrents_state, self.id, 'toggle_pause')
89 self.add_binding_func('W', core.set_torrents_state, self.all, 'fastresume_write')
90 self.add_binding_func('w', core.set_torrents_state, self.id, 'fastresume_write')
91 self.add_binding_func('H', core.set_torrents_state, self.all, 'force_recheck')
92 self.add_binding_func('h', core.set_torrents_state, self.id, 'force_recheck')
94 self.add_binding_func('D', core.remove, self.id, True)
95 self.add_binding_func('d', core.remove, self.id)
97 self.add_binding_func('<', core.set_torrents_state, self.all, 'toggle_managed', publish='update', msg=tuple([True]))
98 self.add_binding_func('>', core.set_torrents_state, self.all, 'toggle_managed', publish='update', msg=tuple([False]))
99 self.add_binding_func('m', core.set_torrents_state, self.id, 'toggle_managed', publish='update')
101 @test
102 def get_data(self, args):
103 c, i = self.get_args(args)
104 return c.get_torrents_status(i, 'torrent_hash', self.keys)
106 class Files(Columns, Base, Bindings):
107 def __init__(self, core):
108 Bindings.__init__(self)
109 Columns.__init__(self)
110 Base.__init__(self)
112 self.name = "Files"
113 self.core = core
115 self.keys = ['files_priority', 'files_size', 'files_progress', 'files_path']
117 self.add_column('files_priority', Column("Priority", str, 1, aright))
118 self.add_column('files_size', Column("Size", fsize, 6, aright))
119 self.add_column('files_progress', Column("Progress", fbar, 12, aleft))
120 self.add_column('_files_progress', Column("Progress", fpcnt, 5, aright))
121 self.add_column('files_path', Column('Name', str, 0, aleft))
123 self.add_binding_view('f', 'Torrents', self.all)
124 self.add_binding_view('F', 'Torrents', self.all)
125 self.add_binding_list('y', 'refresh', self.swap_columns, '_files_progress', 'files_progress', reset=True, sync=True)
126 self.add_binding_func('-', self.prioritize, self.get_hash, -1)
127 self.add_binding_func('=', self.prioritize, self.get_hash, +1)
129 def get_data(self, args):
130 files = []
131 c, i = self.get_args(args)
132 for id, data in c.get_torrents_status(i, 'files_id', self.keys):
133 files += [((h,i), [d[i] for d in data]) for h, i in id]
134 return files
136 def prioritize(self, id, d):
137 t,f = id
138 self.core.prioritize(t, file=f, delta=d)