def run(self): s = str(self.combo.currentText()) if s.startswith('Processing algorithm: '): algName = s[len('Processing algorithm: '):] alg = algList.getAlgorithm(algName) if alg is not None: self.close() self.runAlgorithm(alg) elif s.startswith("Command: "): command = s[len("Command: "):] try: self.runCommand(command) self.close() except Exception as e: self.label.setVisible(True) self.label.setText('Error:' + str(e)) elif s.startswith('Menu action: '): actionName = s[len('Menu action: '):] menuActions = [] actions = iface.mainWindow().menuBar().actions() for action in actions: menuActions.extend(self.getActions(action)) for action in menuActions: if action.text() == actionName: self.close() action.trigger() return else: try: self.runCommand(s) self.close() except Exception as e: self.label.setVisible(True) self.label.setText('Error:' + str(e))
def checkBeforeOpeningParametersDialog(self): for alg in list(self.algs.values()): algInstance = algList.getAlgorithm(alg.consoleName) if algInstance is None: return self.tr( "The model you are trying to run contains an algorithm that is not available: <i>{0}</i>" ).format(alg.consoleName)
def execute(self, progress): self.alg = algList.getAlgorithm(self.description["algname"]).getCopy() for name, value in list(self.description["parameters"].items()): self.alg.setParameterValue(name, value) for name, value in list(self.description["outputs"].items()): self.alg.setOutputValue(name, value) self.alg.execute(progress) self.outputs = self.alg.outputs
def execute(self, progress): self.alg = algList.getAlgorithm(self.description["algname"]).getCopy() for name, value in self.description["parameters"].iteritems(): self.alg.setParameterValue(name, value) for name, value in self.description["outputs"].iteritems(): self.alg.setOutputValue(name, value) self.alg.execute(progress) self.outputs = self.alg.outputs
def execute(self, feedback): self.alg = algList.getAlgorithm(self.description["algname"]).getCopy() for name, value in list(self.description["parameters"].items()): self.alg.setParameterValue(name, value) for name, value in list(self.description["outputs"].items()): self.alg.setOutputValue(name, value) self.alg.execute(feedback) self.outputs = self.alg.outputs
def _dropEvent(event): if event.mimeData().hasText(): text = event.mimeData().text() if text in ModelerParameterDefinitionDialog.paramTypes: self.addInputOfType(text, event.pos()) else: alg = algList.getAlgorithm(text) if alg is not None: self._addAlgorithm(alg.getCopy(), event.pos()) event.accept() else: event.ignore()
def getAlgorithm(name): return algList.getAlgorithm(name)
def addAlgorithm(self): item = self.algorithmTree.currentItem() if isinstance(item, TreeAlgorithmItem): alg = algList.getAlgorithm(item.alg.commandLineName()) self._addAlgorithm(alg.getCopy())
def algorithm(self): if self._algInstance is None: self._algInstance = algList.getAlgorithm(self.consoleName).getCopy() return self._algInstance
def checkBeforeOpeningParametersDialog(self): for alg in list(self.algs.values()): algInstance = algList.getAlgorithm(alg.consoleName) if algInstance is None: return "The model you are trying to run contains an algorithm that is not available: <i>%s</i>" % alg.consoleName
def fromOldFormatFile(filename): def _tr(s): return QCoreApplication.translate('ModelerAlgorithm', s) hardcodedValues = {} modelParameters = [] modelAlgs = [] model = ModelerAlgorithm() model.descriptionFile = filename lines = codecs.open(filename, 'r', encoding='utf-8') line = lines.readline().strip('\n').strip('\r') try: while line != '': if line.startswith('PARAMETER:'): paramLine = line[len('PARAMETER:'):] param = getParameterFromString(paramLine) if param: pass else: raise WrongModelException( _tr('Error in parameter line: %s', 'ModelerAlgorithm') % line) line = lines.readline().strip('\n') tokens = line.split(',') model.addParameter(ModelerParameter(param, QPointF(float(tokens[0]), float(tokens[1])))) modelParameters.append(param.name) elif line.startswith('VALUE:'): valueLine = line[len('VALUE:'):] tokens = valueLine.split('===') name = tokens[0] value = tokens[1].replace(ModelerAlgorithm.LINE_BREAK_STRING, '\n') hardcodedValues[name] = value elif line.startswith('NAME:'): model.name = line[len('NAME:'):] elif line.startswith('GROUP:'): model.group = line[len('GROUP:'):] elif line.startswith('ALGORITHM:'): algLine = line[len('ALGORITHM:'):] alg = algList.getAlgorithm(algLine) if alg is not None: modelAlg = Algorithm(alg.commandLineName()) modelAlg.description = alg.name posline = lines.readline().strip('\n').strip('\r') tokens = posline.split(',') modelAlg.pos = QPointF(float(tokens[0]), float(tokens[1])) # dependenceline = lines.readline().strip('\n').strip('\r') for param in alg.parameters: if not param.hidden: line = lines.readline().strip('\n').strip('\r') if line == unicode(None): modelAlg.params[param.name] = None else: tokens = line.split('|') try: algIdx = int(tokens[0]) except: raise WrongModelException( _tr('Number of parameters in the ' '{} algorithm does not match ' 'current Processing ' 'implementation'.format(alg.name))) if algIdx == -1: if tokens[1] in modelParameters: modelAlg.params[param.name] = ValueFromInput(tokens[1]) else: modelAlg.params[param.name] = hardcodedValues[tokens[1]] else: modelAlg.params[param.name] = ValueFromOutput(algIdx, tokens[1]) for out in alg.outputs: if not out.hidden: line = lines.readline().strip('\n').strip('\r') if unicode(None) != line: if '|' in line: tokens = line.split('|') name = tokens[0] tokens = tokens[1].split(',') pos = QPointF(float(tokens[0]), float(tokens[1])) else: name = line pos = None modelerOutput = ModelerOutput(name) modelerOutput.pos = pos modelAlg.outputs[out.name] = modelerOutput model.addAlgorithm(modelAlg) modelAlgs.append(modelAlg.name) else: raise WrongModelException( _tr('Error in algorithm name: %s',) % algLine) line = lines.readline().strip('\n').strip('\r') for modelAlg in model.algs.values(): for name, value in modelAlg.params.iteritems(): if isinstance(value, ValueFromOutput): value.alg = modelAlgs[value.alg] return model except Exception as e: if isinstance(e, WrongModelException): raise e else: raise WrongModelException(_tr('Error in model definition line: ') + '%s\n%s' % (line.strip(), traceback.format_exc()))