Browse Source

Command prompt - Log files produces empty log files, see https://trac.osgeo.org/grass/ticket/3657

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@73417 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 6 years ago
parent
commit
bc360eed9c
2 changed files with 16 additions and 18 deletions
  1. 14 14
      gui/wxpython/gui_core/goutput.py
  2. 2 4
      gui/wxpython/gui_core/prompt.py

+ 14 - 14
gui/wxpython/gui_core/goutput.py

@@ -173,6 +173,7 @@ class GConsoleWindow(wx.SplitterWindow):
             size=self.btnCmdClear.GetSize())
         self.btnCmdProtocol.SetToolTip(_("Toggle to save list of executed commands into "
                                          "a file; content saved when switching off."))
+        self.cmdFileProtocol = None
 
         if not self._gcstyle & GC_PROMPT:
             self.btnCmdClear.Hide()
@@ -484,24 +485,23 @@ class GConsoleWindow(wx.SplitterWindow):
 
     def CmdProtocolSave(self):
         """Save list of manually entered commands into a text log file"""
-        if not hasattr(self, 'cmdFileProtocol'):
+        if self.cmdFileProtocol is None:
             return  # it should not happen
 
         try:
-            output = open(self.cmdFileProtocol, "a")
-            cmds = self.cmdPrompt.GetCommands()
-            output.write('\n'.join(cmds))
-            if len(cmds) > 0:
-                output.write('\n')
+            with open(self.cmdFileProtocol, "a") as output:
+                cmds = self.cmdPrompt.GetCommands()
+                output.write(os.linesep.join(cmds))
+                if len(cmds) > 0:
+                    output.write(os.linesep)
         except IOError as e:
-            GError(_("Unable to write file '%(filePath)s'.\n\nDetails: %(error)s") %
-                   {'filePath': self.cmdFileProtocol, 'error': e})
-        finally:
-            output.close()
-
-        message = _("Command log saved to '%s'") % self.cmdFileProtocol
-        self.showNotification.emit(message=message)
-        del self.cmdFileProtocol
+            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))
+        )
+        self.cmdFileProtocol = None
 
     def OnCmdProtocol(self, event=None):
         """Save commands into file"""

+ 2 - 4
gui/wxpython/gui_core/prompt.py

@@ -111,8 +111,6 @@ class GPrompt(object):
         if not cmdString:
             return
 
-        self.commands.append(cmdString)  # trace commands
-
         # parse command into list
         try:
             cmd = utils.split(str(cmdString))
@@ -122,8 +120,6 @@ class GPrompt(object):
 
         self.promptRunCmd.emit(cmd=cmd)
 
-        # add command to history & clean prompt
-        # self.UpdateCmdHistory(cmd)
         self.OnCmdErase(None)
         self.ShowStatusText('')
 
@@ -300,6 +296,8 @@ class GPromptSTC(GPrompt, wx.stc.StyledTextCtrl):
         """
         # add command to history
         self.cmdbuffer.append(cmd)
+        # update also traced commands
+        self.commands.append(cmd)
 
         # keep command history to a managable size
         if len(self.cmdbuffer) > 200: