Skip to content

Commit

Permalink
Status enum used instead of status global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
lindakladivova committed May 7, 2024
1 parent 0737655 commit 8415332
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
23 changes: 10 additions & 13 deletions gui/wxpython/core/gconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -550,8 +550,8 @@ def RunCmd(
):
event = gIgnoredCmdRun(cmd=command)
wx.PostEvent(self, event)

return

else:
# other GRASS commands (r|v|g|...)
try:
Expand Down Expand Up @@ -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:
Expand All @@ -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)

Expand Down Expand Up @@ -660,7 +658,6 @@ def RunCmd(
):
event = gIgnoredCmdRun(cmd=command)
wx.PostEvent(self, event)

return

skipInterface = True
Expand All @@ -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,
Expand Down Expand Up @@ -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))
Expand Down
25 changes: 13 additions & 12 deletions gui/wxpython/history/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
),
)

Expand Down
6 changes: 3 additions & 3 deletions gui/wxpython/lmgr/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions gui/wxpython/main_window/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 11 additions & 7 deletions python/grass/grassdb/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@

import json
import shutil
from enum import Enum
from pathlib import Path

from datetime import datetime
import grass.script as gs
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():
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 8415332

Please sign in to comment.