Skip to content

Commit

Permalink
Use QProcess instead of subprocess
Browse files Browse the repository at this point in the history
Fix #21
  • Loading branch information
m-kuhn committed Apr 27, 2017
1 parent 12e2e0e commit ac87db1
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions projectgenerator/libili2pg/iliimporter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import subprocess
import os

import re
import tempfile
import zipfile

from qgis.PyQt.QtCore import QObject, pyqtSignal
import functools

from qgis.PyQt.QtCore import QObject, pyqtSignal, QProcess, QEventLoop

from projectgenerator.utils.qt_utils import download_file

Expand All @@ -23,7 +24,7 @@ def __init__(self):
self.ilifile = ''
self.port = ''
self.inheritance = 'smart1'
self.epsg = 21781 # Default EPSG code in ili2pg
self.epsg = 21781 # Default EPSG code in ili2pg

@property
def uri(self):
Expand All @@ -38,9 +39,11 @@ def uri(self):

return ' '.join(uri)


class JavaNotFoundError(FileNotFoundError):
pass


class Importer(QObject):
SUCCESS = 0
# TODO: Insert more codes?
Expand All @@ -51,6 +54,9 @@ class Importer(QObject):
process_started = pyqtSignal(str)
process_finished = pyqtSignal(int, int)

__done_pattern = None
__result = None

def __init__(self, parent=None):
QObject.__init__(self, parent)
self.filename = None
Expand Down Expand Up @@ -120,35 +126,41 @@ def run(self):

proc = None
for java_path in java_paths:
try:
proc = subprocess.Popen([java_path] + args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
except FileNotFoundError as e:
pass
if proc:
proc = QProcess()
proc.readyReadStandardError.connect(functools.partial(self.stderr_ready, proc=proc))
proc.readyReadStandardOutput.connect(functools.partial(self.stdout_ready, proc=proc))

proc.start(java_path, args)

if not proc.waitForStarted():
proc = None
else:
break

if not proc:
raise JavaNotFoundError()

self.process_started.emit(' '.join(proc.args))
self.process_started.emit(java_path + ' ' + ' '.join(args))

done_pattern = re.compile(r"Info: ...done")
self.__result = Importer.ERROR

result = Importer.ERROR
finished = False
while not finished:
try:
output = proc.communicate(timeout=2)
self.stdout.emit(output[1].decode())
self.stderr.emit(output[0].decode())
if done_pattern.search(output[1].decode()):
result = Importer.SUCCESS
finished = True
except subprocess.TimeoutExpired:
pass
loop = QEventLoop()
proc.finished.connect(loop.exit)
loop.exec()

self.process_finished.emit(proc.exitCode(), self.__result)
return self.__result

def stderr_ready(self, proc):
text = bytes(proc.readAllStandardError()).decode()
if not self.__done_pattern:
self.__done_pattern = re.compile(r"Info: ...done")
if self.__done_pattern.search(text):
self.__result = Importer.SUCCESS

self.stderr.emit(text)
pass

self.process_finished.emit(proc.returncode, result)
return result
def stdout_ready(self, proc):
text = bytes(proc.readAllStandardOutput()).decode()
self.stdout.emit(text)

0 comments on commit ac87db1

Please sign in to comment.