Exemplo n.º 1
0
    def GetBandwidthStringsAndGaugeTuples(self,
                                          bandwidth_tracker,
                                          threshold=600):

        with self._lock:

            rows = []

            rules_sorted = list(self._rules)

            def key((bandwidth_type, time_delta, max_allowed)):

                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
Exemplo n.º 2
0
def StreamResponseToFile(job_key, response, f):

    if 'content-length' in response.headers:

        gauge_range = int(response.headers['content-length'])

    else:

        gauge_range = None

    gauge_value = 0

    try:

        for chunk in response.iter_content(chunk_size=65536):

            (i_paused, should_quit) = job_key.WaitIfNeeded()

            if should_quit:

                raise HydrusExceptions.CancelledException()

            f.write(chunk)

            gauge_value += len(chunk)

            if gauge_range is None:

                text = 'downloading - ' + HydrusData.ConvertIntToBytes(
                    gauge_value)

            else:

                text = 'downloading - ' + HydrusData.ConvertValueRangeToBytes(
                    gauge_value, gauge_range)

            job_key.SetVariable('popup_download',
                                (text, gauge_value, gauge_range))

    finally:

        job_key.DeleteVariable('popup_download')
Exemplo n.º 3
0
    def _Update(self):

        if self._network_job is None or self._network_job.NoEngineYet():

            self._left_text.SetLabelText('')
            self._right_text.SetLabelText('')
            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.SetLabelText(status_text)

            if not self._download_started and current_speed > 0:

                self._download_started = True

            if self._download_started and not self._network_job.HasError():

                speed_text = ''

                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.ConvertIntToBytes(bytes_read)

                if current_speed != bytes_to_read:  # if it is a real quick download, just say its size

                    speed_text += ' ' + HydrusData.ConvertIntToBytes(
                        current_speed) + '/s'

                self._right_text.SetLabelText(speed_text)

            else:

                self._right_text.SetLabelText('')

            self._gauge.SetRange(bytes_to_read)
            self._gauge.SetValue(bytes_read)

        if can_cancel:

            if not self._cancel_button.IsEnabled():

                self._cancel_button.Enable()

        else:

            if self._cancel_button.IsEnabled():

                self._cancel_button.Disable()