def _download_selected(self): """ Download selected songs, bibles and themes. Returns False on download error """ # Build directories for downloads songs_destination_path = Path(gettempdir(), 'openlp') bibles_destination_path = AppLocation.get_section_data_path('bibles') themes_destination_path = AppLocation.get_section_data_path('themes') missed_files = [] # Download songs for i in range(self.songs_list_widget.count()): item = self.songs_list_widget.item(i) if item.checkState() == QtCore.Qt.Checked: filename, sha256 = item.data(QtCore.Qt.UserRole) self._increment_progress_bar(self.downloading.format(name=filename), 0) self.previous_size = 0 destination = songs_destination_path / str(filename) if not download_file(self, '{path}{name}'.format(path=self.songs_url, name=filename), destination, sha256): missed_files.append('Song: {name}'.format(name=filename)) # Download Bibles bibles_iterator = QtWidgets.QTreeWidgetItemIterator(self.bibles_tree_widget) while bibles_iterator.value(): item = bibles_iterator.value() if item.parent() and item.checkState(0) == QtCore.Qt.Checked: bible, sha256 = item.data(0, QtCore.Qt.UserRole) self._increment_progress_bar(self.downloading.format(name=bible), 0) self.previous_size = 0 if not download_file(self, '{path}{name}'.format(path=self.bibles_url, name=bible), bibles_destination_path / bible, sha256): missed_files.append('Bible: {name}'.format(name=bible)) bibles_iterator += 1 # Download themes for item in self.themes_list_widget.selectedItems(): self._increment_progress_bar(self.downloading.format(name=item.file_name), 0) self.previous_size = 0 if not download_file(self, '{url}{file}'.format(url=self.themes_url, file=item.file_name), themes_destination_path / item.file_name, item.sha256): missed_files.append('Theme: name'.format(name=item.file_name)) if missed_files: file_list = '' for entry in missed_files: file_list += '{text}<br \\>'.format(text=entry) msg = QtWidgets.QMessageBox() msg.setIcon(QtWidgets.QMessageBox.Warning) msg.setWindowTitle(translate('OpenLP.FirstTimeWizard', 'Network Error')) msg.setText(translate('OpenLP.FirstTimeWizard', 'Unable to download some files')) msg.setInformativeText(translate('OpenLP.FirstTimeWizard', 'The following files were not able to be ' 'downloaded:<br \\>{text}'.format(text=file_list))) msg.setStandardButtons(msg.Ok) msg.exec() return True
def test_socket_timeout(self, mocked_requests): """ Test socket timeout gets caught """ # GIVEN: Mocked urlopen to fake a network disconnect in the middle of a download mocked_requests.get.side_effect = OSError # WHEN: Attempt to retrieve a file download_file(MagicMock(), url='http://localhost/test', file_path=Path(self.tempfile)) # THEN: socket.timeout should have been caught # NOTE: Test is if $tmpdir/tempfile is still there, then test fails since ftw deletes bad downloaded files assert os.path.exists(self.tempfile) is False, 'tempfile should have been deleted'
def download_and_check(callback=None): """ Download the web site and deploy it. """ sha256, version = download_sha256() file_size = get_url_file_size('https://get.openlp.org/webclient/site.zip') callback.setRange(0, file_size) if download_file(callback, 'https://get.openlp.org/webclient/site.zip', AppLocation.get_section_data_path('remotes') / 'site.zip', sha256=sha256): deploy_zipfile(AppLocation.get_section_data_path('remotes'), 'site.zip')
def start(self): """ Run the worker """ if self.was_cancelled: return try: download_path = Path(gettempdir()) / 'openlp' / self.screenshot is_success = download_file( self, '{host}{name}'.format(host=self.themes_url, name=self.screenshot), download_path) if is_success and not self.was_cancelled: # Signal that the screenshot has been downloaded self.screenshot_downloaded.emit(self.title, self.filename, self.sha256) except: # noqa log.exception('Unable to download screenshot') finally: self.quit.emit()