Skip to content
Permalink
Browse files

[processing] Remove all remaining algorithm copies

Algorithms are no longer copied - just a single const instance
of each algorithm is used
  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent 8b4bf88 commit 03e40f7497e7d6a833d644be795f06a7711f554c
@@ -110,8 +110,6 @@ class QgsProcessingAlgorithm
:rtype: QgsProcessingProvider
%End

void setProvider( QgsProcessingProvider *provider );

QgsProcessingParameterDefinitions parameterDefinitions() const;
%Docstring
Returns an ordered list of parameter definitions utilized by the algorithm.
@@ -92,12 +92,11 @@ def shortHelp(self):
'''.format(self.commandName(), url))

def commandName(self):
alg = self.getCopy()
for output in alg.outputs:
for output in self.outputs:
output.setValue("dummy")
for param in alg.parameters:
for param in self.parameters:
param.setValue("1")
name = alg.getConsoleCommands()[0]
name = self.getConsoleCommands()[0]
if name.endswith(".py"):
name = name[:-3]
return name
@@ -97,9 +97,6 @@ def __init__(self, descriptionfile):
except ImportError:
self.module = None

def getCopy(self):
return self

def name(self):
return self._name

@@ -78,9 +78,6 @@ def __init__(self, descriptionfile):
self._display_name = ''
self._group = ''

def getCopy(self):
return self

def icon(self):
if self._icon is None:
self._icon = QIcon(os.path.join(pluginPath, 'images', 'saga.png'))
@@ -74,15 +74,6 @@ def __init__(self):

self.defineCharacteristics()

def getCopy(self):
"""Returns a new instance of this algorithm, ready to be used
for being executed.
"""
newone = self
newone.parameters = copy.deepcopy(self.parameters)
newone.outputs = copy.deepcopy(self.outputs)
return newone

# methods to overwrite when creating a custom geoalgorithm

def _formatHelp(self, text):
@@ -130,11 +130,6 @@ def runAlgorithm(algOrName, onFinish, *args, **kwargs):
QgsMessageLog.logMessage(Processing.tr('Error: Algorithm {0} not found\n').format(algOrName),
Processing.tr("Processing"))
return
# hack - remove when getCopy is removed
provider = alg.provider()
alg = alg.getCopy()
#hack pt2
alg.setProvider(provider)

if len(args) == 1 and isinstance(args[0], dict):
# Set params by name and try to run the alg even if not all parameter values are provided,
@@ -72,9 +72,7 @@ def accept(self):
self.canceled = False

for row in range(self.mainWidget.tblParameters.rowCount()):
alg = self.alg.getCopy()
# hack - remove when getCopy is removed
alg.setProvider(self.alg.provider())
alg = self.alg
col = 0
for param in alg.parameters:
if param.hidden:
@@ -182,7 +182,7 @@ def save(self):
algParams = {}
algOutputs = {}
col = 0
alg = self.alg.getCopy()
alg = self.alg
for param in alg.parameters:
if param.hidden:
continue
@@ -48,7 +48,7 @@ def __init__(self, alg):
super(EditRenderingStylesDialog, self).__init__(None)
self.setupUi(self)

self.alg = alg.getCopy()
self.alg = alg

self.tblStyles.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.setWindowTitle(self.alg.displayName())
@@ -246,11 +246,6 @@ def executeAlgorithmAsBatchProcess(self):
item = self.algorithmTree.currentItem()
if isinstance(item, TreeAlgorithmItem):
alg = QgsApplication.processingRegistry().algorithmById(item.alg.id())
#hack - remove when getCopy is removed
provider = alg.provider()
alg = alg.getCopy()
#hack pt 2
alg.setProvider(provider)
dlg = BatchAlgorithmDialog(alg)
dlg.show()
dlg.exec_()
@@ -270,11 +265,6 @@ def executeAlgorithm(self):
dlg.exec_()
return

# temporary hack - TODO remove this getCopy when parameters are moved from algorithm
provider = alg.provider()
alg = alg.getCopy()
alg.setProvider(provider)

if (alg.countVisibleParameters() + alg.getVisibleOutputsCount()) > 0:
dlg = alg.getCustomParametersDialog()
if not dlg:
@@ -203,12 +203,6 @@ def _executeAlgorithm(alg):
dlg.exec_()
return

# hack - remove when getCopy is removed
provider = alg.provider()
alg = alg.getCopy()
#hack pt 2
alg.setProvider(provider)

context = dataobjects.createContext()
if (alg.countVisibleParameters() + alg.getVisibleOutputsCount()) > 0:
dlg = alg.getCustomParametersDialog()
@@ -40,9 +40,7 @@ def isEnabled(self):
return isinstance(self.itemData, ModelerAlgorithm)

def execute(self):
alg = self.itemData.getCopy()
#hack - remove when getCopy is removed
alg.setProvider(self.itemData.provider())
alg = self.itemData
dlg = ModelerDialog(alg)
dlg.update_model.connect(self.updateModel)
dlg.show()
@@ -225,21 +225,6 @@ class ModelerAlgorithm(GeoAlgorithm):

CANVAS_SIZE = 4000

def getCopy(self):
newone = ModelerAlgorithm()

newone.algs = {}
for algname, alg in self.algs.items():
newone.algs[algname] = Algorithm()
newone.algs[algname].__dict__.update(copy.deepcopy(alg.todict()))
newone.inputs = copy.deepcopy(self.inputs)
newone.defineCharacteristics()
newone._name = self._name
newone._group = self._group
newone.descriptionFile = self.descriptionFile
newone.helpContent = copy.deepcopy(self.helpContent)
return newone

def __init__(self):
self._name = self.tr('Model', 'ModelerAlgorithm')
# The dialog where this model is being edited
@@ -112,7 +112,7 @@ def _dropEvent(event):
else:
alg = QgsApplication.processingRegistry().algorithmById(text)
if alg is not None:
self._addAlgorithm(alg.getCopy(), event.pos())
self._addAlgorithm(alg, event.pos())
event.accept()
else:
event.ignore()
@@ -269,7 +269,7 @@ def closeEvent(self, evt):
evt.accept()

def editHelp(self):
alg = self.alg.getCopy()
alg = self.alg
dlg = HelpEditionDialog(alg)
dlg.exec_()
if dlg.descriptions:
@@ -281,12 +281,7 @@ def runModel(self):
self.bar.pushMessage("", "Model doesn't contain any algorithm and/or parameter and can't be executed", level=QgsMessageBar.WARNING, duration=5)
return

# hack - remove when above getCopy is removed
provider = self.alg.provider()
alg = self.alg.getCopy()
# hack pt 2
alg.setProvider(provider)
dlg = AlgorithmDialog(alg)
dlg = AlgorithmDialog(self.alg)
dlg.exec_()

def save(self):
@@ -554,7 +549,7 @@ def addAlgorithm(self):
item = self.algorithmTree.currentItem()
if isinstance(item, TreeAlgorithmItem):
alg = QgsApplication.processingRegistry().algorithmById(item.alg.id())
self._addAlgorithm(alg.getCopy())
self._addAlgorithm(alg)

def _addAlgorithm(self, alg, pos=None):
dlg = alg.getCustomModelerParametersDialog(self.alg)
@@ -42,6 +42,6 @@ def isEnabled(self):
not isinstance(self.itemData, PreconfiguredAlgorithm))

def execute(self):
alg = self.itemData.getCopy() # make copy so we do not taint the original one in the dialog
alg = self.itemData
dlg = PreconfiguredAlgorithmDialog(alg, self.toolbox)
dlg.exec_()
@@ -55,19 +55,12 @@ def name(self):
def flags(self):
return QgsProcessingAlgorithm.FlagHideFromModeler

def getCopy(self):
newone = self
newone.outputs = []
newone._name = self._name
newone._group = self._group
return newone

def defineCharacteristics(self):
self._name = self.description["name"]
self._group = self.description["group"]

def execute(self, context=None, feedback=None, model=None):
self.alg = QgsApplication.processingRegistry().algorithmById(self.description["algname"]).getCopy()
self.alg = QgsApplication.processingRegistry().algorithmById(self.description["algname"])
for name, value in list(self.description["parameters"].items()):
self.alg.setParameterValue(name, value)
for name, value in list(self.description["outputs"].items()):
@@ -72,10 +72,6 @@ def __init__(self, descriptionFile, script=None):
if descriptionFile is not None:
self.defineCharacteristicsFromFile()

def getCopy(self):
newone = self
return newone

def icon(self):
return self._icon

@@ -63,7 +63,6 @@ def algorithmHelp(id):
"""
alg = QgsApplication.processingRegistry().algorithmById(id)
if alg is not None:
alg = alg.getCopy()
print(str(alg))
algorithmOptions(id)
else:
@@ -122,12 +122,6 @@ class CORE_EXPORT QgsProcessingAlgorithm
*/
QgsProcessingProvider *provider() const;

/**
* Associates this algorithm with its provider. No transfer of ownership is involved.
*/
//TEMPORARY - remove when algorithms are no longer copied in python code
void setProvider( QgsProcessingProvider *provider );

/**
* Returns an ordered list of parameter definitions utilized by the algorithm.
* \see addParameter()
@@ -282,6 +276,11 @@ class CORE_EXPORT QgsProcessingAlgorithm
QgsProcessingProvider *mProvider = nullptr;
QgsProcessingParameterDefinitions mParameters;

/**
* Associates this algorithm with its provider. No transfer of ownership is involved.
*/
void setProvider( QgsProcessingProvider *provider );

// friend class to access setProvider() - we do not want this public!
friend class QgsProcessingProvider;
friend class TestQgsProcessing;

0 comments on commit 03e40f7

Please sign in to comment.
You can’t perform that action at this time.