def check_torrent_health(self, data_item, forced=False): # ACHTUNG: The health check can be triggered multiple times for a single infohash # by e.g. selection and click signals if not dict_item_is_any_of(data_item, 'type', [REGULAR_TORRENT]): return infohash = data_item['infohash'] if Column.HEALTH not in self.model.column_position: return # Check if the entry still exists in the table row = self.model.item_uid_map.get(infohash) items = self.model.data_items if row is None or row >= len(items): return data_item = items[row] if not forced and data_item.get('health', HEALTH_UNCHECKED) != HEALTH_UNCHECKED: return data_item['health'] = HEALTH_CHECKING health_cell_index = self.model.index( row, self.model.column_position[Column.HEALTH]) self.model.dataChanged.emit(health_cell_index, health_cell_index, []) TriblerNetworkRequest( f"metadata/torrents/{infohash}/health", self.on_health_response, url_params={ "nowait": True, "refresh": True }, capture_core_errors=False, priority=QNetworkRequest.LowPriority, )
def selection_can_be_added_to_channel(self): for row in self.table_view.selectionModel().selectedRows(): data_item = row.model().data_items[row.row()] if dict_item_is_any_of( data_item, 'type', [REGULAR_TORRENT, CHANNEL_TORRENT, COLLECTION_NODE]): return True return False
def test_is_dict_has(): assert not dict_item_is_any_of(None, None, None) assert not dict_item_is_any_of({}, None, None) d = {'k': 'v', 'k1': 'v1'} assert not dict_item_is_any_of(d, 'missed_key', None) assert not dict_item_is_any_of(d, 'missed_key', ['any_value']) assert not dict_item_is_any_of(d, 'k', ['missed_value']) assert not dict_item_is_any_of(d, 'k', ['missed_value', 'missed_value1']) assert dict_item_is_any_of(d, 'k', ['v']) assert dict_item_is_any_of(d, 'k', ['v', 'a']) assert dict_item_is_any_of(d, 'k', ['a', 'v'])
def _on_selection_changed(self, selected, deselected): selected_indices = self.table_view.selectedIndexes() if not selected_indices: self.table_view.clearSelection() return data_item = selected_indices[-1].model().data_items[selected_indices[-1].row()] if not dict_item_is_any_of(data_item, 'type', [REGULAR_TORRENT]): return # Trigger health check if necessary # When the user scrolls the list, we only want to trigger health checks on the line # that the user stopped on, so we do not generate excessive health checks. if data_item['last_tracker_check'] == 0 and data_item.get('health') != HEALTH_CHECKING: if self.healthcheck_cooldown.isActive(): self.healthcheck_cooldown.stop() else: self.check_torrent_health(data_item) self.healthcheck_cooldown.start(HEALTHCHECK_DELAY_MS)