Skip to content

Commit

Permalink
Improve download code
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Mar 26, 2017
1 parent ad26f10 commit 319dd1c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 51 deletions.
31 changes: 0 additions & 31 deletions projectgenerator/libili2pg/downloader.py

This file was deleted.

35 changes: 21 additions & 14 deletions projectgenerator/libili2pg/iliimporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import tempfile
import zipfile

from projectgenerator.libili2pg.downloader import Downloader
from qgis.PyQt.QtCore import QObject, pyqtSignal

ILI2PG_VERSION='3.6.2'
from projectgenerator.utils.qt_utils import download_file

ILI2PG_VERSION = '3.6.2'
ILI2PG_URL = 'http://www.eisenhutinformatik.ch/interlis/ili2pg/ili2pg-{}.zip'.format(ILI2PG_VERSION)


class Configuration(object):
def __init__(self):
self.host = ''
Expand All @@ -23,7 +25,7 @@ def __init__(self):

@property
def uri(self):
uri =[]
uri = []
uri += ['dbname={}'.format(self.database)]
uri += ['user={}'.format(self.user)]
if self.password:
Expand All @@ -34,6 +36,7 @@ def uri(self):

return ' '.join(uri)


class Importer(QObject):
SUCCESS = 0
# TODO: Insert more codes?
Expand All @@ -60,20 +63,24 @@ def run(self):
except FileExistsError:
pass

self.stdout.emit('Downloading ili2pg ...')

downloader = Downloader()
tmpfile = tempfile.NamedTemporaryFile(suffix='.zip', delete=False)
downloader.download(ILI2PG_URL, tmpfile.name)
with zipfile.ZipFile(tmpfile.name, "r") as z:
z.extractall(os.path.join(dir_path, 'bin'))

self.stdout.emit(self.tr('Downloading ili2pg version {version}...'.format(ILI2PG_VERSION)))
download_file(ILI2PG_URL, tmpfile.name, on_progress=lambda received, total: self.stdout.emit('.'))

try:
with zipfile.ZipFile(tmpfile.name, "r") as z:
z.extractall(os.path.join(dir_path, 'bin'))
except zipfile.BadZipFile:
# We will realize soon enough that the files were not extracted
pass

if not os.path.isfile(ili2pg_file):
self.stderr.emit(
'File "{file}" not found. Please download and extract http://www.eisenhutinformatik.ch/interlis/ili2pg/ili2pg-{version}.zip.'.format(
file=ili2pg_file,
version=ILI2PG_VERSION))

self.tr(
'File "{file}" not found. Please download and extract <a href="{ili2pg_url}">{ili2pg_url}</a>.'.format(
file=ili2pg_file,
ili2pg_url=ILI2PG_URL)))

args = ["java"]
args += ["-jar", ili2pg_file]
Expand Down Expand Up @@ -118,4 +125,4 @@ def run(self):
pass

self.process_finished.emit(proc.returncode, result)
return result
return result
67 changes: 61 additions & 6 deletions projectgenerator/utils/qt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,72 @@
"""

import inspect
from qgis.PyQt import QtWidgets
from qgis.PyQt.QtCore import QCoreApplication
from qgis.PyQt.QtWidgets import QFileDialog
from qgis.PyQt.QtCore import (
QCoreApplication,
QObject,
QFile,
QIODevice,
QEventLoop,
QUrl
)
from qgis.PyQt.QtNetwork import QNetworkRequest
from qgis.core import QgsNetworkAccessManager
from functools import partial
import importlib
from qgis.PyQt.QtCore import qVersion


def selectFolder(line_edit_widget, title, file_filter, parent):
filename, matched_filter = QtWidgets.QFileDialog.getOpenFileName(parent, title, line_edit_widget.text(), file_filter)
filename, matched_filter = QFileDialog.getOpenFileName(parent, title, line_edit_widget.text(), file_filter)
line_edit_widget.setText(filename)


def make_file_selector(widget, title=QCoreApplication.translate('projectgenerator', 'Open File'), file_filter=QCoreApplication.translate('projectgenerator', 'Any file(*)'), parent=None):
return partial(selectFolder, line_edit_widget=widget, title=title, file_filter=file_filter, parent=parent)


class NetworkError(RuntimeError):
def __init__(self, error_code, msg):
self.msg = msg
self.error_code = error_code


def download_file(url, filename, on_progress=None):
"""
Will download the file from url to a local filename.
The method will only return once it's finished.
While downloading it will repeatedly report progress by calling on_progress
with two parameters bytes_received and bytes_total.
If an error occurs, it raises a NetworkError exception.
It will return the filename if everything was ok.
"""
network_access_manager = QgsNetworkAccessManager.instance()

req = QNetworkRequest(QUrl(url))
reply = network_access_manager.get(req)

def on_download_progress(bytes_received, bytes_total):
on_progress(bytes_received, bytes_total)

def finished():
print('Download finished {} ({})'.format(filename, reply.error()))
file = QFile(filename)
file.open(QIODevice.WriteOnly)
file.write(reply.readAll())
file.close()

if on_progress:
reply.downloadProgress.connect(on_download_progress)

reply.finished.connect(finished)

loop = QEventLoop()
reply.finished.connect(loop.quit)
loop.exec_()
reply.deleteLater()

if reply.error():
raise NetworkError(reply.error(), reply.errorMessage)
else:
return filename

0 comments on commit 319dd1c

Please sign in to comment.