-
-
Notifications
You must be signed in to change notification settings - Fork 307
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
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
32c15ab
replace log file toggle functionality by exporting the whole history …
50abc74
flake8
7f25280
format function
96f78ea
copy whole history
9d04533
better docs
956b66a
black
286067d
flake8
2240bff
exception handling moves to CopyHistory method
b836ca1
both exceptions handled
e097f25
emits notification only if copying is successful
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
|
||||||
|
||||||
|
@@ -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() | ||||||
|
||||||
|
@@ -234,7 +226,7 @@ def _layout(self): | |||||
) | ||||||
|
||||||
cmdBtnSizer.Add( | ||||||
self.btnCmdProtocol, | ||||||
self.btnCmdExportHistory, | ||||||
proportion=1, | ||||||
flag=wx.ALIGN_CENTER | ||||||
| wx.ALIGN_CENTER_VERTICAL | ||||||
|
@@ -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: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Maybe? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?