def get(url, retry_count=3):
    """Downoads data from a url and returns it

    Args:
        url (str): The url to download (http, https)
        retry_count (int): The number of retries to attempt if the initial download fails.
                If retry_count is 0 the download will only be attempted once.

    Returns:
        bytes: The downloaded data, None is returned if the download fails
    """
    if get_protocol(url) not in supported_protocols:
        return None
    info(f"Downloading {url}")
    download_attempts_count = 0
    while download_attempts_count <= retry_count:
        try:
            response = urllib.request.urlopen(url)
            data = response.read()
            info(f"Got {url}")
            return data
        except Exception as err:
            download_attempts_count += 1
            error(err)
    return None
示例#2
0
    def translate(self):
        """Try to translate based on languages selected.

        Args:
            showError (bool): If True show an error messagebox if the
                currently selected translation isn't installed
        """
        if len(self.languages) < 1:
            return
        input_text = self.left_textEdit.toPlainText()
        input_combo_value = self.left_language_combo.currentIndex()
        input_language = self.languages[input_combo_value]
        output_combo_value = self.right_language_combo.currentIndex()
        output_language = self.languages[output_combo_value]
        translation = input_language.get_translation(output_language)
        if translation:
            bound_translation_function = partial(translation.translate, input_text)
            show_loading_message = len(input_text) > self.SHOW_LOADING_THRESHOLD
            new_worker_thread = TranslationThread(
                bound_translation_function, show_loading_message
            )
            new_worker_thread.send_text_update.connect(self.update_right_textEdit)
            new_worker_thread.finished.connect(self.handle_worker_thread_finished)
            if self.worker_thread == None:
                self.worker_thread = new_worker_thread
                self.worker_thread.start()
            else:
                self.queued_translation = new_worker_thread

        else:
            error("No translation available for this language pair")
示例#3
0
def update_package_index():
    """Downloads remote package index"""
    with package_lock:
        try:
            response = urllib.request.urlopen(settings.remote_package_index)
        except Exception as err:
            error(err)
            return
        data = response.read()
        with open(settings.local_package_index, "wb") as f:
            f.write(data)