Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wxGUI: Export variables to Python in Graphical Modeler #3702

Merged
merged 12 commits into from
Jun 3, 2024
50 changes: 46 additions & 4 deletions gui/wxpython/gmodeler/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3333,6 +3333,12 @@ def __init__(self, fd, model, grassAPI="script"):
self._writePython()

def _getStandardizedOption(self, string):
"""Return GRASS standardized option based on specified string.

:param string: input string to be converted

:return: GRASS standardized option as a string or None if not converted
"""
if string == "raster":
return "G_OPT_R_MAP"
elif string == "vector":
Expand All @@ -3346,7 +3352,7 @@ def _getStandardizedOption(self, string):
elif string == "region":
return "G_OPT_M_REGION"

return ""
return None

def _writePython(self):
"""Write model to file"""
Expand Down Expand Up @@ -3390,7 +3396,8 @@ def _writePython(self):

modelItems = self.model.GetItems(ModelAction)
for item in modelItems:
for flag in item.GetParameterizedParams()["flags"]:
parametrizedParams = item.GetParameterizedParams()
for flag in parametrizedParams["flags"]:
if flag["label"]:
desc = flag["label"]
else:
Expand All @@ -3414,7 +3421,7 @@ def _writePython(self):
self.fd.write("# % answer: False\n")
self.fd.write("# %end\n")

for param in item.GetParameterizedParams()["params"]:
for param in parametrizedParams["params"]:
if param["label"]:
desc = param["label"]
else:
Expand All @@ -3441,6 +3448,29 @@ def _writePython(self):
self.fd.write("# % answer: {}\n".format(param["value"]))
self.fd.write("# %end\n")

# variables
for vname, vdesc in self.model.GetVariables().items():
self.fd.write("# %option")
optionType = self._getStandardizedOption(vdesc["type"])
if optionType:
self.fd.write(" {}".format(optionType))
self.fd.write("\n")
self.fd.write(
r"""# % key: {param_name}
# % description: {description}
# % required: yes
""".format(
param_name=vname,
description=vdesc["description"],
)
)
if optionType is None and vdesc["type"]:
self.fd.write("# % type: {}\n".format(vdesc["type"]))

if vdesc["value"]:
self.fd.write("# % answer: {}\n".format(vdesc["value"]))
self.fd.write("# %end\n")

# import modules
self.fd.write(
r"""
Expand Down Expand Up @@ -3489,8 +3519,11 @@ def cleanup():
self.fd.write(" pass\n")

self.fd.write("\ndef main(options, flags):\n")
modelVars = self.model.GetVariables()
for item in self.model.GetItems(ModelAction):
self._writeItem(item, variables=item.GetParameterizedParams())
modelParams = item.GetParameterizedParams()
modelParams["vars"] = modelVars
self._writeItem(item, variables=modelParams)

self.fd.write(" return 0\n")

Expand Down Expand Up @@ -3565,6 +3598,15 @@ def _getPythonActionCmd(self, item, task, cmdIndent, variables={}):
if name in parameterizedParams:
foundVar = True
value = 'options["{}"]'.format(self._getParamName(name, item))
else:
# check for variables
for var in variables["vars"]:
pattern = re.compile("%" + var)
if pattern.search(value):
foundVar = True
value = pattern.sub("{options['" + var + "']}", value)
if foundVar:
value = 'f"' + value + '"'

if (
foundVar
Expand Down
Loading