Esempio n. 1
0
 def getAlgorithmFromHookFile(self, hookFile):
     if hookFile.endswith("py"):
         script = ScriptAlgorithm(hookFile)
         script.provider = ModelerUtils.providers["script"]
         return script
     elif hookFile.endswith("model"):
         model = ModelerAlgorithm()
         model.openModel(hookFile)
         model.provider = ModelerUtils.providers["model"]
         return model
     else:
         raise Exception("Wrong hook file")
Esempio n. 2
0
 def getAlgorithmFromHookFile(self, hookFile):
     if hookFile.endswith('py'):
         script = ScriptAlgorithm(hookFile)
         script.provider = Providers.providers['script']
         return script
     elif hookFile.endswith('model'):
         model = ModelerAlgorithm()
         model.openModel(hookFile)                
         model.provider = Providers.providers['model']
         return model
     else:
         raise Exception ("Wrong hook file")
Esempio n. 3
0
    def check_algorithm(self, name, defs):
        """
        Will run an algorithm definition and check if it generates the expected result
        :param name: The identifier name used in the test output heading
        :param defs: A python dict containing a test algorithm definition
        """
        QgsProject.instance().removeAllMapLayers()

        params = self.load_params(defs['params'])

        if defs['algorithm'].startswith('script:'):
            filePath = os.path.join(processingTestDataPath(), 'scripts', '{}.py'.format(defs['algorithm'][len('script:'):]))
            alg = ScriptAlgorithm(filePath)
        else:
            alg = QgsApplication.processingRegistry().algorithmById(defs['algorithm'])

        parameters = {}
        if isinstance(params, list):
            for param in zip(alg.parameterDefinitions(), params):
                parameters[param[0].name()] = param[1]
        else:
            for k, p in list(params.items()):
                parameters[k] = p

        for r, p in list(defs['results'].items()):
            if not 'in_place_result' in p or not p['in_place_result']:
                parameters[r] = self.load_result_param(p)

        expectFailure = False
        if 'expectedFailure' in defs:
            exec(('\n'.join(defs['expectedFailure'][:-1])), globals(), locals())
            expectFailure = eval(defs['expectedFailure'][-1])

        # ignore user setting for invalid geometry handling
        context = QgsProcessingContext()
        context.setProject(QgsProject.instance())
        feedback = QgsProcessingFeedback()

        if expectFailure:
            try:
                results, ok = alg.run(parameters, context, feedback)
                self.check_results(results, context, defs['params'], defs['results'])
                if ok:
                    raise _UnexpectedSuccess
            except Exception:
                pass
        else:
            results, ok = alg.run(parameters, context, feedback)
            self.assertTrue(ok, parameters)
            self.check_results(results, context, defs['params'], defs['results'])
Esempio n. 4
0
    def runAlgorithm(self):
        if self.algType == self.SCRIPT_PYTHON:
            alg = ScriptAlgorithm(None, self.editor.text())

        dlg = alg.createCustomParametersWidget(self)
        if not dlg:
            dlg = AlgorithmDialog(alg)

        canvas = iface.mapCanvas()
        prevMapTool = canvas.mapTool()

        dlg.show()
        dlg.exec_()

        if canvas.mapTool() != prevMapTool:
            try:
                canvas.mapTool().reset()
            except:
                pass
            canvas.setMapTool(prevMapTool)
Esempio n. 5
0
 def loadFromFolder(folder):
     if not os.path.exists(folder):
         return []
     algs = []
     for path, subdirs, files in os.walk(folder):
         for descriptionFile in files:
             if descriptionFile.endswith('py'):
                 try:
                     fullpath = os.path.join(path, descriptionFile)
                     alg = ScriptAlgorithm(fullpath)
                     if alg.name().strip() != '':
                         algs.append(alg)
                 except WrongScriptException as e:
                     ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, e.msg)
                 except Exception as e:
                     ProcessingLog.addToLog(
                         ProcessingLog.LOG_ERROR,
                         QCoreApplication.translate('Processing', 'Could not load script: {0}\n{1}').format(descriptionFile, str(e))
                     )
     return algs
