2013-04-12 12:28:59 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
SaveAsPythonScriptAction.py
|
|
|
|
---------------------
|
|
|
|
Date : April 2013
|
|
|
|
Copyright : (C) 2013 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'April 2013'
|
|
|
|
__copyright__ = '(C) 2013, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-04-12 12:28:59 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-04-12 12:28:59 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
import sys
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.gui.ContextAction import ContextAction
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm, \
|
2013-04-12 12:28:59 +02:00
|
|
|
AlgorithmAndParameter
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.script.ScriptUtils import ScriptUtils
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.parameters.ParameterMultipleInput import ParameterMultipleInput
|
|
|
|
|
2013-04-12 12:28:59 +02:00
|
|
|
class SaveAsPythonScriptAction(ContextAction):
|
|
|
|
|
|
|
|
def __init__(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
self.name = 'Save as Python script'
|
2013-04-12 12:28:59 +02:00
|
|
|
|
|
|
|
def isEnabled(self):
|
|
|
|
return isinstance(self.alg, ModelerAlgorithm)
|
|
|
|
|
|
|
|
def execute(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
filename = str(QFileDialog.getSaveFileName(None, 'Save Script',
|
|
|
|
ScriptUtils.scriptsFolder(), 'Python scripts (*.py)'))
|
2013-04-12 12:28:59 +02:00
|
|
|
|
|
|
|
if filename:
|
2013-10-01 20:52:22 +03:00
|
|
|
if not filename.endswith('.py'):
|
|
|
|
filename += '.py'
|
2013-04-15 07:16:20 +02:00
|
|
|
text = self.translateToPythonCode(self.alg)
|
2013-04-12 12:28:59 +02:00
|
|
|
try:
|
2013-10-01 20:52:22 +03:00
|
|
|
fout = open(filename, 'w')
|
2013-04-12 12:28:59 +02:00
|
|
|
fout.write(text)
|
|
|
|
fout.close()
|
2013-10-01 20:52:22 +03:00
|
|
|
if filename.replace('\\', '/').startswith(
|
2014-04-06 12:27:51 +02:00
|
|
|
ScriptUtils.scriptsFolder().replace('\\', '/')):
|
2014-03-26 10:18:27 +01:00
|
|
|
self.toolbox.updateProvider('script')
|
2013-04-12 12:28:59 +02:00
|
|
|
except:
|
2013-10-01 20:52:22 +03:00
|
|
|
QMessageBox.warning(self, self.tr('I/O error'),
|
|
|
|
self.tr('Unable to save edits. Reason:\n %s')
|
|
|
|
% unicode(sys.exc_info()[1]))
|
2013-04-15 07:16:20 +02:00
|
|
|
|
2013-04-12 12:28:59 +02:00
|
|
|
def translateToPythonCode(self, model):
|
2013-10-01 20:52:22 +03:00
|
|
|
s = ['##' + model.name + '=name']
|
2013-04-12 12:28:59 +02:00
|
|
|
for param in model.parameters:
|
|
|
|
s.append(str(param.getAsScriptCode().lower()))
|
|
|
|
i = 0
|
|
|
|
for outs in model.algOutputs:
|
|
|
|
for out in outs.keys():
|
|
|
|
if outs[out]:
|
2013-10-01 20:52:22 +03:00
|
|
|
s.append('##' + out.lower() + '_alg' + str(i) + '='
|
|
|
|
+ model.getOutputType(i, out))
|
2013-04-12 12:28:59 +02:00
|
|
|
i += 1
|
|
|
|
i = 0
|
|
|
|
iMultiple = 0
|
|
|
|
for alg in model.algs:
|
2013-10-01 20:52:22 +03:00
|
|
|
multiple = []
|
2014-03-27 14:53:41 +01:00
|
|
|
runline = 'outputs_' + str(i) + '=processing.runalg("' \
|
2013-10-01 20:52:22 +03:00
|
|
|
+ alg.commandLineName() + '"'
|
2013-04-12 12:28:59 +02:00
|
|
|
for param in alg.parameters:
|
|
|
|
aap = model.algParameters[i][param.name]
|
2013-10-01 20:52:22 +03:00
|
|
|
if aap is None:
|
|
|
|
runline += ', None'
|
2013-04-12 12:28:59 +02:00
|
|
|
elif isinstance(param, ParameterMultipleInput):
|
|
|
|
value = model.paramValues[aap.param]
|
2013-10-01 20:52:22 +03:00
|
|
|
tokens = value.split(';')
|
2013-04-12 12:28:59 +02:00
|
|
|
layerslist = []
|
|
|
|
for token in tokens:
|
2013-10-01 20:52:22 +03:00
|
|
|
(iAlg, paramname) = token.split('|')
|
|
|
|
if float(iAlg) == float(
|
|
|
|
AlgorithmAndParameter.PARENT_MODEL_ALGORITHM):
|
2013-04-12 12:28:59 +02:00
|
|
|
if model.ismodelparam(paramname):
|
|
|
|
value = paramname.lower()
|
|
|
|
else:
|
|
|
|
value = model.paramValues[paramname]
|
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
value = 'outputs_' + str(iAlg) + "['" + paramname \
|
|
|
|
+ "']"
|
2013-04-12 12:28:59 +02:00
|
|
|
layerslist.append(str(value))
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
multiple.append('multiple_' + str(iMultiple) + '=['
|
|
|
|
+ ','.join(layerslist) + ']')
|
|
|
|
runline += ', ";".join(multiple_' + str(iMultiple) + ') '
|
2013-04-12 12:28:59 +02:00
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
if float(aap.alg) == float(
|
|
|
|
AlgorithmAndParameter.PARENT_MODEL_ALGORITHM):
|
2013-04-12 12:28:59 +02:00
|
|
|
if model.ismodelparam(aap.param):
|
2013-10-01 20:52:22 +03:00
|
|
|
runline += ', ' + aap.param.lower()
|
2013-04-12 12:28:59 +02:00
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
runline += ', ' + str(model.paramValues[aap.param])
|
2013-04-12 12:28:59 +02:00
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
runline += ', outputs_' + str(aap.alg) + "['" \
|
|
|
|
+ aap.param + "']"
|
2013-04-12 12:28:59 +02:00
|
|
|
for out in alg.outputs:
|
|
|
|
value = model.algOutputs[i][out.name]
|
|
|
|
if value:
|
2013-10-01 20:52:22 +03:00
|
|
|
name = out.name.lower() + '_alg' + str(i)
|
2013-04-12 12:28:59 +02:00
|
|
|
else:
|
|
|
|
name = str(None)
|
2013-10-01 20:52:22 +03:00
|
|
|
runline += ', ' + name
|
2013-04-12 12:28:59 +02:00
|
|
|
i += 1
|
|
|
|
s += multiple
|
2013-10-01 20:52:22 +03:00
|
|
|
s.append(str(runline + ')'))
|
|
|
|
return '\n'.join(s)
|