Skip to content

Commit ffa87a2

Browse files
committedApr 12, 2013
[sextante] reintroduced Model->Python conversion
1 parent f451d24 commit ffa87a2

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed
 

‎python/plugins/sextante/modeler/ModelerAlgorithmProvider.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* *
1717
***************************************************************************
1818
"""
19+
1920
__author__ = 'Victor Olaya'
2021
__date__ = 'August 2012'
2122
__copyright__ = '(C) 2012, Victor Olaya'
@@ -26,6 +27,7 @@
2627
from PyQt4.QtGui import *
2728
import os.path
2829
from sextante.core.SextanteConfig import SextanteConfig, Setting
30+
from sextante.modeler.SaveAsPythonScriptAction import SaveAsPythonScriptAction
2931
from sextante.core.SextanteLog import SextanteLog
3032
from sextante.modeler.ModelerUtils import ModelerUtils
3133
from sextante.modeler.ModelerAlgorithm import ModelerAlgorithm
@@ -41,7 +43,7 @@ class ModelerAlgorithmProvider(AlgorithmProvider):
4143
def __init__(self):
4244
AlgorithmProvider.__init__(self)
4345
self.actions = [CreateNewModelAction()]
44-
self.contextMenuActions = [EditModelAction(), DeleteModelAction()]
46+
self.contextMenuActions = [EditModelAction(), DeleteModelAction(), SaveAsPythonScriptAction()]
4547

4648
def initializeSettings(self):
4749
AlgorithmProvider.initializeSettings(self)
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
SaveAsPythonScriptAction.py
6+
---------------------
7+
Date : April 2013
8+
Copyright : (C) 2013 by Victor Olaya
9+
Email : volayaf at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
import sys
20+
from sextante.parameters.ParameterMultipleInput import ParameterMultipleInput
21+
22+
__author__ = 'Victor Olaya'
23+
__date__ = 'April 2013'
24+
__copyright__ = '(C) 2013, Victor Olaya'
25+
# This will get replaced with a git SHA1 when you do a git archive
26+
__revision__ = '$Format:%H$'
27+
28+
from sextante.gui.ContextAction import ContextAction
29+
from sextante.modeler.ModelerAlgorithm import ModelerAlgorithm,\
30+
AlgorithmAndParameter
31+
from sextante.script.ScriptUtils import ScriptUtils
32+
from PyQt4.QtCore import *
33+
from PyQt4.QtGui import *
34+
35+
class SaveAsPythonScriptAction(ContextAction):
36+
37+
def __init__(self):
38+
self.name="Save as Python script"
39+
40+
def isEnabled(self):
41+
return isinstance(self.alg, ModelerAlgorithm)
42+
43+
def execute(self):
44+
filename = str(QFileDialog.getSaveFileName(None, "Save Script", ScriptUtils.scriptsFolder(), "Python scripts (*.py)"))
45+
46+
if filename:
47+
if not filename.endswith(".py"):
48+
filename += ".py"
49+
text = self.translateToPythonCode(self.alg)
50+
try:
51+
fout = open(filename, "w")
52+
fout.write(text)
53+
fout.close()
54+
if filename.replace("\\","/").startswith(ScriptUtils.scriptsFolder().replace("\\","/")):
55+
self.toolbox.updateTree()
56+
except:
57+
QMessageBox.warning(self,
58+
self.tr("I/O error"),
59+
self.tr("Unable to save edits. Reason:\n %1").arg(unicode(sys.exc_info()[1]))
60+
)
61+
62+
def translateToPythonCode(self, model):
63+
s = ["##" + model.name + "=name"]
64+
for param in model.parameters:
65+
s.append(str(param.getAsScriptCode().lower()))
66+
i = 0
67+
for outs in model.algOutputs:
68+
for out in outs.keys():
69+
if outs[out]:
70+
s.append("##" + out.lower() + "_alg" + str(i) +"=" + model.getOutputType(i, out))
71+
i += 1
72+
i = 0
73+
iMultiple = 0
74+
for alg in model.algs:
75+
multiple= []
76+
runline = "outputs_" + str(i) + "=Sextante.runalg(\"" + alg.commandLineName() + "\""
77+
for param in alg.parameters:
78+
aap = model.algParameters[i][param.name]
79+
if aap == None:
80+
runline += ", None"
81+
elif isinstance(param, ParameterMultipleInput):
82+
value = model.paramValues[aap.param]
83+
tokens = value.split(";")
84+
layerslist = []
85+
for token in tokens:
86+
iAlg, paramname = token.split("|")
87+
if float(iAlg) == float(AlgorithmAndParameter.PARENT_MODEL_ALGORITHM):
88+
if model.ismodelparam(paramname):
89+
value = paramname.lower()
90+
else:
91+
value = model.paramValues[paramname]
92+
else:
93+
value = "outputs_" + str(iAlg) + "['" + paramname +"']"
94+
layerslist.append(str(value))
95+
96+
multiple.append("multiple_" + str(iMultiple) +"=[" + ",".join(layerslist) + "]")
97+
runline +=", \";\".join(multiple_" + str(iMultiple) + ") "
98+
else:
99+
if float(aap.alg) == float(AlgorithmAndParameter.PARENT_MODEL_ALGORITHM):
100+
if model.ismodelparam(aap.param):
101+
runline += ", " + aap.param.lower()
102+
else:
103+
runline += ", " + str(model.paramValues[aap.param])
104+
else:
105+
runline += ", outputs_" + str(aap.alg) + "['" + aap.param +"']"
106+
for out in alg.outputs:
107+
value = model.algOutputs[i][out.name]
108+
if value:
109+
name = out.name.lower() + "_alg" + str(i)
110+
else:
111+
name = str(None)
112+
runline += ", " + name
113+
i += 1
114+
s += multiple
115+
s.append(str(runline + ")"))
116+
return "\n".join(s)

0 commit comments

Comments
 (0)
Please sign in to comment.