コード例 #1
0
    def saveScript(self, saveAs):
        newPath = None
        if self.filePath is None or saveAs:
            scriptDir = ScriptUtils.scriptsFolders()[0]
            newPath, _ = QFileDialog.getSaveFileName(self,
                                                     self.tr("Save script"),
                                                     scriptDir,
                                                     self.tr("Processing scripts (*.py *.PY)"))

            if newPath:
                if not newPath.lower().endswith(".py"):
                    newPath += ".py"

                self.filePath = newPath

        if self.filePath:
            text = self.editor.text()
            try:
                with codecs.open(self.filePath, "w", encoding="utf-8") as f:
                    f.write(text)
            except IOError as e:
                QMessageBox.warning(self,
                                    self.tr("I/O error"),
                                    self.tr("Unable to save edits:\n{}").format(str(e))
                                    )
                return

            self.setHasChanged(False)

        QgsApplication.processingRegistry().providerById("script").refreshAlgorithms()
コード例 #2
0
ファイル: ScriptEditorDialog.py プロジェクト: rejleite/QGIS
    def saveScript(self, saveAs):
        newPath = None
        if self.filePath is None or saveAs:
            scriptDir = ScriptUtils.scriptsFolders()[0]
            newPath, _ = QFileDialog.getSaveFileName(
                self, self.tr("Save script"), scriptDir,
                self.tr("Script files (*.py *.PY)"))

            if newPath:
                if not newPath.lower().endswith(".py"):
                    newPath += ".py"

                self.filePath = newPath

        if self.filePath:
            text = self.editor.text()
            try:
                with codecs.open(self.filePath, "w", encoding="utf-8") as f:
                    f.write(text)
            except IOError as e:
                QMessageBox.warning(
                    self, self.tr("I/O error"),
                    self.tr("Unable to save edits:\n{}").format(str(e)))
                return
            self.needUpdate = True
            self.setHasChanged(False)
コード例 #3
0
    def loadAlgorithms(self):
        self.algs = []
        folders = ScriptUtils.scriptsFolders()
        # always add default script folder to the list
        defaultScriptFolder = ScriptUtils.defaultScriptsFolder()
        if defaultScriptFolder not in folders:
            folders.append(defaultScriptFolder)
        # load all scripts
        for folder in folders:
            folder = ScriptUtils.resetScriptFolder(folder)
            if not folder:
                continue

            items = [
                f for f in os.listdir(folder)
                if os.path.isfile(os.path.join(folder, f))
            ]
            for entry in items:
                if entry.lower().endswith(".py"):
                    moduleName = os.path.splitext(os.path.basename(entry))[0]
                    filePath = os.path.abspath(os.path.join(folder, entry))
                    alg = ScriptUtils.loadAlgorithm(moduleName, filePath)
                    if alg is not None:
                        self.algs.append(alg)

        for a in self.algs:
            self.addAlgorithm(a)
コード例 #4
0
    def loadAlgorithms(self):
        self.algs = []
        folders = ScriptUtils.scriptsFolders()
        for folder in folders:
            items = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))]
            for entry in items:
                if entry.lower().endswith(".py"):
                    moduleName = os.path.splitext(os.path.basename(entry))[0]
                    filePath = os.path.abspath(os.path.join(folder, entry))
                    alg = ScriptUtils.loadAlgorithm(moduleName, filePath)
                    if alg is not None:
                        self.algs.append(alg)

        for a in self.algs:
            self.addAlgorithm(a)
コード例 #5
0
ファイル: ScriptAlgorithmProvider.py プロジェクト: mach0/QGIS
    def loadAlgorithms(self):
        self.algs = []
        folders = ScriptUtils.scriptsFolders()
        for folder in folders:
            items = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))]
            for entry in items:
                if entry.lower().endswith(".py"):
                    moduleName = os.path.splitext(os.path.basename(entry))[0]
                    filePath = os.path.abspath(os.path.join(folder, entry))
                    alg = ScriptUtils.loadAlgorithm(moduleName, filePath)
                    if alg is not None:
                        self.algs.append(alg)

        for a in self.algs:
            self.addAlgorithm(a)
コード例 #6
0
    def loadAlgorithms(self):
        self.algs = []
        folders = ScriptUtils.scriptsFolders()
        for folder in folders:
            items = os.scandir(folder)
            for entry in items:
                if entry.name.lower().endswith(".py") and entry.is_file():
                    moduleName = os.path.splitext(entry.name)[0]
                    filePath = os.path.abspath(os.path.join(folder, entry.name))
                    alg = ScriptUtils.loadAlgorithm(moduleName, filePath)
                    if alg is not None:
                        self.algs.append(alg)

        for a in self.algs:
            self.addAlgorithm(a)
コード例 #7
0
    def loadAlgorithms(self):
        self.algs = []
        folders = ScriptUtils.scriptsFolders()
        for folder in folders:
            items = os.scandir(folder)
            for entry in items:
                if entry.name.lower().endswith(".py") and entry.is_file():
                    moduleName = os.path.splitext(entry.name)[0]
                    filePath = os.path.abspath(os.path.join(folder, entry.name))
                    alg = ScriptUtils.loadAlgorithm(moduleName, filePath)
                    if alg is not None:
                        self.algs.append(alg)

        for a in self.algs:
            self.addAlgorithm(a)
コード例 #8
0
    def openScript(self):
        if self.hasChanged:
            ret = QMessageBox.warning(
                self, self.tr("Unsaved changes"),
                self.tr("There are unsaved changes in the script. Continue?"),
                QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            if ret == QMessageBox.No:
                return

        scriptDir = ScriptUtils.scriptsFolders()[0]
        fileName, _ = QFileDialog.getOpenFileName(
            self, self.tr("Open script"), scriptDir,
            self.tr("Processing scripts (*.py *.PY)"))

        if fileName == "":
            return

        with OverrideCursor(Qt.WaitCursor):
            self._loadFile(fileName)
コード例 #9
0
    def openScript(self):
        if self.hasChanged:
            ret = QMessageBox.warning(self,
                                      self.tr("Unsaved changes"),
                                      self.tr("There are unsaved changes in the script. Continue?"),
                                      QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            if ret == QMessageBox.No:
                return

        scriptDir = ScriptUtils.scriptsFolders()[0]
        fileName, _ = QFileDialog.getOpenFileName(self,
                                                  self.tr("Open script"),
                                                  scriptDir,
                                                  self.tr("Processing scripts (*.py *.PY)"))

        if fileName == "":
            return

        with OverrideCursor(Qt.WaitCursor):
            self._loadFile(fileName)
コード例 #10
0
    def execute(self):
        settings = QgsSettings()
        lastDir = settings.value("processing/lastScriptsDir", "")
        files, _ = QFileDialog.getOpenFileNames(self.toolbox,
                                                self.tr("Add script(s)"),
                                                lastDir,
                                                self.tr("Processing scripts (*.py *.PY)"))
        if files:
            settings.setValue("processing/lastScriptsDir", os.path.dirname(files[0]))

            valid = 0
            for f in files:
                try:
                    shutil.copy(f, ScriptUtils.scriptsFolders()[0])
                    valid += 1
                except OSError as e:
                    QgsMessageLog.logMessage(self.tr("Could not copy script '{}'\n{}").format(f, str(e)),
                                             "Processing",
                                             Qgis.Warning)

            if valid > 0:
                QgsApplication.processingRegistry().providerById("script").refreshAlgorithms()
コード例 #11
0
ファイル: AddScriptFromFileAction.py プロジェクト: aaime/QGIS
    def execute(self):
        settings = QgsSettings()
        lastDir = settings.value("processing/lastScriptsDir", "")
        files, _ = QFileDialog.getOpenFileNames(self.toolbox,
                                                self.tr("Add script(s)"),
                                                lastDir,
                                                self.tr("Processing scripts (*.py *.PY)"))
        if files:
            settings.setValue("processing/lastScriptsDir", os.path.dirname(files[0]))

            valid = 0
            for f in files:
                try:
                    shutil.copy(f, ScriptUtils.scriptsFolders()[0])
                    valid += 1
                except OSError as e:
                    QgsMessageLog.logMessage(self.tr("Could not copy script '{}'\n{}").format(f, str(e)),
                                             "Processing",
                                             Qgis.Warning)

            if valid > 0:
                QgsApplication.processingRegistry().providerById("script").refreshAlgorithms()
コード例 #12
0
    def loadAlgorithms(self):
        self.algs = []
        folders = ScriptUtils.scriptsFolders()
        # always add default script folder to the list
        defaultScriptFolder = ScriptUtils.defaultScriptsFolder()
        if defaultScriptFolder not in folders:
            folders.append(defaultScriptFolder)
        # load all scripts
        for folder in folders:
            folder = ScriptUtils.resetScriptFolder(folder)
            if not folder:
                continue

            items = [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))]
            for entry in items:
                if entry.lower().endswith(".py"):
                    moduleName = os.path.splitext(os.path.basename(entry))[0]
                    filePath = os.path.abspath(os.path.join(folder, entry))
                    alg = ScriptUtils.loadAlgorithm(moduleName, filePath)
                    if alg is not None:
                        self.algs.append(alg)

        for a in self.algs:
            self.addAlgorithm(a)