Skip to content

Commit

Permalink
wxGUI: Console: Export history of executed commands (#2682)
Browse files Browse the repository at this point in the history
Replaces log file toggle functionality by exporting the whole history of executed commands
  • Loading branch information
lindakarlovska committed Jan 5, 2023
1 parent 36a968b commit 87e1f78
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 70 deletions.
87 changes: 26 additions & 61 deletions gui/wxpython/gui_core/goutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
)
from core.globalvar import CheckWxVersion, wxPythonPhoenix
from gui_core.prompt import GPromptSTC
from gui_core.wrap import Button, ClearButton, ToggleButton, StaticText, StaticBox
from gui_core.wrap import Button, ClearButton, StaticText, StaticBox
from core.settings import UserSettings


Expand Down Expand Up @@ -154,29 +154,21 @@ def __init__(
self.btnOutputSave.SetToolTip(_("Save output window content to the file"))
self.btnCmdAbort = Button(parent=self.panelProgress, id=wx.ID_STOP)
self.btnCmdAbort.SetToolTip(_("Abort running command"))
self.btnCmdProtocol = ToggleButton(
parent=self.panelOutput,
id=wx.ID_ANY,
label=_("&Log file"),
size=self.btnCmdClear.GetSize(),
)
self.btnCmdProtocol.SetToolTip(
_(
"Toggle to save list of executed commands into "
"a file; content saved when switching off."
)
self.btnCmdExportHistory = Button(parent=self.panelOutput, id=wx.ID_ANY)
self.btnCmdExportHistory.SetLabel(_("&Export history"))
self.btnCmdExportHistory.SetToolTip(
_("Export history of executed commands to a file")
)
self.cmdFileProtocol = None

if not self._gcstyle & GC_PROMPT:
self.btnCmdClear.Hide()
self.btnCmdProtocol.Hide()
self.btnCmdExportHistory.Hide()

self.btnCmdClear.Bind(wx.EVT_BUTTON, self.cmdPrompt.OnCmdErase)
self.btnOutputClear.Bind(wx.EVT_BUTTON, self.OnOutputClear)
self.btnOutputSave.Bind(wx.EVT_BUTTON, self.OnOutputSave)
self.btnCmdAbort.Bind(wx.EVT_BUTTON, self._gconsole.OnCmdAbort)
self.btnCmdProtocol.Bind(wx.EVT_TOGGLEBUTTON, self.OnCmdProtocol)
self.btnCmdExportHistory.Bind(wx.EVT_BUTTON, self.OnCmdExportHistory)

self._layout()

Expand Down Expand Up @@ -234,7 +226,7 @@ def _layout(self):
)

cmdBtnSizer.Add(
self.btnCmdProtocol,
self.btnCmdExportHistory,
proportion=1,
flag=wx.ALIGN_CENTER
| wx.ALIGN_CENTER_VERTICAL
Expand Down Expand Up @@ -473,54 +465,27 @@ def OnCmdProgress(self, event):
self.progressbar.SetValue(event.value)
event.Skip()

def CmdProtocolSave(self):
"""Save list of manually entered commands into a text log file"""
if self.cmdFileProtocol is None:
return # it should not happen

try:
with open(self.cmdFileProtocol, "a") as output:
cmds = self.cmdPrompt.GetCommands()
output.write("\n".join(cmds))
if len(cmds) > 0:
output.write("\n")
except IOError as e:
GError(
_("Unable to write file '{filePath}'.\n\nDetails: {error}").format(
filePath=self.cmdFileProtocol, error=e
)
)

self.showNotification.emit(
message=_("Command log saved to '{}'".format(self.cmdFileProtocol))
def OnCmdExportHistory(self, event):
"""Export the history of executed commands stored
in a .wxgui_history file to a selected file."""
dlg = wx.FileDialog(
self,
message=_("Save file as..."),
defaultFile="grass_cmd_log.txt",
wildcard=_("{txt} (*.txt)|*.txt|{files} (*)|*").format(
txt=_("Text files"), files=_("Files")
),
style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT,
)
self.cmdFileProtocol = None

def OnCmdProtocol(self, event=None):
"""Save commands into file"""
if not event.IsChecked():
# stop capturing commands, save list of commands to the
# protocol file
self.CmdProtocolSave()
else:
# start capturing commands
self.cmdPrompt.ClearCommands()
# ask for the file
dlg = wx.FileDialog(
self,
message=_("Save file as..."),
defaultFile="grass_cmd_log.txt",
wildcard=_("%(txt)s (*.txt)|*.txt|%(files)s (*)|*")
% {"txt": _("Text files"), "files": _("Files")},
style=wx.FD_SAVE,
)
if dlg.ShowModal() == wx.ID_OK:
self.cmdFileProtocol = dlg.GetPath()
else:
wx.CallAfter(self.btnCmdProtocol.SetValue, False)

dlg.Destroy()
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
if self.cmdPrompt.CopyHistory(path):
self.showNotification.emit(
message=_("Command history saved to '{}'".format(path))
)

dlg.Destroy()
event.Skip()

def OnCmdRun(self, event):
Expand Down
24 changes: 23 additions & 1 deletion gui/wxpython/gui_core/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import difflib
import codecs
import sys
import shutil

import wx
import wx.stc
Expand All @@ -33,7 +34,7 @@

from core import globalvar
from core import utils
from core.gcmd import EncodeString, DecodeString
from core.gcmd import EncodeString, DecodeString, GError


class GPrompt(object):
Expand Down Expand Up @@ -139,6 +140,27 @@ def _runCmd(self, cmdString):
self.OnCmdErase(None)
self.ShowStatusText("")

def CopyHistory(self, targetFile):
"""Copy history file to the target location.
Returns True if file is successfully copied."""
env = grass.gisenv()
historyFile = os.path.join(
env["GISDBASE"],
env["LOCATION_NAME"],
env["MAPSET"],
".wxgui_history",
)
try:
shutil.copyfile(historyFile, targetFile)
except (IOError, OSError) as e:
GError(
_("Unable to copy file {} to {}'.\n\nDetails: {}").format(
historyFile, targetFile, e
)
)
return False
return True

def GetCommands(self):
"""Get list of launched commands"""
return self.commands
Expand Down
4 changes: 0 additions & 4 deletions gui/wxpython/lmgr/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2257,10 +2257,6 @@ def OnCloseWindowOrExit(self, event):

def _closeWindow(self, event):
"""Close wxGUI"""
# save command protocol if actived
if self.goutput.btnCmdProtocol.GetValue():
self.goutput.CmdProtocolSave()

if not self.currentPage:
self._auimgr.UnInit()
self.Destroy()
Expand Down
4 changes: 0 additions & 4 deletions gui/wxpython/main_window/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2296,10 +2296,6 @@ def OnCloseWindowOrExit(self, event):

def _closeWindow(self, event):
"""Close wxGUI"""
# save command protocol if actived
if self.goutput.btnCmdProtocol.GetValue():
self.goutput.CmdProtocolSave()

if not self.currentPage:
self._auimgr.UnInit()
self.Destroy()
Expand Down

0 comments on commit 87e1f78

Please sign in to comment.