📄 ui.py
字号:
# because it came from a website which set # Pragma:No-Cache on it. # See GetTorrent.get_quietly(). df = self.multitorrent.torrent_known(metainfo.infohash) yield df known = df.getResult() if known: self.torrent_already_open(metainfo) else: df = self.open_torrent_metainfo(metainfo) if df is not None: yield df try: df.getResult() except TorrentAlreadyInQueue: pass except TorrentAlreadyRunning: pass self.open_external_torrents_deferred = None def open_external_torrents(self): """Open torrents added externally (on the command line before startup).""" if self.open_external_torrents_deferred is None and \ len(self.external_torrents): self.open_external_torrents_deferred = launch_coroutine(self.gui_wrap, self._open_external_torrents) def callback(*a): self.open_external_torrents_deferred = None def errback(e): callback() self.logger.error("open_external_torrents failed:", exc_info=e) self.open_external_torrents_deferred.addCallback(callback) self.open_external_torrents_deferred.addErrback(errback) def torrent_already_open(self, metainfo): """Tell the user.""" raise NotImplementedError('BasicApp.torrent_already_open() not implemented') def open_torrent_metainfo(self, metainfo): """Get a valid save path from the user, and then tell multitorrent to create a new torrent from metainfo.""" raise NotImplementedError('BasicApp.open_torrent_metainfo() not implemented') def launch_torrent(self, infohash): """Launch the torrent contents according to operating system.""" if self.torrents.has_key(infohash): torrent = self.torrents[infohash] if torrent.metainfo.is_batch: LaunchPath.launchdir(torrent.working_path) else: LaunchPath.launchfile(torrent.working_path) def launch_torrent_folder(self, infohash): """Launch the torrent location according to operating system.""" if self.torrents.has_key(infohash): torrent = self.torrents[infohash] if torrent.metainfo.is_batch: LaunchPath.launchdir(torrent.working_path) else: path, file = os.path.split(torrent.working_path) LaunchPath.launchdir(path) def launch_installer_at_exit(self): LaunchPath.launchfile(self.installer_to_launch_at_exit) def do_log(self, severity, text): raise NotImplementedError('BasicApp.do_log() not implemented') def init_updates(self): """Make status request at regular intervals.""" raise NotImplementedError('BasicApp.init_updates() not implemented') def make_statusrequest(self, event = None): """Make status request.""" df = launch_coroutine(self.gui_wrap, self.update_status) def errback(e): self.logger.error("update_status failed", exc_info=e) df.addErrback(errback) return True def update_single_torrent(self, infohash): torrent = self.torrents[infohash] df = self.multitorrent.torrent_status(infohash, torrent.wants_peers(), torrent.wants_files() ) yield df try: core_torrent, statistics = df.getResult() except UnknownInfohash: # looks like it's gone now if infohash in self.torrents: self.torrents.pop(infohash) self.torrent_removed(infohash) else: # the infohash might have been removed from torrents # while we were yielding above, so we need to check if infohash in self.torrents: core_torrent = ThreadProxy(core_torrent, self.gui_wrap) torrent.update(core_torrent, statistics) self.update_torrent(torrent) def update_status(self): """Update torrent information based on the results of making a status request.""" df = self.multitorrent.get_torrents() yield df torrents = df.getResult() infohashes = set() au_torrents = {} for torrent in torrents: torrent = ThreadProxy(torrent, self.gui_wrap) infohashes.add(torrent.metainfo.infohash) if (not torrent.hidden and torrent.metainfo.infohash not in self.torrents): # create new torrent widget to = self.new_displayed_torrent(torrent) if torrent.is_auto_update: au_torrents[torrent.metainfo.infohash] = torrent for infohash, torrent in copy(self.torrents).iteritems(): # remove nonexistent torrents if infohash not in infohashes: self.torrents.pop(infohash) self.torrent_removed(infohash) total_completion = 0 total_bytes = 0 for infohash, torrent in copy(self.torrents).iteritems(): # update existing torrents df = self.multitorrent.torrent_status(infohash, torrent.wants_peers(), torrent.wants_files() ) yield df try: core_torrent, statistics = df.getResult() except UnknownInfohash: # looks like it's gone now if infohash in self.torrents: self.torrents.pop(infohash) self.torrent_removed(infohash) else: # the infohash might have been removed from torrents # while we were yielding above, so we need to check if infohash in self.torrents: core_torrent = ThreadProxy(core_torrent, self.gui_wrap) torrent.update(core_torrent, statistics) self.update_torrent(torrent) if statistics['fractionDone'] is not None: amount_done = statistics['fractionDone'] * torrent.metainfo.total_bytes total_completion += amount_done total_bytes += torrent.metainfo.total_bytes all_completed = False if total_bytes == 0: average_completion = 0 else: average_completion = total_completion / total_bytes if total_completion == total_bytes: all_completed = True df = self.multitorrent.auto_update_status() yield df available_version, installable_version, delay = df.getResult() if available_version is not None: if installable_version is None: self.notify_of_new_version(available_version) else: if self.installer_to_launch_at_exit is None: atexit.register(self.launch_installer_at_exit) if installable_version not in au_torrents: df = self.multitorrent.get_torrent(installable_version) yield df torrent = df.getResult() torrent = ThreadProxy(torrent, self.gui_wrap) else: torrent = au_torrents[installable_version] self.installer_to_launch_at_exit = torrent.working_path if bttime() > self.next_autoupdate_nag: self.prompt_for_quit_for_new_version(available_version) self.next_autoupdate_nag = bttime() + delay def get_global_stats(mt): stats = {} u, d = mt.get_total_rates() stats['total_uprate'] = Rate(u) stats['total_downrate'] = Rate(d) u, d = mt.get_total_totals() stats['total_uptotal'] = Size(u) stats['total_downtotal'] = Size(d) torrents = mt.get_visible_torrents() running = mt.get_visible_running() stats['num_torrents'] = len(torrents) stats['num_running_torrents'] = len(running) stats['num_connections'] = 0 for t in torrents: stats['num_connections'] += t.get_num_connections() try: stats['avg_connections'] = (stats['num_connections'] / stats['num_running_torrents']) except ZeroDivisionError: stats['avg_connections'] = 0 stats['avg_connections'] = "%.02f" % stats['avg_connections'] return stats df = self.multitorrent.call_with_obj(get_global_stats) yield df global_stats = df.getResult() yield average_completion, all_completed, global_stats def _update_status(self, total_completion): raise NotImplementedError('BasicApp._update_status() not implemented') def new_displayed_torrent(self, torrent): """Tell the UI that it should draw a new torrent.""" torrent_object = self.torrent_object_class(torrent) self.torrents[torrent.metainfo.infohash] = torrent_object return torrent_object def torrent_removed(self, infohash): """Tell the GUI that a torrent has been removed, by it, or by multitorrent.""" raise NotImplementedError('BasicApp.torrent_removed() removing missing torrents not implemented') def update_torrent(self, torrent_object): """Tell the GUI to update a torrent's info.""" raise NotImplementedError('BasicApp.update_torrent() updating existing torrents not implemented') def notify_of_new_version(self, version): print 'got auto_update_status', version pass def prompt_for_quit_for_new_version(self, version): print 'got new version', version pass # methods that are used to send commands to MultiTorrent def send_config(self, option, value, infohash=None): """Tell multitorrent to set a config item.""" self.config[option] = value if self.multitorrent: self.multitorrent.set_option(option, value, infohash) def remove_infohash(self, infohash, del_files=False): """Tell multitorrent to remove a torrent.""" df = self.multitorrent.remove_torrent(infohash, del_files=del_files) yield df try: df.getResult() except KeyError: pass # it was already gone, who cares if infohash in self.torrents: self.torrent_removed(infohash) tw = self.torrents.pop(infohash) tw.clean_up() def set_file_priority(self, infohash, filenames, dowhat): """Tell multitorrent to set file priorities.""" for f in filenames: self.multitorrent.set_file_priority(infohash, f, dowhat) def stop_torrent(self, infohash, pause=False): """Tell multitorrent to stop a torrent.""" torrent = self.torrents[infohash] if (torrent and torrent.pending == None): torrent.pending = "stop" if not pause: df = self.multitorrent.set_torrent_policy(infohash, "stop") yield df try: df.getResult() except TorrentNotRunning: pass if torrent.state == "running": df = self.multitorrent.stop_torrent(infohash, pause=pause) yield df torrent.state = df.getResult() torrent.pending = None yield True def start_torrent(self, infohash): """Tell multitorrent to start a torrent.""" torrent = self.torrents[infohash] if (torrent and torrent.pending == None and torrent.state in ["failed", "initialized"]): torrent.pending = "start" if torrent.state == "failed": df = self.multitorrent.reinitialize_torrent(infohash) yield df df.getResult() df = self.multitorrent.set_torrent_policy(infohash, "auto") yield df df.getResult() torrent.pending = None yield True def force_start_torrent(self, infohash): torrent = self.torrents[infohash] if (torrent and torrent.pending == None): torrent.pending = "force start" df = self.multitorrent.set_torrent_policy(infohash, "start") yield df df.getResult() if torrent.state in ["failed", "initialized"]: if torrent.state == "failed": df = self.multitorrent.reinitialize_torrent(infohash) yield df df.getResult() df = self.multitorrent.start_torrent(infohash) yield df try: torrent.state = df.getResult() except TorrentAlreadyRunning: torrent.state = "running" torrent.pending = None yield True def external_command(self, action, *datas): """For communication via IPC""" datas = [ d.decode('utf-8') for d in datas ] if action == 'start_torrent': assert len(datas) == 1 self.append_external_torrents(*datas) self.logger.info('got external_command:start_torrent: "%s"' % datas[0]) # this call does Ye Olde Threadede Deferrede: self.open_external_torrents() elif action == 'publish_torrent': self.logger.info('got external_command:publish_torrent: "%s" as "%s"' % datas) launch_coroutine(self.gui_wrap, self.publish_torrent, datas[0], datas[1]) elif action == 'show_error': assert len(datas) == 1 self.logger.error(datas[0]) elif action == 'no-op': self.logger.info('got external_command: no-op') else: self.logger.warning('got unknown external_command: %s' % str(action)) # fun. #code = action + ' '.join(datas) #self.logger.warning('eval: %s' % code) #exec code
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -