|
25 | 25 |
|
26 | 26 | __revision__ = '$Format:%H$' |
27 | 27 |
|
| 28 | +from qgis.processing import alg as algfactory |
28 | 29 | import os |
29 | 30 | import inspect |
30 | 31 | import importlib |
@@ -66,20 +67,26 @@ def loadAlgorithm(moduleName, filePath): |
66 | 67 | spec = importlib.util.spec_from_file_location(moduleName, filePath) |
67 | 68 | module = importlib.util.module_from_spec(spec) |
68 | 69 | spec.loader.exec_module(module) |
69 | | - for x in dir(module): |
70 | | - obj = getattr(module, x) |
71 | | - if inspect.isclass(obj) and issubclass(obj, (QgsProcessingAlgorithm, QgsProcessingFeatureBasedAlgorithm)) and obj.__name__ not in ("QgsProcessingAlgorithm", "QgsProcessingFeatureBasedAlgorithm"): |
72 | | - scriptsRegistry[x] = filePath |
73 | | - return obj() |
| 70 | + try: |
| 71 | + alg = algfactory.instances.pop().createInstance() |
| 72 | + scriptsRegistry[alg.name()] = filePath |
| 73 | + return alg |
| 74 | + except IndexError: |
| 75 | + for x in dir(module): |
| 76 | + obj = getattr(module, x) |
| 77 | + if inspect.isclass(obj) and issubclass(obj, (QgsProcessingAlgorithm, QgsProcessingFeatureBasedAlgorithm)) and obj.__name__ not in ("QgsProcessingAlgorithm", "QgsProcessingFeatureBasedAlgorithm"): |
| 78 | + o = obj() |
| 79 | + scriptsRegistry[o.name()] = filePath |
| 80 | + return o |
74 | 81 | except ImportError as e: |
75 | 82 | QgsMessageLog.logMessage(QCoreApplication.translate("ScriptUtils", "Could not import script algorithm '{}' from '{}'\n{}").format(moduleName, filePath, str(e)), |
76 | 83 | QCoreApplication.translate("ScriptUtils", "Processing"), |
77 | 84 | Qgis.Critical) |
78 | 85 |
|
79 | 86 |
|
80 | | -def findAlgorithmSource(className): |
| 87 | +def findAlgorithmSource(name): |
81 | 88 | global scriptsRegistry |
82 | 89 | try: |
83 | | - return scriptsRegistry[className] |
| 90 | + return scriptsRegistry[name] |
84 | 91 | except: |
85 | 92 | return None |
0 commit comments