📄 multitorrent.py
字号:
elif option == 'maxport': if not self.config['minport'] <= self.singleport_listener.port <= \ self.config['maxport']: self._find_port() def add_policy(self, policy): self.policies.append(policy) def add_auto_update_policy(self, policy): self.add_policy(policy) self.auto_update_policy_index = self.policies.index(policy) def global_error(self, severity, message, exc_info=None): self.logger.log(severity, message, exc_info=exc_info) def create_torrent(self, metainfo, save_incomplete_as, save_as, hidden=False, is_auto_update=False, feedback=None): if self.is_single_torrent and len(self.torrents) > 0: raise TooManyTorrents(_("MultiTorrent is set to download only " "a single torrent, but tried to create more than one.")) #save_as, junk = encode_for_filesystem(save_as) #save_incomplete_as, junk = encode_for_filesystem(save_incomplete_as) infohash = metainfo.infohash if self.torrent_known(infohash): if self.torrent_running(infohash): msg = _("This torrent (or one with the same contents) is " "already running.") raise TorrentAlreadyRunning(msg) else: raise TorrentAlreadyInQueue(_("This torrent (or one with " "the same contents) is " "already waiting to run.")) self._dump_metainfo(metainfo) #BUG. Use _read_torrent_config for 5.0? --Dave config = configfile.read_torrent_config(self.config, self.data_dir, infohash, lambda s : self.global_error(logging.ERROR, s)) t = Torrent(metainfo, save_incomplete_as, save_as, self.config, self.data_dir, self.rawserver, self.choker, self.singleport_listener, self.ratelimiter, self.total_downmeasure, self.filepool, self.dht, self, self.log_root, hidden=hidden, is_auto_update=is_auto_update) if feedback: t.add_feedback(feedback) retdf = Deferred() def torrent_started(*args): if config: t.update_config(config) t._dump_torrent_config() if self.resume_from_torrent_config: self._dump_torrents() t.metainfo.show_encoding_errors(self.logger.log) retdf.callback(t) df = self._init_torrent(t, use_policy=False) df.addCallback(torrent_started) return retdf def remove_torrent(self, ihash, del_files=False): # this feels redundant. the torrent will stop the download itself, # can't we accomplish the rest through a callback or something? if self.torrent_running(ihash): self.stop_torrent(ihash) t = self.torrents[ihash] # super carefully determine whether these are really incomplete files fs_save_incomplete_in, junk = encode_for_filesystem( self.config['save_incomplete_in'] ) inco = ((not t.completed) and (t.working_path != t.destination_path) and t.working_path.startswith(fs_save_incomplete_in)) del_files = del_files and inco df = t.shutdown() df.addCallback(lambda *args: t.remove_state_files(del_files=del_files)) if ihash in self.running: del self.running[ihash] # give the torrent a blank feedback, so post-mortem errors don't # confuse multitorrent t.feedback = Feedback() del self.torrents[ihash] if self.resume_from_torrent_config: self._dump_torrents() return df def reinitialize_torrent(self, infohash): t = self.get_torrent(infohash) if self.torrent_running(infohash): assert t.is_running() raise TorrentAlreadyRunning(infohash.encode("hex")) assert t.state == "failed" df = self._init_torrent(t, use_policy=False) return df def start_torrent(self, infohash): if self.is_single_torrent and len(self.torrents) > 1: raise TooManyTorrents(_("MultiTorrent is set to download only " "a single torrent, but tried to create more than one.")) t = self.get_torrent(infohash) if self.torrent_running(infohash): assert t.is_running() raise TorrentAlreadyRunning(infohash.encode("hex")) if not t.is_initialized(): raise TorrentNotInitialized(infohash.encode("hex")) t.logger.debug("starting torrent") self.running[infohash] = t t.start_download() t._dump_torrent_config() return t.state def stop_torrent(self, infohash, pause=False): if not self.torrent_running(infohash): raise TorrentNotRunning() t = self.get_torrent(infohash) assert t.is_running() t.logger.debug("stopping torrent") t.stop_download(pause=pause) del self.running[infohash] t._dump_torrent_config() return t.state def torrent_status(self, infohash, spew=False, fileinfo=False): torrent = self.get_torrent(infohash) status = torrent.get_status(spew, fileinfo) return torrent, status def get_torrent(self, infohash): try: t = self.torrents[infohash] except KeyError: raise UnknownInfohash(infohash.encode("hex")) return t def get_torrents(self): return self.torrents.values() def get_running(self): return self.running.keys() def get_visible_torrents(self): return [t for t in self.torrents.values() if not t.hidden] def get_visible_running(self): return [i for i in self.running.keys() if not self.torrents[i].hidden] def torrent_running(self, ihash): return ihash in self.running def torrent_known(self, ihash): return ihash in self.torrents def pause(self): for i in self.running.keys(): self.stop_torrent(i, pause=True) def unpause(self): for i in [t.metainfo.infohash for t in self.torrents.values() if t.is_initialized()]: self.start_torrent(i) def set_file_priority(self, infohash, filename, priority): torrent = self.get_torrent(infohash) if torrent is None or not self.torrent_running(infohash): return torrent.set_file_priority(filename, priority) def set_torrent_priority(self, infohash, priority): torrent = self.get_torrent(infohash) if torrent is None: return torrent.priority = priority torrent._dump_torrent_config() def set_torrent_policy(self, infohash, policy): torrent = self.get_torrent(infohash) if torrent is None: return torrent.policy = policy torrent._dump_torrent_config() def get_all_rates(self): rates = {} for infohash, torrent in self.torrents.iteritems(): rates[infohash] = (torrent.get_uprate() or 0, torrent.get_downrate() or 0) return rates def get_variance(self): return self.bandwidth_manager.current_std, self.bandwidth_manager.max_std def get_total_rates(self): u = 0.0 d = 0.0 for torrent in self.torrents.itervalues(): u += torrent.get_uprate() or 0 d += torrent.get_downrate() or 0 return u, d def get_total_totals(self): u = 0.0 d = 0.0 for torrent in self.torrents.itervalues(): u += torrent.get_uptotal() or 0 d += torrent.get_downtotal() or 0 return u, d def auto_update_status(self): if self.auto_update_policy_index is not None: aub = self.policies[self.auto_update_policy_index] return aub.get_auto_update_status() return None, None, None def remove_auto_updates_except(self, infohash): for t in self.torrents.values(): if t.is_auto_update and t.metainfo.infohash != infohash: self.logger.warning(_("Cleaning up old autoupdate %s") % t.metainfo.name) self.remove_torrent(t.metainfo.infohash, del_files=True) ## singletorrent callbacks def started(self, torrent): torrent.logger.debug("started torrent") assert torrent.infohash in self.torrents torrent._dump_torrent_config() for policy in self.policies: policy.started(torrent) def failed(self, torrent): torrent.logger.debug("torrent failed") if torrent.infohash not in self.running: return del self.running[torrent.infohash] t = self.get_torrent(torrent.infohash) for policy in self.policies: policy.failed(t) def finishing(self, torrent): torrent.logger.debug("torrent finishing") t = self.get_torrent(torrent.infohash) def finished(self, torrent): torrent.logger.debug("torrent finished") t = self.get_torrent(torrent.infohash) t._dump_torrent_config() for policy in self.policies: policy.finished(t) def exception(self, torrent, text): torrent.logger.debug("torrent threw exception: " + text) if torrent.infohash not in self.torrents: return for policy in self.policies: policy.exception(torrent, text) def error(self, torrent, level, text): torrent.logger.log(level, text) if torrent.infohash not in self.torrents: return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -