diff --git a/gui/wxpython/core/gconsole.py b/gui/wxpython/core/gconsole.py index c0bb5bfcdda..4705d639297 100644 --- a/gui/wxpython/core/gconsole.py +++ b/gui/wxpython/core/gconsole.py @@ -41,6 +41,7 @@ from grass.pydispatch.signal import Signal from grass.grassdb import history +from grass.grassdb.history import Status from core import globalvar from core.gcmd import CommandThread, GError, GException @@ -448,14 +449,13 @@ def WriteError(self, text): def UpdateHistory(self, status, runtime=None): """Update command history. - :param enum status enum: status of command + :param enum status: status of command run :param int runtime: duration of command run """ if runtime: - cmd_info = {"runtime": runtime, "status": status} + cmd_info = {"runtime": runtime, "status": status.value} else: - cmd_info = {"status": status} - + cmd_info = {"status": status.value} try: history_path = history.get_current_mapset_gui_history_path() history.update_entry(history_path, cmd_info) @@ -550,8 +550,8 @@ def RunCmd( ): event = gIgnoredCmdRun(cmd=command) wx.PostEvent(self, event) - return + else: # other GRASS commands (r|v|g|...) try: @@ -601,7 +601,6 @@ def RunCmd( parent=self._guiparent, message=_("Module <%s> not found.") % command[0], ) - pymodule = imp.load_source(command[0].replace(".", "_"), pyPath) pymain = inspect.getfullargspec(pymodule.main) if pymain and "giface" in pymain.args: @@ -615,8 +614,7 @@ def RunCmd( GUI( parent=self._guiparent, giface=self._giface ).ParseCommand(command) - self.UpdateHistory(status=history.STATUS_SUCCESS) - + self.UpdateHistory(status=Status.SUCCESS) except GException as e: print(e, file=sys.stderr) @@ -660,7 +658,6 @@ def RunCmd( ): event = gIgnoredCmdRun(cmd=command) wx.PostEvent(self, event) - return skipInterface = True @@ -687,7 +684,7 @@ def RunCmd( if task: # process GRASS command without argument GUI(parent=self._guiparent, giface=self._giface).ParseCommand(command) - self.UpdateHistory(status=history.STATUS_SUCCESS) + self.UpdateHistory(status=Status.SUCCESS) else: self.cmdThread.RunCmd( command, @@ -762,15 +759,15 @@ def OnCmdDone(self, event): ) ) msg = _("Command aborted") - status = history.STATUS_ABORTED + status = Status.ABORTED elif event.returncode != 0: msg = _("Command ended with non-zero return code {returncode}").format( returncode=event.returncode ) - status = history.STATUS_FAILED + status = Status.FAILED else: msg = _("Command finished") - status = history.STATUS_SUCCESS + status = Status.SUCCESS # update command history log by status and runtime duration self.UpdateHistory(status=status, runtime=int(ctime)) diff --git a/gui/wxpython/history/tree.py b/gui/wxpython/history/tree.py index 2971c666555..ead959fbaae 100644 --- a/gui/wxpython/history/tree.py +++ b/gui/wxpython/history/tree.py @@ -41,6 +41,7 @@ from grass.pydispatch.signal import Signal from grass.grassdb import history +from grass.grassdb.history import Status # global variables for node types @@ -116,11 +117,11 @@ def __init__( self._iconTypes = [ TIME_PERIOD, - history.STATUS_ABORTED, - history.STATUS_FAILED, - history.STATUS_RUNNING, - history.STATUS_SUCCESS, - history.STATUS_UNKNOWN, + Status.ABORTED.value, + Status.FAILED.value, + Status.RUNNING.value, + Status.SUCCESS.value, + Status.UNKNOWN.value, ] self._initImages() @@ -165,11 +166,11 @@ def _initImages(self): bmpsize = (16, 16) icons = { TIME_PERIOD: MetaIcon(img="time-period").GetBitmap(bmpsize), - history.STATUS_ABORTED: MetaIcon(img="exclamation-mark").GetBitmap(bmpsize), - history.STATUS_FAILED: MetaIcon(img="cross").GetBitmap(bmpsize), - history.STATUS_RUNNING: MetaIcon(img="circle").GetBitmap(bmpsize), - history.STATUS_SUCCESS: MetaIcon(img="success").GetBitmap(bmpsize), - history.STATUS_UNKNOWN: MetaIcon(img="question-mark").GetBitmap(bmpsize), + Status.ABORTED.value: MetaIcon(img="exclamation-mark").GetBitmap(bmpsize), + Status.FAILED.value: MetaIcon(img="cross").GetBitmap(bmpsize), + Status.RUNNING.value: MetaIcon(img="circle").GetBitmap(bmpsize), + Status.SUCCESS.value: MetaIcon(img="success").GetBitmap(bmpsize), + Status.UNKNOWN.value: MetaIcon(img="question-mark").GetBitmap(bmpsize), } il = wx.ImageList(bmpsize[0], bmpsize[1], mask=False) for each in self._iconTypes: @@ -268,7 +269,7 @@ def _initHistoryModel(self): entry["command_info"].get("status") if entry.get("command_info") and entry["command_info"].get("status") is not None - else history.STATUS_UNKNOWN + else Status.UNKNOWN.value ) # Add command to time period node @@ -396,7 +397,7 @@ def InsertCommand(self, entry): type=COMMAND, name=entry["command"].strip(), timestamp=entry["command_info"]["timestamp"], - status=entry["command_info"].get("status", history.STATUS_UNKNOWN), + status=entry["command_info"].get("status", Status.UNKNOWN.value), ), ) diff --git a/gui/wxpython/lmgr/frame.py b/gui/wxpython/lmgr/frame.py index dd3e196b5ab..96b49780f2e 100644 --- a/gui/wxpython/lmgr/frame.py +++ b/gui/wxpython/lmgr/frame.py @@ -83,7 +83,7 @@ create_location_interactively, ) from grass.grassdb.checks import is_first_time_user -from grass.grassdb import history +from grass.grassdb.history import Status class GMFrame(wx.Frame): @@ -976,9 +976,9 @@ def RunSpecialCmd(self, command): " not supported." % " ".join(command) ) if result == 0: - self._gconsole.UpdateHistory(status=history.STATUS_SUCCESS) + self._gconsole.UpdateHistory(status=Status.SUCCESS) else: - self._gconsole.UpdateHistory(status=history.STATUS_FAILED) + self._gconsole.UpdateHistory(status=Status.FAILED) def RunDisplayCmd(self, command): """Handles display commands. diff --git a/gui/wxpython/main_window/frame.py b/gui/wxpython/main_window/frame.py index a69f40bebed..4dd8b5bf279 100644 --- a/gui/wxpython/main_window/frame.py +++ b/gui/wxpython/main_window/frame.py @@ -89,7 +89,7 @@ create_location_interactively, ) from grass.grassdb.checks import is_first_time_user -from grass.grassdb import history +from grass.grassdb.history import Status class SingleWindowAuiManager(aui.AuiManager): @@ -1113,9 +1113,9 @@ def RunSpecialCmd(self, command): " not supported." % " ".join(command) ) if result == 0: - self._gconsole.UpdateHistory(status=history.STATUS_SUCCESS) + self._gconsole.UpdateHistory(status=Status.SUCCESS) else: - self._gconsole.UpdateHistory(status=history.STATUS_FAILED) + self._gconsole.UpdateHistory(status=Status.FAILED) def RunDisplayCmd(self, command): """Handles display commands. diff --git a/python/grass/grassdb/history.py b/python/grass/grassdb/history.py index 0e87d6e647b..d20628dc820 100644 --- a/python/grass/grassdb/history.py +++ b/python/grass/grassdb/history.py @@ -11,6 +11,7 @@ import json import shutil +from enum import Enum from pathlib import Path from datetime import datetime @@ -18,12 +19,15 @@ from grass.script.utils import parse_key_val -# global status variables -STATUS_ABORTED = "aborted" -STATUS_FAILED = "failed" -STATUS_RUNNING = "running" -STATUS_SUCCESS = "success" -STATUS_UNKNOWN = "unknown" +class Status(Enum): + """Enum representing a set of status constants + that are used to represent various states or command outcomes.""" + + ABORTED = "aborted" + FAILED = "failed" + RUNNING = "running" + SUCCESS = "success" + UNKNOWN = "unknown" def get_current_mapset_gui_history_path(): @@ -286,7 +290,7 @@ def get_initial_command_info(env_run): "mask2d": mask2d_present, "mask3d": mask3d_present, "region": region_settings, - "status": STATUS_RUNNING, + "status": Status.RUNNING.value, } return cmd_info