Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wxGUI: Console: Export history of executed commands #2682

Merged
merged 10 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 33 additions & 60 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,35 @@ 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
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,
)

if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
try:
self.cmdPrompt.CopyHistory(path)
except IOError as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move the exception handling in the CopyHistory. Check if the error type is appropriate, it seems OSError should be used instead.

Copy link
Contributor Author

@lindakarlovska lindakarlovska Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, I moved it to the CopyHistory. I think that the error type is okay according to https://docs.python.org/3/library/shutil.html : Changed in version 3.3: IOError used to be raised instead of OSError. Added follow_symlinks argument. Now returns dst.
But when I looked at the examples of how to use shutil.copyfile I found out that they use handling of different error types: https://www.geeksforgeeks.org/python-shutil-copyfile-method/
So difficult to decide... I would probably keep the IOError or just put the general except Exception as e: . What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
except IOError as e:
except (IOError, OSError) as e:

Maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. A good compromise.

GError(
_(
"Unable to copy .wxgui_history file to {}'.\n\nDetails: {}"
).format(path, e)
)
)

self.showNotification.emit(
message=_("Command log saved to '{}'".format(self.cmdFileProtocol))
)
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,
self.showNotification.emit(
message=_("Command history saved to '{}'".format(path))
)
if dlg.ShowModal() == wx.ID_OK:
self.cmdFileProtocol = dlg.GetPath()
else:
wx.CallAfter(self.btnCmdProtocol.SetValue, False)

dlg.Destroy()

dlg.Destroy()
event.Skip()

def OnCmdRun(self, event):
Expand Down
12 changes: 12 additions & 0 deletions 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 Down Expand Up @@ -139,6 +140,17 @@ def _runCmd(self, cmdString):
self.OnCmdErase(None)
self.ShowStatusText("")

def CopyHistory(self, targetFile):
"""Copy history file to the target location"""
env = grass.gisenv()
historyFile = os.path.join(
env["GISDBASE"],
env["LOCATION_NAME"],
env["MAPSET"],
".wxgui_history",
)
shutil.copyfile(historyFile, targetFile)

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