Esempio n. 6
0
    def editHelp(self):
        if self.alg is None:
            if self.algType == self.SCRIPT_PYTHON:
                alg = ScriptAlgorithm(None, self.editor.text())
            elif self.algType == self.SCRIPT_R:
                alg = RAlgorithm(None, self.editor.text())
        else:
            alg = self.alg

        dlg = HelpEditionDialog(alg)
        dlg.exec_()
        if dlg.descriptions:
            self.help = dlg.descriptions
            self.setHasChanged(True)
Esempio n. 7
0
    def check_algorithm(self, name, defs):
        """
        Will run an algorithm definition and check if it generates the expected result
        :param name: The identifier name used in the test output heading
        :param defs: A python dict containing a test algorithm definition
        """
        self.vector_layer_params = {}
        QgsProject.instance().removeAllMapLayers()

        params = self.load_params(defs['params'])

        if defs['algorithm'].startswith('script:'):
            filePath = os.path.join(processingTestDataPath(), 'scripts', '{}.py'.format(defs['algorithm'][len('script:'):]))
            alg = ScriptAlgorithm(filePath)
            alg.initAlgorithm()
        else:
            alg = QgsApplication.processingRegistry().createAlgorithmById(defs['algorithm'])

        parameters = {}
        if isinstance(params, list):
            for param in zip(alg.parameterDefinitions(), params):
                parameters[param[0].name()] = param[1]
        else:
            for k, p in list(params.items()):
                parameters[k] = p

        for r, p in list(defs['results'].items()):
            if not 'in_place_result' in p or not p['in_place_result']:
                parameters[r] = self.load_result_param(p)

        expectFailure = False
        if 'expectedFailure' in defs:
            exec(('\n'.join(defs['expectedFailure'][:-1])), globals(), locals())
            expectFailure = eval(defs['expectedFailure'][-1])

        # ignore user setting for invalid geometry handling
        context = QgsProcessingContext()
        context.setProject(QgsProject.instance())
        feedback = QgsProcessingFeedback()

        if expectFailure:
            try:
                results, ok = alg.run(parameters, context, feedback)
                self.check_results(results, context, defs['params'], defs['results'])
                if ok:
                    raise _UnexpectedSuccess
            except Exception:
                pass
        else:
            results, ok = alg.run(parameters, context, feedback)
            self.assertTrue(ok, 'params: {}, results: {}'.format(parameters, results))
            self.check_results(results, context, defs['params'], defs['results'])
Esempio n. 8
0
    def runAlgorithm(self):
        if self.algType == self.SCRIPT_PYTHON:
            alg = ScriptAlgorithm(None, unicode(self.editor.text()))
            alg.provider = Providers.providers["script"]
        if self.algType == self.SCRIPT_R:
            alg = RAlgorithm(None, unicode(self.editor.text()))
            alg.provider = Providers.providers["r"]

        dlg = alg.getCustomParametersDialog()
        if not dlg:
            dlg = ParametersDialog(alg)

        canvas = iface.mapCanvas()
        prevMapTool = canvas.mapTool()

        dlg.show()
        dlg.exec_()

        if canvas.mapTool() != prevMapTool:
            try:
                canvas.mapTool().reset()
            except:
                pass
            canvas.setMapTool(prevMapTool)
Esempio n. 9
0
    def runAlgorithm(self):
        if self.algType == self.SCRIPT_PYTHON:
            alg = ScriptAlgorithm(None, self.editor.text())

        dlg = alg.createCustomParametersWidget(self)
        if not dlg:
            dlg = AlgorithmDialog(alg)

        canvas = iface.mapCanvas()
        prevMapTool = canvas.mapTool()

        dlg.show()
        dlg.exec_()

        # have to manually delete the dialog - otherwise it's owned by the
        # iface mainWindow and never deleted
        dlg.deleteLater()

        if canvas.mapTool() != prevMapTool:
            try:
                canvas.mapTool().reset()
            except:
                pass
            canvas.setMapTool(prevMapTool)
