AUTO_LT_SYNC
[tore.git] / torc / torc.py
blob9471feb51d5e4e96c63d01adf3db13895f8fa0a8
1 #!/usr/bin/python
2 import os.path
3 import pytx, traceback, sys, logging
5 from tord.pyro import *
6 from tord.common import *
7 from views import *
8 from list import *
10 DEFAULT_PREFS = {}
12 class Msg(pytx.List):
13 def __init__(self, flags=0, size=5):
14 pytx.List.__init__(self, flags)
15 self.size = size
16 self.count = 0
18 @test
19 def msg(self, text):
20 self.add(self.count, text)
21 self.count += 1;
22 self.scroll(1)
23 self.draw()
25 @test
26 def size_cb(self, s, d):
27 return self.size
29 class Status(pytx.List):
30 def __init__(self, core, flags = 0):
31 pytx.List.__init__(self, flags)
32 self.core = core
34 self.keys = ["download_rate", "download_limit", "upload_rate", "upload_limit", "num_peers", "dht"]
35 self.format = [fspeed, fspeed, fspeed, fspeed, fpeers, lambda x: x<0 and "off" or fpeers(x)]
37 @test
38 def idle_cb(self):
39 data = self.core.get_session_status(self.keys)
40 status = [f(v) for k, f, v in zip(self.keys, self.format, data)]
41 text = "Download[%6s|%6s] Upload[%6s|%6s] Peers[%3s] DHT[%3s]" % tuple(status)
42 self.add(0, text)
43 self.draw()
45 @test
46 def size_cb(self, s, d):
47 return 1
49 class TXTor:
50 def __init__(self):
51 pytx.init()
53 @test
54 def go(self):
56 self.config = Config("torc.conf", DEFAULT_PREFS)
57 self.core = self.get_core()
60 l = Views(pytx.FOCUS|pytx.SCROLL|pytx.SEPARATOR|pytx.TITLES|pytx.HIGHLIGHT, 6)
61 l.view_add(Torrents(self.core))
62 l.view_add(Files(self.core))
63 l.view_add(Peers(self.core))
65 b = pytx.Box(pytx.VERT, pytx.ROOT|pytx.BORDER)
66 m = Msg()
67 s = Status(self.core)
69 b.add(l); b.add(m); b.add(s)
70 pytx.adjust()
72 stream=FakeIO(m.msg, m.draw)
73 logging.root.handlers[0].stream = stream
75 s.idle_cb()
76 b.draw()
77 l.refresh(update=True)
79 self.events = {
80 "update": lambda c,i,m: l.refresh(update=True, sort=False, args=(c,i)),
81 "sort": lambda c,i,m: l.refresh(update=True, sort=True, args=(c,i)),
82 "remove": lambda c,i,m: l.refresh(remove=i),
85 pytx.operate()
86 self.looping = False
88 @test
89 def message(self, msg):
90 if msg is None:
91 self.looping = False
92 print "hello"
93 subject, alert = msg
94 try: self.events[subject](self.get_core(), *alert)
95 except: log.info("%-20s %s" % (str(subject)+':', alert[1]))
97 @test
98 def shutdown(self):
99 pytx.free()
100 logging.root.handlers[0].stream = sys.stderr
102 def main():
103 host = None
104 if len(sys.argv) == 2: host = sys.argv[1]
106 PyroClient(TXTor, host)