Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 10 additions & 5 deletions gui/wxpython/gmodeler/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,8 @@ def Validate(self):
sval = pattern.search(value)
if not sval:
continue
var = sval.group(2).strip()[2:-1] # strip '%{...}'
s = sval.group(2).strip()
var = s[2:-1] if s.startswith("%{") else s[1:] # strip curly braces only if present
found = False
for v in variables:
if var.startswith(v):
Expand Down Expand Up @@ -539,7 +540,8 @@ def _substituteFile(self, item, params=None, checkOnly=False):
write = False
variables = self.GetVariables()
for variable in variables:
pattern = re.compile("%{" + variable + "}")
# curly braces are optional
pattern = re.compile("%{?" + variable + "}?")
value = ""
if params and "variables" in params:
for p in params["variables"]["params"]:
Expand All @@ -560,7 +562,8 @@ def _substituteFile(self, item, params=None, checkOnly=False):
pattern = re.compile(r"(.*)(%\{.+})(.*)")
sval = pattern.search(data)
if sval:
var = sval.group(2).strip()[2:-1] # ignore '%{...}'
s = sval.group(2).strip()
var = s[2:-1] if s.startswith("%{") else s[1:] # strip curly braces only if present
cmd = item.GetLog(string=False)[0]
errList.append(cmd + ": " + _("undefined variable '%s'") % var)

Expand Down Expand Up @@ -690,7 +693,8 @@ def Run(self, log, onDone, parent=None):
# substitute variables in condition
variables = self.GetVariables()
for variable in variables:
pattern = re.compile("%{" + variable + "}")
# curly braces are optional
pattern = re.compile("%{?" + variable + "}?")
if not pattern.search(cond):
continue
value = ""
Expand All @@ -713,7 +717,8 @@ def Run(self, log, onDone, parent=None):
# split condition
# TODO: this part needs some better solution
condVar, condText = (x.strip() for x in re.split(r"\s* in \s*", cond))
pattern = re.compile("%{" + condVar + "}")
# curly braces are optional
pattern = re.compile("%{?" + condVar + "}?")
# for vars()[condVar] in eval(condText): ?
vlist = []
if condText[0] == "`" and condText[-1] == "`":
Expand Down
10 changes: 6 additions & 4 deletions gui/wxpython/gmodeler/model_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def _writeItem(self, item, ignoreBlock=True, variables={}):
# substitute condition
cond = item.GetLabel()
for variable in self.model.GetVariables():
pattern = re.compile("%{" + variable + "}")
# curly braces are optional
pattern = re.compile("%{?" + variable + "}?")
if pattern.search(cond):
value = variables[variable].get("value", "")
if variables[variable].get("type", "string") == "string":
Expand Down Expand Up @@ -969,14 +970,15 @@ def _substitutePythonParamValue(
# check for variables
formattedVar = False
for var in variables["vars"]:
pattern = re.compile("%{" + var + "}")
found = pattern.search(value)
# curly braces are optional
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curly braces enclosing variables were introduced in version 8.5. I assumed this change was meant to ensure backward compatibility, which would be nice. I tested to load model without braces (attached in the issue), the conversion to Python works. But attempt to run the model (directly from the modeler, not converted Python code) fails - no variables are substituted.

p.in.pdal -e -o --overwrite input=%path/dmp1g/%tile.laz output=%tile_dmp method=mean type=FCELL zscale=1.0 iscale=1.0 resolution=%resolution dimension=z
...

Backward compatibility wasn’t explicitly mentioned in the issue. I think it’s fine to keep this change in the PR, but in that case the model execution should be fixed as well.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the latest commits I changed every re.compile in the gmodeler folder have optional curly braces.

I managed to both run the model directly and have the values parametrized in the generated python code, but I had a leftover empty raster in my map at the end of the process:

Failed to run command 'd.rast map=%tile_chm'. Details:

GRASS_INFO_ERROR(70582,1): Raster map <%tile_chm> not found
GRASS_INFO_END(70582,1)
Failed to run command 'd.rast map=%tile_chm'. Details:

GRASS_INFO_ERROR(70665,1): Raster map <%tile_chm> not found
GRASS_INFO_END(70665,1)

I couldn't see, where the d.rast function is called, it's not in the model or the python code.

It doesn't break it, so I didn't look for a solution yet.

pattern = re.compile("%{?" + var + "}?")
found = pattern.search(parameterizedValue)
if found:
foundVar = True
if found.end() != len(value):
formattedVar = True
parameterizedValue = pattern.sub(
"{options['" + var + "']}", value
"{options['" + var + "']}", parameterizedValue
)
else:
parameterizedValue = f'options["{var}"]'
Expand Down
3 changes: 2 additions & 1 deletion gui/wxpython/gmodeler/model_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ def GetLog(self, string=True, substitute=None):

# order variables by length
for variable in sorted(variables, key=len, reverse=True):
pattern = re.compile("%{" + variable + "}")
# curly braces are optional
pattern = re.compile("%{?" + variable + "}?")
value = ""
if substitute and "variables" in substitute:
for p in substitute["variables"]["params"]:
Expand Down