Esempio n. 10
0
    def runAlgorithm(self):
        if self.algType == self.SCRIPT_PYTHON:
            alg = ScriptAlgorithm(None, self.editor.text())
            alg.provider = QgsApplication.processingRegistry().providerById('script')
        if self.algType == self.SCRIPT_R:
            alg = RAlgorithm(None, self.editor.text())
            alg.provider = QgsApplication.processingRegistry().providerById('r')

        dlg = alg.getCustomParametersDialog()
        if not dlg:
            dlg = AlgorithmDialog(alg)

        canvas = iface.mapCanvas()
        prevMapTool = canvas.mapTool()

        dlg.show()
        dlg.exec_()

        if canvas.mapTool() != prevMapTool:
            try:
                canvas.mapTool().reset()
            except:
                pass
            canvas.setMapTool(prevMapTool)
Esempio n. 11
0
    def runAlgorithm(self):
        if self.algType == self.SCRIPT_PYTHON:
            alg = ScriptAlgorithm(None, self.editor.text())

        dlg = alg.createCustomParametersWidget(self)
        if not dlg:
            dlg = AlgorithmDialog(alg)

        canvas = iface.mapCanvas()
        prevMapTool = canvas.mapTool()

        dlg.show()
        dlg.exec_()

        # have to manually delete the dialog - otherwise it's owned by the
        # iface mainWindow and never deleted
        dlg.deleteLater()

        if canvas.mapTool() != prevMapTool:
            try:
                canvas.mapTool().reset()
            except:
                pass
            canvas.setMapTool(prevMapTool)
Esempio n. 12
0
    def editHelp(self):
        if self.alg is None:
            if self.algType == self.SCRIPT_PYTHON:
                alg = ScriptAlgorithm(None, unicode(self.editor.text()))
            elif self.algType == self.SCRIPT_R:
                alg = RAlgorithm(None, unicode(self.editor.text()))
        else:
            alg = self.alg

        dlg = HelpEditionDialog(alg)
        dlg.exec_()

        # We store the description string in case there were not saved
        # because there was no filename defined yet
        if self.alg is None and dlg.descriptions:
            self.help = dlg.descriptions
Esempio n. 13
0
 def loadFromFolder(self, folder):
     if not os.path.exists(folder):
         return
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith('py'):
             try:
                 fullpath = os.path.join(folder, descriptionFile)
                 alg = ScriptAlgorithm(fullpath)
                 if alg.name.strip() != '':
                     self.algs.append(alg)
             except WrongScriptException, e:
                 ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, e.msg)
             except Exception, e:
                 ProcessingLog.addToLog(
                     ProcessingLog.LOG_ERROR, 'Could not load script:' +
                     descriptionFile + '\n' + unicode(e))
Esempio n. 14
0
 def execute(self):
     filename = QtGui.QFileDialog.getOpenFileName(self.toolbox,
                                                  'Script files', None,
                                                  '*.py')
     if filename:
         try:
             script = ScriptAlgorithm(filename)
         except WrongScriptException:
             QtGui.QMessageBox.warning(
                 self.toolbox, "Error reading script",
                 "The selected file does not contain a valid script")
             return
         destFilename = os.path.join(ScriptUtils.scriptsFolder(),
                                     os.path.basename(filename))
         with open(destFilename, "w") as f:
             f.write(script.script)
         self.toolbox.updateProvider('script')
Esempio n. 15
0
 def loadFromFolder(folder):
     if not os.path.exists(folder):
         return []
     algs = []
     for path, subdirs, files in os.walk(folder):
         for descriptionFile in files:
             if descriptionFile.endswith('py'):
                 try:
                     fullpath = os.path.join(path, descriptionFile)
                     alg = ScriptAlgorithm(fullpath)
                     if alg.name.strip() != '':
                         algs.append(alg)
                 except WrongScriptException, e:
                     ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, e.msg)
                 except Exception, e:
                     ProcessingLog.addToLog(
                         ProcessingLog.LOG_ERROR, 'Could not load script:' +
                         descriptionFile + '\n' + unicode(e))
Esempio n. 16
0
 def execute(self):
     settings = QSettings()
     lastDir = settings.value('Processing/lastScriptsDir', '')
     filename, selected_filter = QFileDialog.getOpenFileName(self.toolbox,
                                                             self.tr('Script files', 'AddScriptFromFileAction'), lastDir,
                                                             self.tr('Script files (*.py *.PY)', 'AddScriptFromFileAction'))
     if filename:
         try:
             settings.setValue('Processing/lastScriptsDir',
                               QFileInfo(filename).absoluteDir().absolutePath())
             script = ScriptAlgorithm(filename)
         except WrongScriptException:
             QMessageBox.warning(self.toolbox,
                                 self.tr('Error reading script', 'AddScriptFromFileAction'),
                                 self.tr('The selected file does not contain a valid script', 'AddScriptFromFileAction'))
             return
         destFilename = os.path.join(ScriptUtils.scriptsFolders()[0], os.path.basename(filename))
         with open(destFilename, 'w') as f:
             f.write(script.script)
         algList.reloadProvider('script')
Esempio n. 17
0
 def loadFromFolder(folder):
     if not os.path.exists(folder):
         return []
     algs = []
     for path, subdirs, files in os.walk(folder):
         for descriptionFile in files:
             if descriptionFile.endswith('py'):
                 try:
                     fullpath = os.path.join(path, descriptionFile)
                     alg = ScriptAlgorithm(fullpath)
                     if alg.name.strip() != '':
                         algs.append(alg)
                 except WrongScriptException as e:
                     ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, e.msg)
                 except Exception as e:
                     ProcessingLog.addToLog(
                         ProcessingLog.LOG_ERROR,
                         QCoreApplication.translate('Processing', 'Could not load script: {0}\n{1}').format(descriptionFile, str(e))
                     )
     return algs
Esempio n. 18
0
 def execute(self):
     filename = QFileDialog.getOpenFileName(
         self.toolbox, self.tr('Script files',
                               'AddScriptFromFileAction'), None,
         self.tr('Script files (*.py *.PY)', 'AddScriptFromFileAction'))
     if filename:
         try:
             script = ScriptAlgorithm(filename)
         except WrongScriptException:
             QMessageBox.warning(
                 self.toolbox,
                 self.tr('Error reading script', 'AddScriptFromFileAction'),
                 self.tr(
                     'The selected file does not contain a valid script',
                     'AddScriptFromFileAction'))
             return
         destFilename = os.path.join(ScriptUtils.scriptsFolder(),
                                     os.path.basename(filename))
         with open(destFilename, 'w') as f:
             f.write(script.script)
         self.toolbox.updateProvider('script')
Esempio n. 19
0
    def install(self):
        """Install the processing scripts in the collection.

        We copy the processing scripts exist in the processing dir to the
        user's processing scripts directory (~/.qgis2/processing/scripts) and
        refresh the provider.
        """
        # Check if the dir exists, pass installing silently if it doesn't exist
        if not os.path.exists(self.resource_dir):
            return

        # Get all the script files under self.resource_dir
        processing_files = []
        for item in os.listdir(self.resource_dir):
            file_path = os.path.join(self.resource_dir, item)
            if fnmatch.fnmatch(file_path, '*.py'):
                processing_files.append(file_path)

        for processing_file in processing_files:
            # Install silently the processing file
            try:
                script = ScriptAlgorithm(processing_file)
            except WrongScriptException:
                continue

            script_file_name = os.path.basename(processing_file)
            script_name = '%s (%s).%s' % (
                os.path.splitext(script_file_name)[0],
                self.collection_id,
                os.path.splitext(script_file_name)[1],
            )
            dest_path = os.path.join(self.scripts_folder(), script_name)
            with open(dest_path, 'w') as f:
                f.write(script.script)

        self.refresh_script_provider()
Esempio n. 20
0
    def runAlgorithm(self):
        if self.algType == self.SCRIPT_PYTHON:
            alg = ScriptAlgorithm(None, self.editor.text())
            alg.provider = algList.getProviderFromName('script')
        if self.algType == self.SCRIPT_R:
            alg = RAlgorithm(None, self.editor.text())
            alg.provider = algList.getProviderFromName('r')

        dlg = alg.getCustomParametersDialog()
        if not dlg:
            dlg = AlgorithmDialog(alg)

        canvas = iface.mapCanvas()
        prevMapTool = canvas.mapTool()

        dlg.show()
        dlg.exec_()

        if canvas.mapTool() != prevMapTool:
            try:
                canvas.mapTool().reset()
            except:
                pass
            canvas.setMapTool(prevMapTool)
Esempio n. 21
0
    def runAlgorithm(self):
        if self.algType == self.SCRIPT_PYTHON:
            alg = ScriptAlgorithm(None, unicode(self.editor.text()))
            alg.provider = Providers.providers['script']
        if self.algType == self.SCRIPT_R:
            alg = RAlgorithm(None, unicode(self.editor.text()))
            alg.provider = Providers.providers['r']

        dlg = alg.getCustomParametersDialog()
        if not dlg:
            dlg = ParametersDialog(alg)

        canvas = interface.iface.mapCanvas()
        prevMapTool = canvas.mapTool()

        dlg.show()
        dlg.exec_()

        if canvas.mapTool() != prevMapTool:
            try:
                canvas.mapTool().reset()
            except:
                pass
            canvas.setMapTool(prevMapTool)
Esempio n. 22
0
    def check_algorithm(self, name, defs):
        """
        Will run an algorithm definition and check if it generates the expected result
        :param name: The identifier name used in the test output heading
        :param defs: A python dict containing a test algorithm definition
        """
        QgsProject.instance().removeAllMapLayers()

        params = self.load_params(defs['params'])

        if defs['algorithm'].startswith('script:'):
            filePath = os.path.join(processingTestDataPath(), 'scripts', '{}.py'.format(defs['algorithm'][len('script:'):]))
            alg = ScriptAlgorithm(filePath)
        else:
            alg = QgsApplication.processingRegistry().algorithmById(defs['algorithm'])

        if isinstance(params, list):
            for param in zip(alg.parameters, params):
                param[0].setValue(param[1])
        else:
            for k, p in list(params.items()):
                alg.setParameterValue(k, p)

        for r, p in list(defs['results'].items()):
            alg.setOutputValue(r, self.load_result_param(p))

        expectFailure = False
        if 'expectedFailure' in defs:
            exec(('\n'.join(defs['expectedFailure'][:-1])), globals(), locals())
            expectFailure = eval(defs['expectedFailure'][-1])

        if expectFailure:
            try:
                alg.execute()
                self.check_results(alg.getOutputValuesAsDictionary(), defs['params'], defs['results'])
            except Exception:
                pass
            else:
                raise _UnexpectedSuccess
        else:
            alg.execute()
            self.check_results(alg.getOutputValuesAsDictionary(), defs['params'], defs['results'])
Esempio n. 23
0
    def check_algorithm(self, name, defs):
        """
        Will run an algorithm definition and check if it generates the expected result
        :param name: The identifier name used in the test output heading
        :param defs: A python dict containing a test algorithm definition
        """
        QgsProject.instance().removeAllMapLayers()

        params = self.load_params(defs['params'])

        if defs['algorithm'].startswith('script:'):
            filePath = os.path.join(
                processingTestDataPath(), 'scripts',
                '{}.py'.format(defs['algorithm'][len('script:'):]))
            alg = ScriptAlgorithm(filePath)
        else:
            alg = QgsApplication.processingRegistry().algorithmById(
                defs['algorithm'])

        if isinstance(params, list):
            for param in zip(alg.parameters, params):
                param[0].setValue(param[1])
        else:
            for k, p in list(params.items()):
                alg.setParameterValue(k, p)

        for r, p in list(defs['results'].items()):
            alg.setOutputValue(r, self.load_result_param(p))

        expectFailure = False
        if 'expectedFailure' in defs:
            exec(('\n'.join(defs['expectedFailure'][:-1])), globals(),
                 locals())
            expectFailure = eval(defs['expectedFailure'][-1])

        # ignore user setting for invalid geometry handling
        context = QgsProcessingContext()
        context.setProject(QgsProject.instance())

        if expectFailure:
            try:
                alg.execute(context)
                self.check_results(alg.getOutputValuesAsDictionary(),
                                   defs['params'], defs['results'])
            except Exception:
                pass
            else:
                raise _UnexpectedSuccess
        else:
            alg.execute(context)
            self.check_results(alg.getOutputValuesAsDictionary(),
                               defs['params'], defs['results'])