def testResetScriptFolder(self): # if folder exist defaultScriptFolder = ScriptUtils.defaultScriptsFolder() folder = ScriptUtils.resetScriptFolder(defaultScriptFolder) self.assertEqual(folder, defaultScriptFolder) folder = ScriptUtils.resetScriptFolder('.') self.assertEqual(folder, '.') # if folder does not exist and not absolute folder = ScriptUtils.resetScriptFolder('fake') self.assertEqual(folder, None) # if absolute but not relative to QgsApplication.qgisSettingsDirPath() folder = os.path.join(tempfile.gettempdir(), 'fakePath') newFolder = ScriptUtils.resetScriptFolder(folder) self.assertEqual(newFolder, folder) # if absolute profile but poiting somewhere # reset the path as pointing to profile into the current settings folder = QgsApplication.qgisSettingsDirPath() # modify default profile changing absolute path pointing somewhere paths = folder.split(os.sep) paths[0] = '/' paths[1] = 'fakelocation' folder = os.path.join(*paths) folder = ScriptUtils.resetScriptFolder(folder) self.assertEqual(folder, QgsApplication.qgisSettingsDirPath())
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() # 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 for path, subdirs, files in os.walk(folder): for entry in files: if entry.lower().endswith(".py"): moduleName = os.path.splitext(os.path.basename(entry))[0] filePath = os.path.abspath(os.path.join(path, entry)) alg = ScriptUtils.loadAlgorithm(moduleName, filePath) if alg is not None: self.algs.append(alg) for alg_class in self.additional_algorithm_classes: self.algs.append(alg_class()) for a in self.algs: self.addAlgorithm(a)
def __register_processing_scripts(self, scripts_dir): count = 0 registered_scripts = list() qgis_scripts_dir = ScriptUtils.defaultScriptsFolder() for filename in glob.glob(os.path.join(scripts_dir, '*.py')): try: shutil.copy(filename, qgis_scripts_dir) count += 1 registered_scripts.append(os.path.basename(filename)) except OSError as e: self.logger.critical( __name__, "Couldn't install LADM-COL script '{}'!".format(filename)) if count: QgsApplication.processingRegistry().providerById( "script").refreshAlgorithms() if DEFAULT_LOG_MODE == EnumLogMode.DEV: self.logger.debug( __name__, "{} LADM-COL Processing scripts were installed! {}".format( count, registered_scripts)) else: self.logger.debug( __name__, "{} LADM-COL Processing scripts were installed!".format( count))
def unloadAllScripts(self): """Uninstall the processing scripts from processing toolbox.""" if not os.path.exists(SCRIPTS_DIR): return # Remove the processing script files that are intalled by the # provider for item in os.listdir(SCRIPTS_DIR): file_path = os.path.join(SCRIPTS_DIR, item) if fnmatch.fnmatch(file_path, '*.py'): script_path = os.path.join(ScriptUtils.defaultScriptsFolder(), item) if os.path.exists(script_path): os.remove(script_path) self.refresh_script_provider()
def loadAllScripts(self): """ Install processing custom scripts. Method freely inspired by Resource Sharing plugin. """ # Pass silently if the directory does not exist if not os.path.exists(SCRIPTS_DIR): return # Get all the script files in the plugin processing_files = [] for item in os.listdir(SCRIPTS_DIR): file_path = os.path.join(SCRIPTS_DIR, item) if fnmatch.fnmatch(file_path, '*.py'): processing_files.append(file_path) valid = 0 for processing_file in processing_files: # Install the processing file silently # firt trying to create symlink. If error (for filesystem and OS reasons), then copy it try: destination_file = os.path.join( ScriptUtils.defaultScriptsFolder(), os.path.basename(processing_file) ) os.symlink(processing_file, destination_file) valid += 1 except OSError as e: message = "Could not link script '" +str(processing_file) + "' trying copying it\n" + str(e) QgsMessageLog.logMessage(message, galiciasustentable_constants.PLUGIN_DOMAIN, level=Qgis.Critical) try: shutil.copy(processing_file, ScriptUtils.defaultScriptsFolder()) valid += 1 except OSError as e: message = "Could not copy script '" +str(processing_file) + "'\n" + str(e) QgsMessageLog.logMessage(message, galiciasustentable_constants.PLUGIN_DOMAIN, level=Qgis.Critical) if valid > 0: self.refresh_script_provider()
def load(self): ProcessingConfig.settingIcons[self.name()] = self.icon() ProcessingConfig.addSetting(Setting(self.name(), ScriptUtils.SCRIPTS_FOLDERS, self.tr("Scripts folder(s)"), ScriptUtils.defaultScriptsFolder(), valuetype=Setting.MULTIPLE_FOLDERS)) ProviderActions.registerProviderActions(self, self.actions) ProviderContextMenuActions.registerProviderContextMenuActions(self.contextMenuActions) ProcessingConfig.readSettings() self.refreshAlgorithms() return True
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 scripts_folder(self): """Return the default processing scripts folder.""" return ScriptUtils.defaultScriptsFolder()