def runChole(commands, progress=None): cwd = os.path.dirname(__file__) + os.sep + 'Chloe2012' if progress is None: progress = SilentProgress() fused_command = ' '.join(commands) progress.setInfo('Command:') progress.setCommand(fused_command) progress.setInfo('Output:') # Execution Chloe with subprocess command system success = False retry_count = 0 while success == False: loglines = [] loglines.append('Execution console output :') try: process = subprocess.Popen( fused_command, shell=True, stdout=subprocess.PIPE, stdin=open(os.devnull), stderr=subprocess.STDOUT, universal_newlines=True, cwd=cwd, ) regex = re.compile( r'^\s*##\s*(?P<percentage>\d+)\s*\/\s*\d+\s*$') while True: output = process.stdout.readline() if output == '' and process.poll() is not None: break if output: line = output.strip() res = regex.search(line) if res: percentage = int(res.group('percentage')) progress.setPercentage(percentage) else: progress.setConsoleInfo(line) loglines.append(line) rc = process.poll() success = True except IOError as e: if retry_count < 5: retry_count += 1 else: raise IOError( e.message + u'\nTried 5 times without success. Last iteration stopped after reading {} line(s).\nLast line(s):\n{}' .format(len(loglines), u'\n'.join(loglines[-10:]))) # Save log ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) ChloeUtils.consoleOutput = loglines
def handleAlgorithmResults(alg, progress=None, showResults=True): wrongLayers = [] htmlResults = False if progress is None: progress = SilentProgress() progress.setText(QCoreApplication.translate('Postprocessing', '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 hasattr(out, "layer") and out.layer is not None: out.layer.setLayerName(out.description) QgsMapLayerRegistry.instance().addMapLayers([out.layer]) else: if ProcessingConfig.getSetting( ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(out.value) else: name = out.description isRaster = True if isinstance(out, OutputRaster) else False dataobjects.load(out.value, name, alg.crs, RenderingStyles.getStyle(alg.commandLineName(), out.name), isRaster) except Exception: ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, "Error loading result layer:\n" + traceback.format_exc()) wrongLayers.append(out.description) elif isinstance(out, OutputFile) and out.ext == 'csv': try: dataobjects.load(out.value, name, alg.crs, RenderingStyles.getStyle(alg.commandLineName(), out.name)) except Exception: ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, "Error loading result layer:\n" + traceback.format_exc()) wrongLayers.append(out.description) elif isinstance(out, OutputHTML): ProcessingResults.addResult(out.description, out.value) htmlResults = True i += 1 QApplication.restoreOverrideCursor() if wrongLayers: msg = "The following layers were not correctly generated.<ul>" msg += "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += "You can check the log messages to find more information about the execution of the algorithm" progress.error(msg) if showResults and htmlResults and not wrongLayers: dlg = ResultsDialog() dlg.exec_() return len(wrongLayers) == 0
def handleAlgorithmResults(alg, progress=None, showResults=True): wrongLayers = [] htmlResults = False if progress is None: progress = SilentProgress() progress.setText(QCoreApplication.translate('Postprocessing', '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 hasattr(out, "layer") and out.layer is not None: out.layer.setLayerName(out.description) QgsMapLayerRegistry.instance().addMapLayers([out.layer]) else: if ProcessingConfig.getSetting( ProcessingConfig.USE_FILENAME_AS_LAYER_NAME): name = os.path.basename(out.value) else: name = out.description isRaster = True if isinstance(out, OutputRaster) else False dataobjects.load(out.value, name, alg.crs, RenderingStyles.getStyle(alg.commandLineName(), out.name), isRaster) except Exception: ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, "Error loading result layer:\n" + traceback.format_exc()) wrongLayers.append(out.description) elif isinstance(out, OutputHTML): ProcessingResults.addResult(out.description, out.value) htmlResults = True i += 1 QApplication.restoreOverrideCursor() if wrongLayers: msg = "The following layers were not correctly generated.<ul>" msg += "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>" msg += "You can check the log messages to find more information about the execution of the algorithm" progress.error(msg) if showResults and htmlResults and not wrongLayers: dlg = ResultsDialog() dlg.exec_() return len(wrongLayers) == 0