Skip to content

Commit

Permalink
Incorporate suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
tmszi committed Dec 30, 2023
1 parent 86a9609 commit c64ab82
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 81 deletions.
110 changes: 110 additions & 0 deletions gui/wxpython/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1198,5 +1198,115 @@ def is_shell_running():
return True


def parse_mapcalc_cmd(command):
"""Parse r.mapcalc/r3.mapcalc module command
>>> parse_mapcalc_cmd(command="r.mapcalc map = 1")
"r.mapcalc expression='map = 1'"
>>> parse_mapcalc_cmd(command="r.mapcalc map = 1")
"r.mapcalc expression='map = 1'"
>>> parse_mapcalc_cmd(command="r.mapcalc map=1")
"r.mapcalc expression='map=1'"
>>> parse_mapcalc_cmd(command="r.mapcalc map = a - b")
"r.mapcalc expression='map = a - b'"
>>> parse_mapcalc_cmd(command="r.mapcalc expression=map = a - b")
"r.mapcalc expression='map = a - b'"
>>> parse_mapcalc_cmd(command="r.mapcalc expression=map = a - b")
"r.mapcalc expression='map = a - b'"
>>> cmd = "r.mapcalc expr='map = a - b' region=clip --overwrite"
>>> parse_mapcalc_cmd(command=cmd)
"r.mapcalc --overwrite expr='map = a - b' region=clip"
>>> cmd = 'r.mapcalc expr="map = a - b" region=clip --overwrite'
>>> parse_mapcalc_cmd(command=cmd)
"r.mapcalc --overwrite expr='map = a - b' region=clip"
>>> cmd = "r.mapcalc -s map = (a - b) / c region=clip --overwrite --verbose"
>>> parse_mapcalc_cmd(command=cmd)
"r.mapcalc -s --overwrite --verbose expression='map = (a - b) / c' region=clip"
:param str command: r.mapcalc command string
:return str: parsed r.mapcalc command string
"""
flags = []
others_params_args = []
expr_param_regex = r"expression=|expr="
flag_regex = r"^-[a-z]|^--overwrite|^--quiet|^--verbose|^--o|^--v|^--h"

command = split(command)
module = command.pop(0)

for arg in command[:]:
flag = re.search(flag_regex, arg)
if flag:
flags.append(command.pop(command.index(flag.group())))
elif "region=" in arg or "file=" in arg or "seed=" in arg:
others_params_args.append(command.pop(command.index(arg)))

cmd = " ".join(command)
expr_param = re.search(expr_param_regex, cmd)
if not expr_param:
expr_param_name = expr_param_regex.split("|")[0]
else:
# Remove expression param
command = split(cmd.replace(expr_param.group(), ""))
expr_param_name = expr_param.group()
# Add quotes
if "'" not in cmd or '"' not in cmd:
cmd = f"'{' '.join(command)}'"
expression_param_arg = f"{expr_param_name}{cmd}"

return " ".join(
[
module,
*flags,
expression_param_arg,
*others_params_args,
]
)


def replace_module_cmd_special_flags(command):
"""Replace module command special flags short version with
full version
Flags:
--o -> --overwrite
--q -> --quiet
--v -> --verbose
>>> cmd = "r.mapcalc -s --o --v expression='map = 1' region=clip"
>>> replace_module_cmd_special_flags(command=cmd)
"r.mapcalc -s --overwrite --verbose expression='map = 1' region=clip"
>>> cmd = "r.mapcalc -s --o --q expression='map = 1' region=clip"
>>> replace_module_cmd_special_flags(command=cmd)
"r.mapcalc -s --overwrite --quiet expression='map = 1' region=clip"
:param str command: module command string
:return str: module command string with replaced flags
"""
flags_regex = re.compile(r"(--o(\s+|$))|(--q(\s+|$))|(--v(\s+|$))")
replace = {
"--o": "--overwrite ",
"--q": "--quiet ",
"--v": "--verbose ",
}
return re.sub(
flags_regex,
lambda flag: replace[flag.group().strip()],
command,
)


if __name__ == "__main__":
sys.exit(doc_test())
88 changes: 7 additions & 81 deletions gui/wxpython/history/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

from core import globalvar
from core.gcmd import GError, GException
from core.utils import split
from core.utils import (
parse_mapcalc_cmd,
replace_module_cmd_special_flags,
split,
)
from gui_core.forms import GUI
from gui_core.treeview import CTreeView
from history.tree import HistoryBrowserTree
Expand Down Expand Up @@ -88,87 +92,9 @@ def _getSelectedNode(self):
return None
return selection[0]

def _parseMapcalcCmd(self, command):
"""Parse r.mapcalc/r3.mapcalc module command
Examples:
r.mapcalc foo = 1 -> r.mapcalc expression=foo=1
r.mapcalc foo=1 -> r.mapcalc expression=foo=1
r.mapcalc expression=foo=1 -> r.mapcalc expression=foo=1
r.mapcalc expression=foo = 1 -> r.mapcalc expression=foo=1
r.mapcalc expression="foo = 1" -> r.mapcalc expression="foo=1"
r.mapcalc expression="foo=1" -> r.mapcalc expression="foo=1"
r.mapcalc expression='foo = 1' -> r.mapcalc expression='foo=1'
r.mapcalc expression='foo=1' -> r.mapcalc expression='foo=1'
:param str command: r.mapcalc command string
:return str command: parsed r.mapcalc command string
"""
flags = []
others_params_args = []
expression_param = "expression="
command = command.split()

for arg in command[:]:
if arg.startswith("-"):
flags.append(command.pop(command.index(arg)))
elif "region=" in arg or "file=" in arg or "seed=" in arg:
others_params_args.append(command.pop(command.index(arg)))

if expression_param not in "".join(command):
expression_param_arg = f"{expression_param}{''.join(command[1:])}"
else:
expression_param_arg = "".join(command[1:])

command = " ".join(
[
command[0],
*flags,
expression_param_arg,
*others_params_args,
]
)
return command

def _refreshTree(self):
self._tree.SetModel(self._model.GetModel())

def _replaceModuleCmdSpecialFlags(self, command):
"""Replace module command special flags short version with
full version
Flags:
--o -> --overwrite
--q -> --quiet
--v -> --verbose
:param str command: module command string
:return str: module command string with replaced flags
"""
flags_regex = re.compile(r"(--o(\s+|$))|(--q(\s+|$))|(--v(\s+|$))")
replace = {
"--o": "--overwrite ",
"--q": "--quiet ",
"--v": "--verbose ",
}
return re.sub(
flags_regex,
lambda flag: replace[flag.group().strip()],
command,
)

def UpdateHistoryModelFromScratch(self):
"""Update the model from scratch and refresh the tree"""
self._model.CreateModel()
Expand Down Expand Up @@ -198,8 +124,8 @@ def Run(self, node=None):
self.runIgnoredCmdPattern.emit(cmd=split(command))
return
if re.compile(r"^r[3]?\.mapcalc").search(command):
command = self._parseMapcalcCmd(command)
command = self._replaceModuleCmdSpecialFlags(command)
command = parse_mapcalc_cmd(command)
command = replace_module_cmd_special_flags(command)
lst = split(command)
try:
GUI(parent=self, giface=self._giface).ParseCommand(lst)
Expand Down

0 comments on commit c64ab82

Please sign in to comment.