📄 oldtorrentqueue.py
字号:
pass self.global_error(ERROR, _("Could not write file ") + path + ' (' + str(e) + '), ' + _("torrent will not be restarted " "correctly on client restart")) config = configfile.read_torrent_config(self.config, self.config['data_dir'], infohash, self.global_error) if config: t.config.update(config) if save_as: t.dlpath = save_as # save_as is removed until it is properly implelented## self.run_ui_task(self.ui.set_config, 'save_as', save_as) else: save_as = None self.torrents[infohash] = t t.state = QUEUED # HACK because TQ is very broken self.queue.append(infohash) #self.other_torrents.append(infohash) self._dump_state() self.run_ui_task(self.ui.new_displayed_torrent, infohash, t.metainfo, save_as, t.state, t.config) def show_error(level, text): self.run_ui_task(self.ui.error, infohash, level, text) t.metainfo.show_encoding_errors(show_error) # HACK because TQ is very broken self.change_torrent_state(infohash, QUEUED, RUNNING) # HACK because TQ is very broken self._dump_config() def set_config(self, option, value, ihash=None): if not ihash: oldvalue = self.config[option] self.config[option] = value self.multitorrent.set_option(option, value) if option == 'pause': if value:# and not oldvalue: self.set_zero_running_torrents() elif not value:# and oldvalue: self._check_queue() elif option == 'max_upload_rate': # This has a terrible bug, which is that it triggers # set_max_upload_rate which in turn calls this again. # The result is a flickering slider. #self.run_ui_task(self.ui.mainwindow.rate_slider_box.set_slider, value) pass else: torrent = self.torrents[ihash] if torrent.state == RUNNING: torrent.dl.set_option(option, value) if option in ('forwarded_port', 'maxport'): torrent.dl.change_port() torrent.config[option] = value self._dump_config() def set_file_priority(self, infohash, filename, priority): torrent = self.torrents.get(infohash) if torrent is None or torrent.state != RUNNING: return torrent.dl.set_file_priority(filename, priority) def request_status(self, infohash, want_spew, want_fileinfo): torrent = self.torrents.get(infohash) if torrent is None or torrent.state != RUNNING: return status = torrent.dl.get_status(want_spew, want_fileinfo) if torrent.finishtime is not None: now = bttime() uptotal = status['upTotal'] + torrent.uptotal_old downtotal = status['downTotal'] + torrent.downtotal_old ulspeed = status['upRate2'] if self.queue: ratio = torrent.dl.config['next_torrent_ratio'] / 100 if torrent.dl.config['seed_forever']: ratio = 1e99 else: ratio = torrent.dl.config['last_torrent_ratio'] / 100 if torrent.dl.config['seed_last_forever']: ratio = 1e99 if ulspeed <= 0 or ratio >= 1e99: rem = 1e99 elif downtotal == 0: rem = (torrent.metainfo.total_bytes * ratio - uptotal) / ulspeed else: rem = (downtotal * ratio - uptotal) / ulspeed if self.queue and not torrent.dl.config['seed_forever']: rem = min(rem, torrent.finishtime + torrent.dl.config['next_torrent_time'] * 60 - now) rem = max(rem, torrent.finishtime + 120 - now) if rem <= 0: rem = 1 if rem >= 1e99: rem = None status['timeEst'] = rem self.run_ui_task(self.ui.update_status, infohash, status) def _get_list(self, state): if state == KNOWN: return self.other_torrents elif state == QUEUED: return self.queue elif state in (RUNNING, RUN_QUEUED): return self.running_torrents assert False def change_torrent_state(self, infohash, oldstate, newstate=None, pred=None, succ=None, replaced=None, force_running=False): t = self.torrents.get(infohash) if t is None or (t.state != oldstate and not (t.state == RUN_QUEUED and oldstate == RUNNING)): return if newstate is None: newstate = oldstate assert oldstate in (KNOWN, QUEUED, RUNNING) assert newstate in (KNOWN, QUEUED, RUNNING) pos = None if oldstate != RUNNING and newstate == RUNNING and replaced is None: if len(self.running_torrents) >= (force_running and self.config[ 'max_running_torrents'] or self.config['def_running_torrents']): if force_running: self.global_error(ERROR, _("Can't run more than %d torrents " "simultaneously. For more info see the" " FAQ at %s.")% (self.config['max_running_torrents'], FAQ_URL)) newstate = QUEUED pos = 0 l = self._get_list(newstate) if newstate == oldstate: try: origpos = l.index(infohash) except IndexError, e: states = ['KNOWN', 'QUEUED', 'RUNNING'] raise IndexError('%s: %s is not %s' % (e, infohash.encode('hex'), states[newstate])) del l[origpos] if pos is None: pos = decode_position(l, pred, succ, -1) if pos == -1 or l == origpos: l.insert(origpos, infohash) return l.insert(pos, infohash) self._dump_state() self.run_ui_task(self.ui.reorder_torrent, infohash, pos) return if pos is None: pos = decode_position(l, pred, succ) if newstate == RUNNING: newstate = RUN_QUEUED if replaced and len(self.running_torrents) >= \ self.config['def_running_torrents']: t2 = self.torrents.get(replaced) if t2 is None or t2.state not in (RUNNING, RUN_QUEUED): return if self.running_torrents.index(replaced) < pos: pos -= 1 if self._stop_running(replaced): t2.state = QUEUED self.queue.insert(0, replaced) self._send_state(replaced) else: self.other_torrents.append(replaced) if oldstate == RUNNING: if newstate == QUEUED and len(self.running_torrents) <= \ self.config['def_running_torrents'] and pos == 0: return if not self._stop_running(infohash): if newstate == KNOWN: self.other_torrents.insert(pos, infohash) self.run_ui_task(self.ui.reorder_torrent, infohash, pos) else: self.other_torrents.append(infohash) return else: self._get_list(oldstate).remove(infohash) t.state = newstate l.insert(pos, infohash) self._check_queue() # sends state if it starts the torrent from queue if t.state != RUNNING or newstate == RUN_QUEUED: self._send_state(infohash) self._dump_state() def set_zero_running_torrents(self): newrun = [] for infohash in list(self.running_torrents): t = self.torrents[infohash] if self._stop_running(infohash): newrun.append(infohash) t.state = RUN_QUEUED else: self.other_torrents.append(infohash) self.running_torrents = newrun def check_completion(self, infohash, filelist=False): t = self.torrents.get(infohash) if t is None: return r = self.multitorrent.get_completion(self.config, t.metainfo, t.dlpath, filelist) if r is None or not filelist: self.run_ui_task(self.ui.update_completion, infohash, r) else: self.run_ui_task(self.ui.update_completion, infohash, *r) def global_error(self, level, text): self.run_ui_task(self.ui.global_error, level, text) # callbacks from torrent instances def failed(self, torrent, is_external): infohash = torrent.infohash if infohash == self.starting_torrent: self.starting_torrent = None self.running_torrents.remove(infohash) t = self.torrents[infohash] t.state = KNOWN if is_external: t.completion = self.multitorrent.get_completion( self.config, t.metainfo, t.dlpath) else: t.completion = None totals = t.dl.get_total_transfer() t.uptotal_old += totals[0] t.uptotal = t.uptotal_old t.downtotal_old += totals[1] t.downtotal = t.downtotal_old t.dl = None self.other_torrents.append(infohash) self._send_state(infohash) if not self.doneflag.isSet(): self._check_queue() self._dump_state() def finished(self, torrent): """called when a download reaches 100%""" infohash = torrent.infohash t = self.torrents[infohash] totals = t.dl.get_total_transfer() if t.downtotal == 0 and t.downtotal_old == 0 and totals[1] == 0: self.set_config('seed_forever', True, infohash) self.set_config('seed_last_forever', True, infohash) self.request_status(infohash, False, False) if infohash == self.starting_torrent: t = self.torrents[infohash] if self.queue: ratio = t.config['next_torrent_ratio'] / 100 if t.config['seed_forever']: ratio = 1e99 msg = _("Not starting torrent as there are other torrents " "waiting to run, and this one already meets the " "settings for when to stop seeding.") else: ratio = t.config['last_torrent_ratio'] / 100 if t.config['seed_last_forever']: ratio = 1e99 msg = _("Not starting torrent as it already meets the " "settings for when to stop seeding the last " "completed torrent.") if ratio < 1e99 and t.uptotal >= t.metainfo.total_bytes * ratio: raise BTShutdown(msg) self.torrents[torrent.infohash].finishtime = bttime() def started(self, torrent): infohash = torrent.infohash assert infohash == self.starting_torrent self.starting_torrent = None self._check_queue() def error(self, torrent, level, text): self.run_ui_task(self.ui.error, torrent.infohash, level, text)class ThreadWrappedQueue(object): def __init__(self, wrapped): self.wrapped = wrapped def set_done(self): self.wrapped.doneflag.set() # add a dummy task to make sure the thread wakes up and notices flag def dummy(): pass self.wrapped.rawserver.external_add_task(0, dummy) def __getattr__(self, attr): if attr in ('change_torrent_state', 'check_completion', 'remove_torrent', 'request_status', 'set_config', 'set_save_location', 'set_file_priority', 'start_new_torrent', 'send_gui_rates'): method = getattr(self.wrapped, attr) return lambda *a: self.wrapped.rawserver.external_add_task(0, method, *a)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -