Added the file backup functionality in the code style fixer.

Tue, 29 Apr 2014 18:22:44 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 29 Apr 2014 18:22:44 +0200
changeset 3549
96ebf42cd960
parent 3547
c2a187c63209
child 3550
8019baa33b0c

Added the file backup functionality in the code style fixer.

APIs/Python3/eric5.api file | annotate | diff | comparison | revisions
Documentation/Help/source.qch file | annotate | diff | comparison | revisions
Documentation/Source/eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleChecker.html file | annotate | diff | comparison | revisions
Documentation/Source/eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleFixer.html file | annotate | diff | comparison | revisions
Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleChecker.py file | annotate | diff | comparison | revisions
Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleCheckerDialog.py file | annotate | diff | comparison | revisions
Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py file | annotate | diff | comparison | revisions
--- a/APIs/Python3/eric5.api	Tue Apr 29 18:02:02 2014 +0200
+++ b/APIs/Python3/eric5.api	Tue Apr 29 18:22:44 2014 +0200
@@ -3808,7 +3808,7 @@
 eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleFixer.CodeStyleFixer.fixIssue?4(line, pos, message)
 eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleFixer.CodeStyleFixer.mutualStartswith?4(b)
 eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleFixer.CodeStyleFixer.saveFile?4(encoding)
-eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleFixer.CodeStyleFixer?1(filename, sourceLines, fixCodes, noFixCodes, maxLineLength, inPlace, eol)
+eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleFixer.CodeStyleFixer?1(filename, sourceLines, fixCodes, noFixCodes, maxLineLength, inPlace, eol, backup=False)
 eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleFixer.FixableCodeStyleIssues?7
 eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleFixer.IndentationWrapper.SKIP_TOKENS?7
 eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleFixer.IndentationWrapper.pep8Expected?4()
Binary file Documentation/Help/source.qch has changed
--- a/Documentation/Source/eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleChecker.html	Tue Apr 29 18:02:02 2014 +0200
+++ b/Documentation/Source/eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleChecker.html	Tue Apr 29 18:22:44 2014 +0200
@@ -135,7 +135,7 @@
         excludeMessages (str), includeMessages (str), repeatMessages
         (bool), fixCodes (str), noFixCodes (str), fixIssues (bool),
         maxLineLength (int), hangClosing (bool), docType (str), errors
-        (list of str), eol (str), encoding (str))
+        (list of str), eol (str), encoding (str), backup (bool))
 </dd>
 </dl><dl>
 <dt>Returns:</dt>
--- a/Documentation/Source/eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleFixer.html	Tue Apr 29 18:02:02 2014 +0200
+++ b/Documentation/Source/eric5.Plugins.CheckerPlugins.CodeStyleChecker.CodeStyleFixer.html	Tue Apr 29 18:22:44 2014 +0200
@@ -235,7 +235,7 @@
 </table>
 <a NAME="CodeStyleFixer.__init__" ID="CodeStyleFixer.__init__"></a>
 <h4>CodeStyleFixer (Constructor)</h4>
-<b>CodeStyleFixer</b>(<i>filename, sourceLines, fixCodes, noFixCodes, maxLineLength, inPlace, eol</i>)
+<b>CodeStyleFixer</b>(<i>filename, sourceLines, fixCodes, noFixCodes, maxLineLength, inPlace, eol, backup=False</i>)
 <p>
         Constructor
 </p><dl>
@@ -263,6 +263,10 @@
 </dd><dt><i>eol</i></dt>
 <dd>
 end of line character(s) (string)
+</dd><dt><i>backup</i></dt>
+<dd>
+flag indicating to create a backup before fixing
+            anything (boolean)
 </dd>
 </dl><a NAME="CodeStyleFixer.__codeMatch" ID="CodeStyleFixer.__codeMatch"></a>
 <h4>CodeStyleFixer.__codeMatch</h4>
--- a/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleChecker.py	Tue Apr 29 18:02:02 2014 +0200
+++ b/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleChecker.py	Tue Apr 29 18:22:44 2014 +0200
@@ -94,14 +94,14 @@
         excludeMessages (str), includeMessages (str), repeatMessages
         (bool), fixCodes (str), noFixCodes (str), fixIssues (bool),
         maxLineLength (int), hangClosing (bool), docType (str), errors
-        (list of str), eol (str), encoding (str))
+        (list of str), eol (str), encoding (str), backup (bool))
     @return tuple of stats (dict) and results (tuple for each found violation
         of style (tuple of lineno (int), position (int), text (str), ignored
             (bool), fixed (bool), autofixing (bool), fixedMsg (str)))
     """
     excludeMessages, includeMessages, \
         repeatMessages, fixCodes, noFixCodes, fixIssues, maxLineLength, \
-        hangClosing, docType, errors, eol, encoding = args
+        hangClosing, docType, errors, eol, encoding, backup = args
     
     stats = {}
     # avoid 'Encoding declaration in unicode string' exception on Python2
@@ -116,7 +116,7 @@
         from CodeStyleFixer import CodeStyleFixer
         fixer = CodeStyleFixer(
             filename, source, fixCodes, noFixCodes,
-            maxLineLength, True, eol)  # always fix in place
+            maxLineLength, True, eol, backup)  # always fix in place
     else:
         fixer = None
     
--- a/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleCheckerDialog.py	Tue Apr 29 18:02:02 2014 +0200
+++ b/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleCheckerDialog.py	Tue Apr 29 18:22:44 2014 +0200
@@ -420,7 +420,9 @@
             self.__itms.append(itm)
         
         eol = self.__getEol(self.filename)
-        args = self.__options + [errors, eol, encoding]
+        args = self.__options + [
+            errors, eol, encoding, Preferences.getEditor("CreateBackupFile")
+        ]
         self.styleCheckService.styleCheck(
             None, self.filename, self.source, args)
 
--- a/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py	Tue Apr 29 18:02:02 2014 +0200
+++ b/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleFixer.py	Tue Apr 29 18:22:44 2014 +0200
@@ -48,7 +48,7 @@
     Class implementing a fixer for certain code style issues.
     """
     def __init__(self, filename, sourceLines, fixCodes, noFixCodes,
-                 maxLineLength, inPlace, eol):
+                 maxLineLength, inPlace, eol, backup=False):
         """
         Constructor
         
@@ -62,6 +62,8 @@
         @param maxLineLength maximum allowed line length (integer)
         @param inPlace flag indicating to modify the file in place (boolean)
         @param eol end of line character(s) (string)
+        @param backup flag indicating to create a backup before fixing
+            anything (boolean)
         """
         super(CodeStyleFixer, self).__init__()
         
@@ -78,14 +80,13 @@
         self.__indentWord = self.__getIndentWord()
         
         if inPlace:
-            # TODO: Do a backup before any changes depending on
-            # 'CreateBackupFile' editor config setting.
-            pass
+            self.__createBackup = backup
         else:
             self.__origName = self.__filename
             self.__filename = os.path.join(
                 os.path.dirname(self.__filename),
                 "fixed_" + os.path.basename(self.__filename))
+            self.__createBackup = False
         self.__eol = eol
 
         self.__fixes = {
@@ -188,6 +189,23 @@
             # no need to write
             return
         
+        if self.__createBackup:
+            # create a backup file before writing any changes
+            if os.path.islink(self.__filename):
+                bfn = '{0}~'.format(os.path.realpath(self.__filename))
+            else:
+                bfn = '{0}~'.format(self.__filename)
+            try:
+                os.remove(bfn)
+            except EnvironmentError:
+                # if there was an error, ignore it
+                pass
+            try:
+                os.rename(self.__filename, bfn)
+            except EnvironmentError:
+                # if there was an error, ignore it
+                pass
+        
         txt = "".join(self.__source)
         try:
             if sys.version_info[0] == 3:

eric ide

mercurial