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()
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)
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)
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)
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)
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)
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)
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()
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)