diff --git a/gui/wxpython/gmodeler/model.py b/gui/wxpython/gmodeler/model.py index 1d75f3869df..a41fd88e000 100644 --- a/gui/wxpython/gmodeler/model.py +++ b/gui/wxpython/gmodeler/model.py @@ -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): @@ -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(r"%(?:\{" + variable + r"\}|" + variable + r")") value = "" if params and "variables" in params: for p in params["variables"]["params"]: @@ -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) @@ -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(r"%(?:\{" + variable + r"\}|" + variable + r")") if not pattern.search(cond): continue value = "" @@ -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(r"%(?:\{" + condVar + r"\}|" + condVar + r")") # for vars()[condVar] in eval(condText): ? vlist = [] if condText[0] == "`" and condText[-1] == "`": diff --git a/gui/wxpython/gmodeler/model_convert.py b/gui/wxpython/gmodeler/model_convert.py index ab9f6885eb2..07735a1b0ee 100644 --- a/gui/wxpython/gmodeler/model_convert.py +++ b/gui/wxpython/gmodeler/model_convert.py @@ -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(r"%(?:\{" + variable + r"\}|" + variable + r")") if pattern.search(cond): value = variables[variable].get("value", "") if variables[variable].get("type", "string") == "string": @@ -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 + pattern = re.compile(r"%(?:\{" + var + r"\}|" + var + r")") + 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}"]' diff --git a/gui/wxpython/gmodeler/model_items.py b/gui/wxpython/gmodeler/model_items.py index c866817ea55..19d8319c944 100644 --- a/gui/wxpython/gmodeler/model_items.py +++ b/gui/wxpython/gmodeler/model_items.py @@ -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(r"%(?:\{" + variable + r"\}|" + variable + r")") value = "" if substitute and "variables" in substitute: for p in substitute["variables"]["params"]: