Improved the way the Python variant of a source file is detected (thanks to Tobias Rzepka).

Sat, 29 Mar 2014 20:12:47 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 29 Mar 2014 20:12:47 +0100
changeset 3446
5a670e55adbb
parent 3441
1b07f996e9ee
child 3447
64fb656bd334

Improved the way the Python variant of a source file is detected (thanks to Tobias Rzepka).

APIs/Python3/eric5.api file | annotate | diff | comparison | revisions
Debugger/DebugUI.py file | annotate | diff | comparison | revisions
Documentation/Help/source.qch file | annotate | diff | comparison | revisions
Documentation/Help/source.qhp file | annotate | diff | comparison | revisions
Documentation/Source/eric5.DataViews.PyProfileDialog.html file | annotate | diff | comparison | revisions
Documentation/Source/eric5.Project.ProjectSourcesBrowser.html file | annotate | diff | comparison | revisions
Documentation/Source/eric5.QScintilla.Editor.html file | annotate | diff | comparison | revisions
Documentation/Source/eric5.Utilities.__init__.html file | annotate | diff | comparison | revisions
Plugins/PluginCodeStyleChecker.py file | annotate | diff | comparison | revisions
Plugins/PluginSyntaxChecker.py file | annotate | diff | comparison | revisions
Plugins/PluginTabnanny.py file | annotate | diff | comparison | revisions
QScintilla/Editor.py file | annotate | diff | comparison | revisions
UI/UserInterface.py file | annotate | diff | comparison | revisions
Utilities/__init__.py file | annotate | diff | comparison | revisions
ViewManager/ViewManager.py file | annotate | diff | comparison | revisions
--- a/APIs/Python3/eric5.api	Fri Mar 28 19:52:34 2014 +0100
+++ b/APIs/Python3/eric5.api	Sat Mar 29 20:12:47 2014 +0100
@@ -6905,9 +6905,11 @@
 eric5.QScintilla.Editor.Editor.highlight?4(line=None, error=False, syntaxError=False)
 eric5.QScintilla.Editor.Editor.highlightVisible?4()
 eric5.QScintilla.Editor.Editor.indentLineOrSelection?4()
+eric5.QScintilla.Editor.Editor.isJavascriptFile?4()
 eric5.QScintilla.Editor.Editor.isLastEditPositionAvailable?4()
 eric5.QScintilla.Editor.Editor.isPy2File?4()
 eric5.QScintilla.Editor.Editor.isPy3File?4()
+eric5.QScintilla.Editor.Editor.isPyFile?4()
 eric5.QScintilla.Editor.Editor.isRubyFile?4()
 eric5.QScintilla.Editor.Editor.isSpellCheckRegion?4(pos)
 eric5.QScintilla.Editor.Editor.joinLines?4()
@@ -8470,6 +8472,7 @@
 eric5.Utilities.decode?4(text)
 eric5.Utilities.decodeBytes?4(buffer)
 eric5.Utilities.decodeString?4(text)
+eric5.Utilities.determinePythonVersion?4(filename, source, editor=None)
 eric5.Utilities.direntries?4(path, filesonly=False, pattern=None, followsymlinks=True, checkStop=None)
 eric5.Utilities.encode?4(text, orig_coding)
 eric5.Utilities.escape_entities?4(m, map=_escape_map)
@@ -8505,6 +8508,7 @@
 eric5.Utilities.isExecutable?4(exe)
 eric5.Utilities.isinpath?4(file)
 eric5.Utilities.joinext?4(prefix, ext)
+eric5.Utilities.jsCheckSyntax?4(file, codestring="")
 eric5.Utilities.linesep?4()
 eric5.Utilities.normabsjoinpath?4(a, *p)
 eric5.Utilities.normabspath?4(path)
--- a/Debugger/DebugUI.py	Fri Mar 28 19:52:34 2014 +0100
+++ b/Debugger/DebugUI.py	Sat Mar 29 20:12:47 2014 +0100
@@ -831,7 +831,7 @@
                     break
             else:
                 if editor.isPy2File():
-                    cap = self.debugServer.getClientCapabilities('Python')
+                    cap = self.debugServer.getClientCapabilities('Python2')
                 elif editor.isPy3File():
                     cap = self.debugServer.getClientCapabilities('Python3')
                 elif editor.isRubyFile():
@@ -869,7 +869,7 @@
         if editor is None:
             return
         
-        if editor.isPy3File() or editor.isPy2File() or editor.isRubyFile():
+        if editor.isPyFile() or editor.isRubyFile():
             if editor.curLineHasBreakpoint():
                 self.dbgEditBpAct.setEnabled(True)
             else:
Binary file Documentation/Help/source.qch has changed
--- a/Documentation/Help/source.qhp	Fri Mar 28 19:52:34 2014 +0100
+++ b/Documentation/Help/source.qhp	Sat Mar 29 20:12:47 2014 +0100
@@ -4360,6 +4360,7 @@
       <keyword name="Editor.__getCharacter" id="Editor.__getCharacter" ref="eric5.QScintilla.Editor.html#Editor.__getCharacter" />
       <keyword name="Editor.__getCodeCoverageFile" id="Editor.__getCodeCoverageFile" ref="eric5.QScintilla.Editor.html#Editor.__getCodeCoverageFile" />
       <keyword name="Editor.__getMacroName" id="Editor.__getMacroName" ref="eric5.QScintilla.Editor.html#Editor.__getMacroName" />
+      <keyword name="Editor.__getPyVersion" id="Editor.__getPyVersion" ref="eric5.QScintilla.Editor.html#Editor.__getPyVersion" />
       <keyword name="Editor.__ignoreSpellingAlways" id="Editor.__ignoreSpellingAlways" ref="eric5.QScintilla.Editor.html#Editor.__ignoreSpellingAlways" />
       <keyword name="Editor.__indentLine" id="Editor.__indentLine" ref="eric5.QScintilla.Editor.html#Editor.__indentLine" />
       <keyword name="Editor.__indentSelection" id="Editor.__indentSelection" ref="eric5.QScintilla.Editor.html#Editor.__indentSelection" />
@@ -4554,9 +4555,11 @@
       <keyword name="Editor.highlight" id="Editor.highlight" ref="eric5.QScintilla.Editor.html#Editor.highlight" />
       <keyword name="Editor.highlightVisible" id="Editor.highlightVisible" ref="eric5.QScintilla.Editor.html#Editor.highlightVisible" />
       <keyword name="Editor.indentLineOrSelection" id="Editor.indentLineOrSelection" ref="eric5.QScintilla.Editor.html#Editor.indentLineOrSelection" />
+      <keyword name="Editor.isJavascriptFile" id="Editor.isJavascriptFile" ref="eric5.QScintilla.Editor.html#Editor.isJavascriptFile" />
       <keyword name="Editor.isLastEditPositionAvailable" id="Editor.isLastEditPositionAvailable" ref="eric5.QScintilla.Editor.html#Editor.isLastEditPositionAvailable" />
       <keyword name="Editor.isPy2File" id="Editor.isPy2File" ref="eric5.QScintilla.Editor.html#Editor.isPy2File" />
       <keyword name="Editor.isPy3File" id="Editor.isPy3File" ref="eric5.QScintilla.Editor.html#Editor.isPy3File" />
+      <keyword name="Editor.isPyFile" id="Editor.isPyFile" ref="eric5.QScintilla.Editor.html#Editor.isPyFile" />
       <keyword name="Editor.isRubyFile" id="Editor.isRubyFile" ref="eric5.QScintilla.Editor.html#Editor.isRubyFile" />
       <keyword name="Editor.isSpellCheckRegion" id="Editor.isSpellCheckRegion" ref="eric5.QScintilla.Editor.html#Editor.isSpellCheckRegion" />
       <keyword name="Editor.joinLines" id="Editor.joinLines" ref="eric5.QScintilla.Editor.html#Editor.joinLines" />
@@ -9222,6 +9225,7 @@
       <keyword name="ProjectSourcesBrowser.__addSourceDirectory" id="ProjectSourcesBrowser.__addSourceDirectory" ref="eric5.Project.ProjectSourcesBrowser.html#ProjectSourcesBrowser.__addSourceDirectory" />
       <keyword name="ProjectSourcesBrowser.__addSourceFiles" id="ProjectSourcesBrowser.__addSourceFiles" ref="eric5.Project.ProjectSourcesBrowser.html#ProjectSourcesBrowser.__addSourceFiles" />
       <keyword name="ProjectSourcesBrowser.__closeAllWindows" id="ProjectSourcesBrowser.__closeAllWindows" ref="eric5.Project.ProjectSourcesBrowser.html#ProjectSourcesBrowser.__closeAllWindows" />
+      <keyword name="ProjectSourcesBrowser.__createJavaScriptPopupMenus" id="ProjectSourcesBrowser.__createJavaScriptPopupMenus" ref="eric5.Project.ProjectSourcesBrowser.html#ProjectSourcesBrowser.__createJavaScriptPopupMenus" />
       <keyword name="ProjectSourcesBrowser.__createPythonPopupMenus" id="ProjectSourcesBrowser.__createPythonPopupMenus" ref="eric5.Project.ProjectSourcesBrowser.html#ProjectSourcesBrowser.__createPythonPopupMenus" />
       <keyword name="ProjectSourcesBrowser.__createRubyPopupMenus" id="ProjectSourcesBrowser.__createRubyPopupMenus" ref="eric5.Project.ProjectSourcesBrowser.html#ProjectSourcesBrowser.__createRubyPopupMenus" />
       <keyword name="ProjectSourcesBrowser.__deleteFile" id="ProjectSourcesBrowser.__deleteFile" ref="eric5.Project.ProjectSourcesBrowser.html#ProjectSourcesBrowser.__deleteFile" />
@@ -13207,6 +13211,7 @@
       <keyword name="decodeBytes" id="decodeBytes" ref="eric5.Utilities.__init__.html#decodeBytes" />
       <keyword name="decodeString" id="decodeString" ref="eric5.Utilities.__init__.html#decodeString" />
       <keyword name="decryptData" id="decryptData" ref="eric5.Utilities.crypto.py3AES.html#decryptData" />
+      <keyword name="determinePythonVersion" id="determinePythonVersion" ref="eric5.Utilities.__init__.html#determinePythonVersion" />
       <keyword name="direntries" id="direntries" ref="eric5.Utilities.__init__.html#direntries" />
       <keyword name="displayString" id="displayString" ref="eric5.Plugins.PluginVcsMercurial.html#displayString" />
       <keyword name="displayString" id="displayString" ref="eric5.Plugins.PluginVcsPySvn.html#displayString" />
@@ -13468,6 +13473,7 @@
       <keyword name="iter_child_nodes" id="iter_child_nodes" ref="eric5.Utilities.pyflakes.checker.html#iter_child_nodes" />
       <keyword name="iter_child_nodes" id="iter_child_nodes" ref="eric5.UtilitiesPython2.pyflakes.checker.html#iter_child_nodes" />
       <keyword name="joinext" id="joinext" ref="eric5.Utilities.__init__.html#joinext" />
+      <keyword name="jsCheckSyntax" id="jsCheckSyntax" ref="eric5.Utilities.__init__.html#jsCheckSyntax" />
       <keyword name="jsclbr (Module)" id="jsclbr (Module)" ref="eric5.Utilities.ClassBrowsers.jsclbr.html" />
       <keyword name="largefiles (Module)" id="largefiles (Module)" ref="eric5.Plugins.VcsPlugins.vcsMercurial.LargefilesExtension.largefiles.html" />
       <keyword name="linesep" id="linesep" ref="eric5.Utilities.__init__.html#linesep" />
--- a/Documentation/Source/eric5.DataViews.PyProfileDialog.html	Fri Mar 28 19:52:34 2014 +0100
+++ b/Documentation/Source/eric5.DataViews.PyProfileDialog.html	Sat Mar 29 20:12:47 2014 +0100
@@ -34,7 +34,7 @@
 <td>Class implementing a custom QTreeWidgetItem to allow sorting on numeric values.</td>
 </tr><tr>
 <td><a href="#PyProfileDialog">PyProfileDialog</a></td>
-<td>Class implementing a dialog to display the results of a syntax check run.</td>
+<td>Class implementing a dialog to display the results of a profiling run.</td>
 </tr>
 </table>
 <h3>Functions</h3>
@@ -109,7 +109,7 @@
 <a NAME="PyProfileDialog" ID="PyProfileDialog"></a>
 <h2>PyProfileDialog</h2>
 <p>
-    Class implementing a dialog to display the results of a syntax check run.
+    Class implementing a dialog to display the results of a profiling run.
 </p>
 <h3>Derived from</h3>
 QDialog, Ui_PyProfileDialog
--- a/Documentation/Source/eric5.Project.ProjectSourcesBrowser.html	Fri Mar 28 19:52:34 2014 +0100
+++ b/Documentation/Source/eric5.Project.ProjectSourcesBrowser.html	Sat Mar 29 20:12:47 2014 +0100
@@ -94,6 +94,9 @@
 <td><a href="#ProjectSourcesBrowser.__closeAllWindows">__closeAllWindows</a></td>
 <td>Private method to close all project related windows.</td>
 </tr><tr>
+<td><a href="#ProjectSourcesBrowser.__createJavaScriptPopupMenus">__createJavaScriptPopupMenus</a></td>
+<td>Privat method to generate the popup menus for a Python project.</td>
+</tr><tr>
 <td><a href="#ProjectSourcesBrowser.__createPythonPopupMenus">__createPythonPopupMenus</a></td>
 <td>Privat method to generate the popup menus for a Python project.</td>
 </tr><tr>
@@ -201,6 +204,11 @@
 <b>__closeAllWindows</b>(<i></i>)
 <p>
         Private method to close all project related windows.
+</p><a NAME="ProjectSourcesBrowser.__createJavaScriptPopupMenus" ID="ProjectSourcesBrowser.__createJavaScriptPopupMenus"></a>
+<h4>ProjectSourcesBrowser.__createJavaScriptPopupMenus</h4>
+<b>__createJavaScriptPopupMenus</b>(<i></i>)
+<p>
+        Privat method to generate the popup menus for a Python project.
 </p><a NAME="ProjectSourcesBrowser.__createPythonPopupMenus" ID="ProjectSourcesBrowser.__createPythonPopupMenus"></a>
 <h4>ProjectSourcesBrowser.__createPythonPopupMenus</h4>
 <b>__createPythonPopupMenus</b>(<i></i>)
--- a/Documentation/Source/eric5.QScintilla.Editor.html	Fri Mar 28 19:52:34 2014 +0100
+++ b/Documentation/Source/eric5.QScintilla.Editor.html	Sat Mar 29 20:12:47 2014 +0100
@@ -290,6 +290,9 @@
 <td><a href="#Editor.__getMacroName">__getMacroName</a></td>
 <td>Private method to select a macro name from the list of macros.</td>
 </tr><tr>
+<td><a href="#Editor.__getPyVersion">__getPyVersion</a></td>
+<td>Private method to return the Python main version (2 or 3) or 0 if it's not a Python file at all.</td>
+</tr><tr>
 <td><a href="#Editor.__ignoreSpellingAlways">__ignoreSpellingAlways</a></td>
 <td>Private to always ignore the word below the spelling context menu.</td>
 </tr><tr>
@@ -872,15 +875,21 @@
 <td><a href="#Editor.indentLineOrSelection">indentLineOrSelection</a></td>
 <td>Public slot to indent the current line or current selection.</td>
 </tr><tr>
+<td><a href="#Editor.isJavascriptFile">isJavascriptFile</a></td>
+<td>Public method to return a flag indicating a Javascript file.</td>
+</tr><tr>
 <td><a href="#Editor.isLastEditPositionAvailable">isLastEditPositionAvailable</a></td>
 <td>Public method to check, if a last edit position is available.</td>
 </tr><tr>
 <td><a href="#Editor.isPy2File">isPy2File</a></td>
-<td>Public method to return a flag indicating a Python file.</td>
+<td>Public method to return a flag indicating a Python2 file.</td>
 </tr><tr>
 <td><a href="#Editor.isPy3File">isPy3File</a></td>
 <td>Public method to return a flag indicating a Python3 file.</td>
 </tr><tr>
+<td><a href="#Editor.isPyFile">isPyFile</a></td>
+<td>Public method to return a flag indicating a Python (2 or 3) file.</td>
+</tr><tr>
 <td><a href="#Editor.isRubyFile">isRubyFile</a></td>
 <td>Public method to return a flag indicating a Ruby file.</td>
 </tr><tr>
@@ -1631,6 +1640,17 @@
 Tuple of macro name and a flag, indicating, if the user
             pressed ok or canceled the operation. (string, boolean)
 </dd>
+</dl><a NAME="Editor.__getPyVersion" ID="Editor.__getPyVersion"></a>
+<h4>Editor.__getPyVersion</h4>
+<b>__getPyVersion</b>(<i></i>)
+<p>
+        Private method to return the Python main version (2 or 3) or 0 if it's
+        not a Python file at all.
+</p><dl>
+<dt>Returns:</dt>
+<dd>
+Python version (2 or 3) or 0 if it's not a Python file (int)
+</dd>
 </dl><a NAME="Editor.__ignoreSpellingAlways" ID="Editor.__ignoreSpellingAlways"></a>
 <h4>Editor.__ignoreSpellingAlways</h4>
 <b>__ignoreSpellingAlways</b>(<i></i>)
@@ -3379,7 +3399,17 @@
 <b>indentLineOrSelection</b>(<i></i>)
 <p>
         Public slot to indent the current line or current selection.
-</p><a NAME="Editor.isLastEditPositionAvailable" ID="Editor.isLastEditPositionAvailable"></a>
+</p><a NAME="Editor.isJavascriptFile" ID="Editor.isJavascriptFile"></a>
+<h4>Editor.isJavascriptFile</h4>
+<b>isJavascriptFile</b>(<i></i>)
+<p>
+        Public method to return a flag indicating a Javascript file.
+</p><dl>
+<dt>Returns:</dt>
+<dd>
+flag indicating a Javascript file (boolean)
+</dd>
+</dl><a NAME="Editor.isLastEditPositionAvailable" ID="Editor.isLastEditPositionAvailable"></a>
 <h4>Editor.isLastEditPositionAvailable</h4>
 <b>isLastEditPositionAvailable</b>(<i></i>)
 <p>
@@ -3393,11 +3423,11 @@
 <h4>Editor.isPy2File</h4>
 <b>isPy2File</b>(<i></i>)
 <p>
-        Public method to return a flag indicating a Python file.
+        Public method to return a flag indicating a Python2 file.
 </p><dl>
 <dt>Returns:</dt>
 <dd>
-flag indicating a Python file (boolean)
+flag indicating a Python2 file (boolean)
 </dd>
 </dl><a NAME="Editor.isPy3File" ID="Editor.isPy3File"></a>
 <h4>Editor.isPy3File</h4>
@@ -3409,6 +3439,16 @@
 <dd>
 flag indicating a Python3 file (boolean)
 </dd>
+</dl><a NAME="Editor.isPyFile" ID="Editor.isPyFile"></a>
+<h4>Editor.isPyFile</h4>
+<b>isPyFile</b>(<i></i>)
+<p>
+        Public method to return a flag indicating a Python (2 or 3) file.
+</p><dl>
+<dt>Returns:</dt>
+<dd>
+flag indicating a Python (2 or 3) file (boolean)
+</dd>
 </dl><a NAME="Editor.isRubyFile" ID="Editor.isRubyFile"></a>
 <h4>Editor.isRubyFile</h4>
 <b>isRubyFile</b>(<i></i>)
--- a/Documentation/Source/eric5.Utilities.__init__.html	Fri Mar 28 19:52:34 2014 +0100
+++ b/Documentation/Source/eric5.Utilities.__init__.html	Sat Mar 29 20:12:47 2014 +0100
@@ -64,6 +64,9 @@
 <td><a href="#decodeString">decodeString</a></td>
 <td>Function to decode a string containing Unicode encoded characters.</td>
 </tr><tr>
+<td><a href="#determinePythonVersion">determinePythonVersion</a></td>
+<td>Function to determine the python version of a given file.</td>
+</tr><tr>
 <td><a href="#direntries">direntries</a></td>
 <td>Function returning a list of all files and directories.</td>
 </tr><tr>
@@ -169,6 +172,9 @@
 <td><a href="#joinext">joinext</a></td>
 <td>Function to join a file extension to a path.</td>
 </tr><tr>
+<td><a href="#jsCheckSyntax">jsCheckSyntax</a></td>
+<td>Function to check a Javascript source file for syntax errors.</td>
+</tr><tr>
 <td><a href="#linesep">linesep</a></td>
 <td>Function to return the lineseparator used by the editor.</td>
 </tr><tr>
@@ -501,6 +507,31 @@
 </dl>
 <div align="right"><a href="#top">Up</a></div>
 <hr /><hr />
+<a NAME="determinePythonVersion" ID="determinePythonVersion"></a>
+<h2>determinePythonVersion</h2>
+<b>determinePythonVersion</b>(<i>filename, source, editor=None</i>)
+<p>
+    Function to determine the python version of a given file.
+</p><dl>
+<dt><i>filename</i></dt>
+<dd>
+name of the file with extension (str)
+</dd><dt><i>source</i></dt>
+<dd>
+of the file (str)
+</dd><dt><i>editor=</i></dt>
+<dd>
+reference to the editor, if the file is opened
+        already (Editor object)
+</dd>
+</dl><dl>
+<dt>Returns:</dt>
+<dd>
+Python version if file is Python2 or Python3 (int)
+</dd>
+</dl>
+<div align="right"><a href="#top">Up</a></div>
+<hr /><hr />
 <a NAME="direntries" ID="direntries"></a>
 <h2>direntries</h2>
 <b>direntries</b>(<i>path, filesonly=False, pattern=None, followsymlinks=True, checkStop=None</i>)
@@ -1173,6 +1204,29 @@
 </dl>
 <div align="right"><a href="#top">Up</a></div>
 <hr /><hr />
+<a NAME="jsCheckSyntax" ID="jsCheckSyntax"></a>
+<h2>jsCheckSyntax</h2>
+<b>jsCheckSyntax</b>(<i>file, codestring=""</i>)
+<p>
+    Function to check a Javascript source file for syntax errors.
+</p><dl>
+<dt><i>file</i></dt>
+<dd>
+source filename (string)
+</dd><dt><i>codestring</i></dt>
+<dd>
+string containing the code to check (string)
+</dd>
+</dl><dl>
+<dt>Returns:</dt>
+<dd>
+A tuple indicating status (True = an error was found), the
+        file name, the line number and the error message (boolean, string,
+        string, string). The values are only valid, if the status is True.
+</dd>
+</dl>
+<div align="right"><a href="#top">Up</a></div>
+<hr /><hr />
 <a NAME="linesep" ID="linesep"></a>
 <h2>linesep</h2>
 <b>linesep</b>(<i></i>)
--- a/Plugins/PluginCodeStyleChecker.py	Fri Mar 28 19:52:34 2014 +0100
+++ b/Plugins/PluginCodeStyleChecker.py	Sat Mar 29 20:12:47 2014 +0100
@@ -266,8 +266,7 @@
         if menuName == "Checks":
             if not self.__editorAct in menu.actions():
                 menu.addAction(self.__editorAct)
-            self.__editorAct.setEnabled(
-                editor.isPy3File() or editor.isPy2File())
+            self.__editorAct.setEnabled(editor.isPyFile())
     
     def __editorCodeStyleCheck(self):
         """
--- a/Plugins/PluginSyntaxChecker.py	Fri Mar 28 19:52:34 2014 +0100
+++ b/Plugins/PluginSyntaxChecker.py	Sat Mar 29 20:12:47 2014 +0100
@@ -253,8 +253,7 @@
             if not self.__editorAct in menu.actions():
                 menu.addAction(self.__editorAct)
             self.__editorAct.setEnabled(
-                editor.isPy3File() or 
-                editor.isPy2File() or 
+                editor.isPyFile() or 
                 editor.isJavascriptFile()
             )
     
--- a/Plugins/PluginTabnanny.py	Fri Mar 28 19:52:34 2014 +0100
+++ b/Plugins/PluginTabnanny.py	Sat Mar 29 20:12:47 2014 +0100
@@ -252,8 +252,7 @@
         if menuName == "Checks":
             if not self.__editorAct in menu.actions():
                 menu.addAction(self.__editorAct)
-            self.__editorAct.setEnabled(
-                editor.isPy3File() or editor.isPy2File())
+            self.__editorAct.setEnabled(editor.isPyFile())
     
     def __editorTabnanny(self):
         """
--- a/QScintilla/Editor.py	Fri Mar 28 19:52:34 2014 +0100
+++ b/QScintilla/Editor.py	Sat Mar 29 20:12:47 2014 +0100
@@ -326,9 +326,8 @@
                     if not res:
                         raise IOError()
                 self.readFile(self.fileName, True)
-                bindName = self.__bindName(self.text(0))
-                self.__bindLexer(bindName)
-                self.__bindCompleter(bindName)
+                self.__bindLexer(self.fileName)
+                self.__bindCompleter(self.fileName)
                 self.__autoSyntaxCheck(useText=False)
                 self.isResourcesFile = self.fileName.endswith(".qrc")
                 
@@ -1487,28 +1486,27 @@
             self.SCN_STYLENEEDED.disconnect(self.__styleNeeded)
         
         language = ""
-        if self.project.isOpen() and self.project.isProjectFile(filename):
-            language = self.project.getEditorLexerAssoc(
-                os.path.basename(filename))
-        if not language:
-            ext = os.path.splitext(filename)[1]
-            if ext in [".py", ".pyw"]:
-                if self.isPy3File():
-                    language = "Python3"
-                elif self.isPy2File():
-                    language = "Python2"
-                else:
-                    # default is Python 3
-                    language = "Python3"
+        basename = os.path.basename(filename)
+        if not self.filetype:
+            if self.project.isOpen() and self.project.isProjectFile(filename):
+                language = self.project.getEditorLexerAssoc(basename)
+            if not language:
+                language = Preferences.getEditorLexerAssoc(basename)
+            if not language:
+                bindName = self.__bindName(self.text(0))
+                language = Preferences.getEditorLexerAssoc(bindName)
+            if language == "Python":
+                # correction for Python
+                pyVer = Utilities.determinePythonVersion(
+                    filename, self.text(0), self)
+                language = "Python{0}".format(pyVer)
+            if language in ['Python2', 'Python3']:
+                self.filetype = language
             else:
-                filename = os.path.basename(filename)
-                language = Preferences.getEditorLexerAssoc(filename)
-                if language == "Python":
-                    # correction for Python
-                    if self.isPy2File():
-                        language = "Python2"
-                    else:
-                        language = "Python3"
+                self.filetype = ""
+        else:
+            language = self.filetype
+        
         if language.startswith("Pygments|"):
             pyname = language.split("|", 1)[1]
             language = ""
@@ -1616,10 +1614,9 @@
         filename = os.path.basename(filename)
         apiLanguage = Preferences.getEditorLexerAssoc(filename)
         if apiLanguage == "":
-            if self.isPy3File():
-                apiLanguage = "Python3"
-            elif self.isPy2File():
-                apiLanguage = "Python2"
+            pyVer = self.__getPyVersion()
+            if pyVer:
+                apiLanguage = "Python{0}".format(pyVer)
             elif self.isRubyFile():
                 apiLanguage = "Ruby"
         
@@ -1751,12 +1748,9 @@
         """
         ftype = self.filetype
         if not ftype:
-            ftype = self.getFileTypeByFlag()
-        if not ftype:
-            if self.isPy3File():
-                ftype = "Python3"
-            elif self.isPy2File():
-                ftype = "Python2"
+            pyVer = self.__getPyVersion()
+            if pyVer:
+                ftype = "Python{0}".format(pyVer)
             elif self.isRubyFile():
                 ftype = "Ruby"
             else:
@@ -1771,51 +1765,32 @@
         @return current encoding (string)
         """
         return self.encoding
-        
+    
+    def __getPyVersion(self):
+        """
+        Private method to return the Python main version (2 or 3) or 0 if it's
+        not a Python file at all.
+        
+        @return Python version (2 or 3) or 0 if it's not a Python file (int)
+        """
+        return Utilities.determinePythonVersion(
+            self.fileName, self.text(0), self)
+    
+    def isPyFile(self):
+        """
+        Public method to return a flag indicating a Python (2 or 3) file.
+        
+        @return flag indicating a Python (2 or 3) file (boolean)
+        """
+        return self.__getPyVersion() in [2, 3]
+    
     def isPy2File(self):
         """
-        Public method to return a flag indicating a Python file.
-        
-        @return flag indicating a Python file (boolean)
-        """
-        if self.filetype in ["Python", "Python2"]:
-            return True
-        
-        if self.filetype == "":
-            # 1) Determine by first line
-            line0 = self.text(0)
-            if line0.startswith("#!") and \
-               ("python2" in line0 or
-                    ("python" in line0 and not "python3" in line0)):
-                self.filetype = "Python2"
-                return True
-            
-            if self.fileName is not None:
-                ext = os.path.splitext(self.fileName)[1]
-                if ext in [".py", ".pyw"]:
-                    # 2) .py and .pyw are ambiguous; determine from project
-                    if Preferences.getProject("DeterminePyFromProject") and \
-                       self.project.isOpen() and \
-                       self.project.isProjectFile(self.fileName):
-                        isProjectPy2 = \
-                            self.project.getProjectLanguage() in ["Python",
-                                                                  "Python2"]
-                        if isProjectPy2:
-                            self.filetype = "Python2"
-                        return isProjectPy2
-                    else:
-                        # 3) determine by compiling the sources
-                        syntaxError = Utilities.py2compile(
-                            self.fileName, checkFlakes=False)[0]
-                        if not syntaxError:
-                            self.filetype = "Python2"
-                            return True
-                
-                if ext in self.dbs.getExtensions('Python2'):
-                    self.filetype = "Python2"
-                    return True
-        
-        return False
+        Public method to return a flag indicating a Python2 file.
+        
+        @return flag indicating a Python2 file (boolean)
+        """
+        return self.__getPyVersion() == 2
 
     def isPy3File(self):
         """
@@ -1823,42 +1798,7 @@
         
         @return flag indicating a Python3 file (boolean)
         """
-        if self.filetype in ["Python3"]:
-            return True
-        
-        if self.filetype == "":
-            # 1) Determine by first line
-            line0 = self.text(0)
-            if line0.startswith("#!") and \
-               "python3" in line0:
-                self.filetype = "Python3"
-                return True
-            
-            if self.fileName is not None:
-                ext = os.path.splitext(self.fileName)[1]
-                if ext in [".py", ".pyw"]:
-                    # 2) .py and .pyw are ambiguous; determine from project
-                    if Preferences.getProject("DeterminePyFromProject") and \
-                       self.project.isOpen() and \
-                       self.project.isProjectFile(self.fileName):
-                        isProjectPy3 = \
-                            self.project.getProjectLanguage() in ["Python3"]
-                        if isProjectPy3:
-                            self.filetype = "Python3"
-                        return isProjectPy3
-                    else:
-                        # 3) determine by compiling the sources
-                        syntaxError = Utilities.compile(
-                            self.fileName, self.text())
-                        if not syntaxError:
-                            self.filetype = "Python3"
-                            return True
-                
-                if ext in self.dbs.getExtensions('Python3'):
-                    self.filetype = "Python3"
-                    return True
-        
-        return False
+        return self.__getPyVersion() == 3
 
     def isRubyFile(self):
         """
@@ -2131,7 +2071,7 @@
         @param temporary flag indicating a temporary breakpoint (boolean)
         """
         if self.fileName and \
-           (self.isPy3File() or self.isPy2File() or self.isRubyFile()):
+           (self.isPyFile() or self.isRubyFile()):
             self.breakpointModel.addBreakPoint(
                 self.fileName, line, ('', temporary, True, 0))
             self.breakpointToggled.emit(self)
@@ -3889,7 +3829,7 @@
         
         @param goUp flag indicating the move direction (boolean)
         """
-        if self.isPy3File() or self.isPy2File() or self.isRubyFile():
+        if self.isPyFile() or self.isRubyFile():
             lineNo = self.getCursorPosition()[0]
             line = self.text(lineNo)
             if line.strip().startswith(("class ", "def ", "module ")):
@@ -4729,13 +4669,12 @@
             self.menuActs["Cut"].setEnabled(self.hasSelectedText())
             self.menuActs["Copy"].setEnabled(self.hasSelectedText())
         if not self.isResourcesFile:
-            if self.fileName and \
-               (self.isPy3File() or self.isPy2File()):
+            if self.fileName and self.isPyFile():
                 self.menuActs["Show"].setEnabled(True)
             else:
                 self.menuActs["Show"].setEnabled(False)
             if self.fileName and \
-               (self.isPy3File() or self.isPy2File() or self.isRubyFile()):
+               (self.isPyFile() or self.isRubyFile()):
                 self.menuActs["Diagrams"].setEnabled(True)
             else:
                 self.menuActs["Diagrams"].setEnabled(False)
@@ -4871,8 +4810,7 @@
         Private slot handling the aboutToShow signal of the margins context
         menu.
         """
-        if self.fileName and \
-           (self.isPy3File() or self.isPy2File() or self.isRubyFile()):
+        if self.fileName and (self.isPyFile() or self.isRubyFile()):
             self.marginMenuActs["Breakpoint"].setEnabled(True)
             self.marginMenuActs["TempBreakpoint"].setEnabled(True)
             if self.markersAtLine(self.line) & self.breakpointMask:
--- a/UI/UserInterface.py	Fri Mar 28 19:52:34 2014 +0100
+++ b/UI/UserInterface.py	Sat Mar 29 20:12:47 2014 +0100
@@ -5387,8 +5387,7 @@
                     self.utEditorOpen = cap & HasUnittest
                     return
             
-            if self.viewmanager.getOpenEditor(fn).isPy3File() or \
-               self.viewmanager.getOpenEditor(fn).isPy2File():
+            if self.viewmanager.getOpenEditor(fn).isPyFile():
                 self.utScriptAct.setEnabled(True)
                 self.utEditorOpen = True
         
@@ -5414,7 +5413,7 @@
                     self.utEditorOpen = cap & HasUnittest
                     return
             
-            if editor.isPy3File() or editor.isPy2File():
+            if editor.isPyFile():
                 self.utScriptAct.setEnabled(True)
                 self.utEditorOpen = True
                 return
--- a/Utilities/__init__.py	Fri Mar 28 19:52:34 2014 +0100
+++ b/Utilities/__init__.py	Sat Mar 29 20:12:47 2014 +0100
@@ -1287,6 +1287,70 @@
     return sys.hexversion >> 16
     
 
+def determinePythonVersion(filename, source, editor=None):
+    """
+    Function to determine the python version of a given file.
+    
+    @param filename name of the file with extension (str)
+    @param source of the file (str)
+    @keyparam editor reference to the editor, if the file is opened
+        already (Editor object)
+    @return Python version if file is Python2 or Python3 (int)
+    """
+    pyAssignment = {"Python": 2, "Python2": 2, "Python3": 3}
+    
+    if not editor:
+        viewManager = e5App().getObject('ViewManager')
+        editor = viewManager.getOpenEditor(filename)
+    
+    # Maybe the user has changed the language
+    if editor and editor.getFileType() in pyAssignment:
+        return pyAssignment[editor.getFileType()]
+
+    pyVer = 0
+    flags = extractFlags(source)
+    ext = os.path.splitext(filename)[1]
+    py2Ext = Preferences.getPython("PythonExtensions")
+    py3Ext = Preferences.getPython("Python3Extensions")
+    project = e5App().getObject('Project')
+    basename = os.path.basename(filename)
+    
+    if "FileType" in flags:
+        pyVer = pyAssignment.get(flags["FileType"], 0)
+    elif project.isOpen() and project.isProjectFile(filename):
+        language = project.getEditorLexerAssoc(basename)
+        if not language:
+            language = Preferences.getEditorLexerAssoc(basename)
+        if language in ['Python2', 'Python3']:
+            pyVer = pyAssignment[language]
+    
+    if pyVer:
+        # Skip the next tests
+        pass
+    elif (Preferences.getProject("DeterminePyFromProject") and
+          project.isOpen() and
+          project.isProjectFile(filename)):
+                pyVer = pyAssignment.get(project.getProjectLanguage(), 0)
+    elif ext in py2Ext and ext not in py3Ext:
+        pyVer = 2
+    elif ext in py3Ext and ext not in py2Ext:
+        pyVer = 3
+    elif source.startswith("#!"):
+        line0 = source.splitlines()[0]
+        if "python3" in line0:
+            pyVer = 3
+        elif "python" in line0:
+            pyVer = 2
+    
+    if pyVer == 0 and ext in py2Ext + py3Ext:
+        pyVer = sys.version_info[0]
+    
+    if editor and pyVer:
+        editor.filetype = "Python{0}".format(pyVer)
+    
+    return pyVer
+
+
 def compile(file, codestring=""):
     """
     Function to compile one Python source file to Python bytecode.
--- a/ViewManager/ViewManager.py	Fri Mar 28 19:52:34 2014 +0100
+++ b/ViewManager/ViewManager.py	Sat Mar 29 20:12:47 2014 +0100
@@ -6323,7 +6323,7 @@
             else:
                 self.autoCompleteFromAPIsAct.setEnabled(False)
             
-            if editor.isPy3File() or editor.isPy2File() or editor.isRubyFile():
+            if editor.isPyFile() or editor.isRubyFile():
                 self.gotoPreviousDefAct.setEnabled(True)
                 self.gotoNextDefAct.setEnabled(True)
             else:

eric ide

mercurial