ScriptAlgorithmProvider.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """
  2. ***************************************************************************
  3. ScriptAlgorithmProvider.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Victor Olaya'
  18. __date__ = 'August 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. import os
  21. from qgis.core import (Qgis,
  22. QgsMessageLog,
  23. QgsApplication,
  24. QgsProcessingProvider,
  25. QgsRuntimeProfiler)
  26. from processing.core.ProcessingConfig import ProcessingConfig, Setting
  27. from processing.gui.ProviderActions import (ProviderActions,
  28. ProviderContextMenuActions)
  29. from processing.script.AddScriptFromFileAction import AddScriptFromFileAction
  30. from processing.script.CreateNewScriptAction import CreateNewScriptAction
  31. from processing.script.AddScriptFromTemplateAction import AddScriptFromTemplateAction
  32. from processing.script.DeleteScriptAction import DeleteScriptAction
  33. from processing.script.EditScriptAction import EditScriptAction
  34. from processing.script.OpenScriptFromFileAction import OpenScriptFromFileAction
  35. from processing.script import ScriptUtils
  36. from processing.tools.system import userFolder
  37. class ScriptAlgorithmProvider(QgsProcessingProvider):
  38. def __init__(self):
  39. super().__init__()
  40. self.algs = []
  41. self.additional_algorithm_classes = []
  42. self.actions = [CreateNewScriptAction(),
  43. AddScriptFromTemplateAction(),
  44. OpenScriptFromFileAction(),
  45. AddScriptFromFileAction()
  46. ]
  47. self.contextMenuActions = [EditScriptAction(),
  48. DeleteScriptAction()]
  49. def load(self):
  50. with QgsRuntimeProfiler.profile('Script Provider'):
  51. ProcessingConfig.settingIcons[self.name()] = self.icon()
  52. ProcessingConfig.addSetting(Setting(self.name(),
  53. ScriptUtils.SCRIPTS_FOLDERS,
  54. self.tr("Scripts folder(s)"),
  55. ScriptUtils.defaultScriptsFolder(),
  56. valuetype=Setting.MULTIPLE_FOLDERS))
  57. ProviderActions.registerProviderActions(self, self.actions)
  58. ProviderContextMenuActions.registerProviderContextMenuActions(self.contextMenuActions)
  59. ProcessingConfig.readSettings()
  60. self.refreshAlgorithms()
  61. return True
  62. def unload(self):
  63. ProcessingConfig.removeSetting(ScriptUtils.SCRIPTS_FOLDERS)
  64. ProviderActions.deregisterProviderActions(self)
  65. ProviderContextMenuActions.deregisterProviderContextMenuActions(self.contextMenuActions)
  66. def icon(self):
  67. return QgsApplication.getThemeIcon("/processingScript.svg")
  68. def svgIconPath(self):
  69. return QgsApplication.iconPath("processingScript.svg")
  70. def id(self):
  71. return "script"
  72. def name(self):
  73. return self.tr("Scripts")
  74. def supportsNonFileBasedOutput(self):
  75. # TODO - this may not be strictly true. We probably need a way for scripts
  76. # to indicate whether individual outputs support non-file based outputs,
  77. # but for now allow it. At best we expose nice features to users, at worst
  78. # they'll get an error if they use them with incompatible outputs...
  79. return True
  80. def add_algorithm_class(self, algorithm_class):
  81. """
  82. Adds an algorithm class to the provider
  83. """
  84. self.additional_algorithm_classes.append(algorithm_class)
  85. self.refreshAlgorithms()
  86. def loadAlgorithms(self):
  87. self.algs = []
  88. folders = ScriptUtils.scriptsFolders()
  89. # always add default script folder to the list
  90. defaultScriptFolder = ScriptUtils.defaultScriptsFolder()
  91. if defaultScriptFolder not in folders:
  92. folders.append(defaultScriptFolder)
  93. # load all scripts
  94. for folder in folders:
  95. folder = ScriptUtils.resetScriptFolder(folder)
  96. if not folder:
  97. continue
  98. for path, subdirs, files in os.walk(folder):
  99. for entry in files:
  100. if entry.lower().endswith(".py"):
  101. moduleName = os.path.splitext(os.path.basename(entry))[0]
  102. filePath = os.path.abspath(os.path.join(path, entry))
  103. alg = ScriptUtils.loadAlgorithm(moduleName, filePath)
  104. if alg is not None:
  105. self.algs.append(alg)
  106. for alg_class in self.additional_algorithm_classes:
  107. self.algs.append(alg_class())
  108. for a in self.algs:
  109. self.addAlgorithm(a)