Skip to content

Commit

Permalink
[processing] made postprocessing code more pythonic
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Jun 10, 2014
1 parent 9ec1d46 commit 0009040
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 59 deletions.
6 changes: 2 additions & 4 deletions python/plugins/processing/gui/AlgorithmClassification.py
Expand Up @@ -50,10 +50,8 @@ def loadClassification():
lines.close()

@staticmethod
def classificationFile():
folder = os.path.join(os.path.dirname(__file__), 'help')
f = os.path.join(folder, 'algclasssification.txt')
return f
def classificationFile():
return os.path.join(os.path.join(os.path.dirname(__file__), 'algclasssification.txt')

@staticmethod
def getGroupsAndName(alg):
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/gui/AlgorithmExecutionDialog.py
Expand Up @@ -37,7 +37,7 @@
from processing.core.ProcessingLog import ProcessingLog
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.WrongHelpFileException import WrongHelpFileException
from processing.gui.Postprocessing import Postprocessing
from processing.gui.Postprocessing import handleAlgorithmResults
from processing.gui.UnthreadedAlgorithmExecutor import \
UnthreadedAlgorithmExecutor
from processing.parameters.ParameterRaster import ParameterRaster
Expand Down Expand Up @@ -283,7 +283,7 @@ def finish(self):
keepOpen = ProcessingConfig.getSetting(
ProcessingConfig.KEEP_DIALOG_OPEN)
if self.iterateParam is None:
Postprocessing.handleAlgorithmResults(self.alg, self, not keepOpen)
handleAlgorithmResults(self.alg, self, not keepOpen)
self.executed = True
self.setInfo('Algorithm %s finished' % self.alg.name)
QApplication.restoreOverrideCursor()
Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/gui/BatchProcessingDialog.py
Expand Up @@ -29,7 +29,7 @@
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from processing.core.ProcessingResults import ProcessingResults
from processing.gui.Postprocessing import Postprocessing
from processing.gui.Postprocessing import handleAlgorithmResults
from processing.gui.FileSelectionPanel import FileSelectionPanel
from processing.gui.BatchInputSelectionPanel import BatchInputSelectionPanel
from processing.gui.AlgorithmExecutionDialog import AlgorithmExecutionDialog
Expand Down Expand Up @@ -203,7 +203,7 @@ def accept(self):
if UnthreadedAlgorithmExecutor.runalg(alg, self) \
and not self.canceled:
if self.load[i]:
Postprocessing.handleAlgorithmResults(alg, self, False)
handleAlgorithmResults(alg, self, False)
else:
QApplication.restoreOverrideCursor()
return
Expand Down
76 changes: 36 additions & 40 deletions python/plugins/processing/gui/Postprocessing.py
Expand Up @@ -39,45 +39,41 @@
from processing.outputs.OutputHTML import OutputHTML
from processing.tools import dataobjects


class Postprocessing:

@staticmethod
def handleAlgorithmResults(alg, progress, showResults=True):
wrongLayers = []
htmlResults = False
progress.setText('Loading resulting layers')
i = 0
for out in alg.outputs:
progress.setPercentage(100 * i / float(len(alg.outputs)))
if out.hidden or not out.open:
continue
if isinstance(out, (OutputRaster, OutputVector, OutputTable)):
try:
if out.value.startswith('memory:'):
layer = out.memoryLayer
QgsMapLayerRegistry.instance().addMapLayers([layer])
def handleAlgorithmResults(alg, progress, showResults=True):
wrongLayers = []
htmlResults = False
progress.setText('Loading resulting layers')
i = 0
for out in alg.outputs:
progress.setPercentage(100 * i / float(len(alg.outputs)))
if out.hidden or not out.open:
continue
if isinstance(out, (OutputRaster, OutputVector, OutputTable)):
try:
if out.value.startswith('memory:'):
layer = out.memoryLayer
QgsMapLayerRegistry.instance().addMapLayers([layer])
else:
if ProcessingConfig.getSetting(
ProcessingConfig.USE_FILENAME_AS_LAYER_NAME):
name = os.path.basename(out.value)
else:
if ProcessingConfig.getSetting(
ProcessingConfig.USE_FILENAME_AS_LAYER_NAME):
name = os.path.basename(out.value)
else:
name = out.description
dataobjects.load(out.value, name, alg.crs,
RenderingStyles.getStyle(alg.commandLineName(),
out.name))
except Exception, e:
wrongLayers.append(out)
elif isinstance(out, OutputHTML):
ProcessingResults.addResult(out.description, out.value)
htmlResults = True
i += 1
if wrongLayers:
QApplication.restoreOverrideCursor()
dlg = CouldNotLoadResultsDialog(wrongLayers, alg)
dlg.exec_()
name = out.description
dataobjects.load(out.value, name, alg.crs,
RenderingStyles.getStyle(alg.commandLineName(),
out.name))
except Exception, e:
wrongLayers.append(out)
elif isinstance(out, OutputHTML):
ProcessingResults.addResult(out.description, out.value)
htmlResults = True
i += 1
if wrongLayers:
QApplication.restoreOverrideCursor()
dlg = CouldNotLoadResultsDialog(wrongLayers, alg)
dlg.exec_()

if showResults and htmlResults and not wrongLayers:
QApplication.restoreOverrideCursor()
dlg = ResultsDialog()
dlg.exec_()
if showResults and htmlResults and not wrongLayers:
QApplication.restoreOverrideCursor()
dlg = ResultsDialog()
dlg.exec_()
11 changes: 4 additions & 7 deletions python/plugins/processing/gui/UnthreadedAlgorithmExecutor.py
Expand Up @@ -32,7 +32,7 @@
from processing.core.ProcessingLog import ProcessingLog
from processing.core.GeoAlgorithmExecutionException import \
GeoAlgorithmExecutionException
from processing.gui.Postprocessing import Postprocessing
from processing.gui.Postprocessing import handleAlgorithmResults
from processing.tools import dataobjects
from processing.tools.system import *
from processing.tools import vector
Expand Down Expand Up @@ -87,9 +87,8 @@ def runalgIterating(alg, paramToIter, progress):
for out in alg.outputs:
outputs[out.name] = out.value

# now run all the algorithms
i = 1
for f in filelist:
# now run all the algorithms
for i,f in enumerate(filelist):
alg.setParameterValue(paramToIter, f)
for out in alg.outputs:
filename = outputs[out.name]
Expand All @@ -101,9 +100,7 @@ def runalgIterating(alg, paramToIter, progress):
+ str(len(filelist)) + '...')
progress.setPercentage(i * 100 / len(filelist))
if UnthreadedAlgorithmExecutor.runalg(alg, SilentProgress()):
Postprocessing.handleAlgorithmResults(alg, SilentProgress(),
False)
i += 1
handleAlgorithmResults(alg, SilentProgress(), False)
else:
return False

Expand Down
6 changes: 2 additions & 4 deletions python/plugins/processing/tools/general.py
Expand Up @@ -27,7 +27,7 @@

from qgis.core import *
from processing.core.Processing import Processing
from processing.gui.Postprocessing import Postprocessing
from processing.gui.Postprocessing import handleAlgorithmResults
from processing.parameters.ParameterSelection import ParameterSelection


Expand Down Expand Up @@ -74,6 +74,4 @@ def runalg(algOrName, *args):


def runandload(name, *args):
return Processing.runAlgorithm(name,
Postprocessing.handleAlgorithmResults,
*args)
return Processing.runAlgorithm(name, handleAlgorithmResults, *args)

0 comments on commit 0009040

Please sign in to comment.