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

Graphical modeler: export variables to Python #3701

Closed
wants to merge 58 commits into from
Closed
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
a23851c
first step, split 'frame' into 'panel' and 'canvas' modules
landam Jun 4, 2023
10809a9
gmodeler dockable
landam Jun 4, 2023
04306ce
fix menu, WIP
landam Jun 5, 2023
f6545f3
black applied
landam Jun 5, 2023
94c1d2d
Merge branch 'main' into gmodeler_panel
landam Nov 6, 2023
b7096fc
merge manually #3188
landam Nov 6, 2023
281db48
merge manually #2991
landam Nov 6, 2023
d0ca042
fix indetation error
landam Nov 6, 2023
0b67f5a
log in main statusbar
landam Nov 6, 2023
15d5447
close page WIP
landam Nov 6, 2023
b5e7fe4
fix page closing
landam Nov 6, 2023
14101cd
base class wip
landam Nov 7, 2023
a710fb3
remove currentDisplay
landam Nov 7, 2023
de66788
WIP
landam Nov 7, 2023
9438c1c
undock/dock
landam Nov 7, 2023
1ea3826
rename methods map -> main
landam Nov 7, 2023
4e45154
add missing module
landam Nov 8, 2023
1d8ee1f
+_pgnumDict()
landam Nov 8, 2023
abcdc9b
rename notebook to _mainnotebook to avoid variable override
landam Nov 8, 2023
b591ca7
new icon modeler-settings, add model properies into toolbar'
landam Nov 8, 2023
dcad156
black applied
landam Nov 8, 2023
a6089f3
fix multiwindow layout
landam Nov 8, 2023
0fb7a94
remove unused import
landam Nov 8, 2023
3f90773
remove unused import
landam Nov 8, 2023
a182199
remove unused import
landam Nov 8, 2023
4b7e5f5
disable E1101
landam Nov 8, 2023
c1a7959
black applied
landam Nov 8, 2023
38611d8
pylint: change from inline solution to .pyintrc
landam Nov 8, 2023
f49a473
fix pyint ignored-classes
landam Nov 8, 2023
5a1ba75
Merge branch 'main' into gmodeler_panel
landam Mar 2, 2024
0be08d3
merge manually a71b72a
landam Mar 2, 2024
4284066
merge manually f393525
landam Mar 2, 2024
e5c9482
merge manually e07531f
landam Mar 2, 2024
610d042
merge manually 0ad33cc
landam Mar 2, 2024
4f45ead
merge manually 76c1ac5
landam Mar 2, 2024
d816fdd
disable ModelChanged() in OnSize() when window is dockable
landam Mar 2, 2024
bbeb272
use IsDockable() instead of _dockable
landam Mar 2, 2024
33e8219
hide shortcuts when window is dockable
landam Mar 2, 2024
eb91883
menu initial implementation
landam Mar 3, 2024
a19672d
one word menu item
landam Mar 3, 2024
14e9835
fix menu replace
landam Mar 3, 2024
dc25c86
menu index simplified
landam Mar 3, 2024
ccc39f8
unique menu shortcuts
landam Mar 3, 2024
410159f
Merge branch 'main' into gmodeler_panel
landam Mar 3, 2024
82e8817
fix menu for undocked windows
landam Mar 3, 2024
33b1119
reuse menu objects if already existing
landam Mar 3, 2024
83a4a4c
mapDispDocking renamed to docking
landam Mar 22, 2024
78ad9a7
fix 'MapPanel' object has no attribute '_saved_output_img_size'
landam Mar 22, 2024
714f76a
Merge branch 'main' into gmodeler_panel
landam Mar 22, 2024
a3125d3
Merge branch 'main' into gmodeler_panel
landam Apr 3, 2024
d387432
ignore security alert - consider using safer ast.literal_eval
landam Apr 3, 2024
59be93e
nosec: fix rule id
landam Apr 3, 2024
846a383
fix missing imports (TextEntryDialogs)
landam Apr 3, 2024
40f7361
propagate variables in UI
landam Apr 3, 2024
9b028aa
variable propagated into commands
landam Apr 3, 2024
75bffa4
fix var substitution in cmd
landam Apr 3, 2024
564e6b4
merge from main
landam May 11, 2024
d6756c6
remove unused code
landam May 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions gui/wxpython/gmodeler/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3170,6 +3170,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 @@ -3183,7 +3189,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 @@ -3227,7 +3233,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 @@ -3251,7 +3258,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 @@ -3278,6 +3285,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 @@ -3326,8 +3356,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 @@ -3402,6 +3435,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