def AddData(self, key, data): with self._lock: if key not in self._keys_to_data: while self._total_estimated_memory_footprint > self._cache_size: self._DeleteItem() self._keys_to_data[key] = data self._TouchKey(key) self._RecalcMemoryUsage() if HG.cache_report_mode: HydrusData.ShowText( 'Cache "{}" adding "{}" ({}). Current size {}.'.format( self._name, key, HydrusData.ToHumanBytes( data.GetEstimatedMemoryFootprint()), HydrusData.ConvertValueRangeToBytes( self._total_estimated_memory_footprint, self._cache_size)))
def GetBandwidthStringsAndGaugeTuples(self, bandwidth_tracker, threshold=600): with self._lock: rows = [] rules_sorted = list(self._rules) def key(rule_tuple): (bandwidth_type, time_delta, max_allowed) = rule_tuple if time_delta is None: return -1 else: return time_delta rules_sorted.sort(key=key) for (bandwidth_type, time_delta, max_allowed) in rules_sorted: time_is_less_than_threshold = time_delta is not None and time_delta <= threshold if time_is_less_than_threshold or max_allowed == 0: continue usage = bandwidth_tracker.GetUsage(bandwidth_type, time_delta) s = 'used ' if bandwidth_type == HC.BANDWIDTH_TYPE_DATA: s += HydrusData.ConvertValueRangeToBytes( usage, max_allowed) elif bandwidth_type == HC.BANDWIDTH_TYPE_REQUESTS: s += HydrusData.ConvertValueRangeToPrettyString( usage, max_allowed) + ' requests' if time_delta is None: s += ' this month' else: s += ' in the past ' + HydrusData.TimeDeltaToPrettyTimeDelta( time_delta) rows.append((s, (usage, max_allowed))) return rows
def _Delete(self, key): if key not in self._keys_to_data: return del self._keys_to_data[key] self._RecalcMemoryUsage() if HG.cache_report_mode: HydrusData.ShowText( 'Cache "{}" removing "{}". Current size {}.'.format( self._name, key, HydrusData.ConvertValueRangeToBytes( self._total_estimated_memory_footprint, self._cache_size)))
def _Update( self ): if self._network_job is None or self._network_job.NoEngineYet(): can_cancel = False self._left_text.clear() self._right_text.clear() self._gauge.SetRange( 1 ) self._gauge.SetValue( 0 ) else: can_cancel = not self._network_job.IsDone() ( status_text, current_speed, bytes_read, bytes_to_read ) = self._network_job.GetStatus() self._left_text.setText( status_text ) speed_text = '' if bytes_read is not None and bytes_read > 0 and not self._network_job.HasError(): if bytes_to_read is not None and bytes_read != bytes_to_read: speed_text += HydrusData.ConvertValueRangeToBytes( bytes_read, bytes_to_read ) else: speed_text += HydrusData.ToHumanBytes( bytes_read ) if current_speed != bytes_to_read: # if it is a real quick download, just say its size speed_text += ' ' + HydrusData.ToHumanBytes( current_speed ) + '/s' show_right_text = speed_text != '' if self._right_text.isVisible() != show_right_text: self._right_text.setVisible( show_right_text ) self.updateGeometry() if speed_text != '': self._right_text.setText( speed_text ) right_width = ClientGUIFunctions.ConvertTextToPixelWidth( self._right_text, len( speed_text ) ) right_min_width = right_width if right_min_width != self._last_right_min_width: self._last_right_min_width = right_min_width self._right_text.setMinimumWidth( right_min_width ) self.updateGeometry() self._gauge.SetRange( bytes_to_read ) self._gauge.SetValue( bytes_read ) if self._cancel_button.isEnabled() != can_cancel: self._cancel_button.setEnabled( can_cancel )
def _Update( self ): if self._network_job is None or self._network_job.NoEngineYet(): self._left_text.setText( '' ) self._right_text.setText( '' ) self._gauge.SetRange( 1 ) self._gauge.SetValue( 0 ) can_cancel = False else: if self._network_job.IsDone(): can_cancel = False else: can_cancel = True ( status_text, current_speed, bytes_read, bytes_to_read ) = self._network_job.GetStatus() self._left_text.setText( status_text ) if not self._download_started and current_speed > 0: self._download_started = True speed_text = '' if self._download_started and not self._network_job.HasError(): if bytes_read is not None: if bytes_to_read is not None and bytes_read != bytes_to_read: speed_text += HydrusData.ConvertValueRangeToBytes( bytes_read, bytes_to_read ) else: speed_text += HydrusData.ToHumanBytes( bytes_read ) if current_speed != bytes_to_read: # if it is a real quick download, just say its size speed_text += ' ' + HydrusData.ToHumanBytes( current_speed ) + '/s' self._right_text.setText( speed_text ) right_width = ClientGUIFunctions.ConvertTextToPixelWidth( self._right_text, len( speed_text ) ) right_min_width = right_width if right_min_width != self._last_right_min_width: self._last_right_min_width = right_min_width self._right_text.setMinimumWidth( right_min_width ) self._gauge.SetRange( bytes_to_read ) self._gauge.SetValue( bytes_read ) if can_cancel: if not self._cancel_button.isEnabled(): self._cancel_button.setEnabled( True ) else: if self._cancel_button.isEnabled(): self._cancel_button.setEnabled( False )