Updated behavior of the Python version detection. BgService

Thu, 27 Mar 2014 21:27:08 +0100

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Thu, 27 Mar 2014 21:27:08 +0100
branch
BgService
changeset 3442
927186c0d409
parent 3422
ecfe8271cc73
child 3443
7d919fd299f6

Updated behavior of the Python version detection.

Debugger/DebugUI.py file | annotate | diff | comparison | revisions
Plugins/PluginCodeStyleChecker.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/Debugger/DebugUI.py	Sat Mar 22 18:51:14 2014 +0100
+++ b/Debugger/DebugUI.py	Thu Mar 27 21:27:08 2014 +0100
@@ -871,7 +871,7 @@
         if editor is None:
             return
         
-        if editor.isPy3File() or editor.isPy2File() or editor.isRubyFile():
+        if editor.getPyVersion() or editor.isRubyFile():
             if editor.curLineHasBreakpoint():
                 self.dbgEditBpAct.setEnabled(True)
             else:
--- a/Plugins/PluginCodeStyleChecker.py	Sat Mar 22 18:51:14 2014 +0100
+++ b/Plugins/PluginCodeStyleChecker.py	Thu Mar 27 21:27:08 2014 +0100
@@ -337,8 +337,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.getPyVersion())
     
     def __editorCodeStyleCheck(self):
         """
--- a/Plugins/PluginTabnanny.py	Sat Mar 22 18:51:14 2014 +0100
+++ b/Plugins/PluginTabnanny.py	Thu Mar 27 21:27:08 2014 +0100
@@ -288,8 +288,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.getPyVersion())
     
     def __editorTabnanny(self):
         """
--- a/QScintilla/Editor.py	Sat Mar 22 18:51:14 2014 +0100
+++ b/QScintilla/Editor.py	Thu Mar 27 21:27:08 2014 +0100
@@ -333,9 +333,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()
                 self.isResourcesFile = self.fileName.endswith(".qrc")
                 
@@ -1452,28 +1451,24 @@
             self.SCN_STYLENEEDED.disconnect(self.__styleNeeded)
         
         language = ""
+        basename = os.path.basename(filename)
         if self.project.isOpen() and self.project.isProjectFile(filename):
-            language = self.project.getEditorLexerAssoc(
-                os.path.basename(filename))
+            language = self.project.getEditorLexerAssoc(basename)
+        if not language:
+            language = Preferences.getEditorLexerAssoc(basename)
         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"
-            else:
-                filename = os.path.basename(filename)
-                language = Preferences.getEditorLexerAssoc(filename)
-                if language == "Python":
-                    # correction for Python
-                    if self.isPy2File():
-                        language = "Python2"
-                    else:
-                        language = "Python3"
+            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:
+            self.filetype = ""
+        
         if language.startswith("Pygments|"):
             pyname = language.split("|", 1)[1]
             language = ""
@@ -1581,10 +1576,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"
         
@@ -1713,12 +1707,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:
@@ -1733,51 +1724,24 @@
         @return current encoding (string)
         """
         return self.encoding
-        
+    
+    def getPyVersion(self):
+        """
+        Public methode 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 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.compile(
-#                            self.fileName, self.text(), True)[0]
-#                        if not syntaxError:
-#                            self.filetype = "Python2"
-#                            return True
-                
-                if ext in self.dbs.getExtensions('Python2'):
-                    self.filetype = "Python2"
-                    return True
-        
-        return False
+        return self.getPyVersion() == 2
 
     def isPy3File(self):
         """
@@ -1785,42 +1749,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(), False)[0]
-#                        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):
         """
@@ -2073,7 +2002,7 @@
         @param temporary flag indicating a temporary breakpoint (boolean)
         """
         if self.fileName and \
-           (self.isPy3File() or self.isPy2File() or self.isRubyFile()):
+           (self.getPyVersion() or self.isRubyFile()):
             self.breakpointModel.addBreakPoint(
                 self.fileName, line, ('', temporary, True, 0))
             self.breakpointToggled.emit(self)
@@ -3757,7 +3686,7 @@
         
         @param goUp flag indicating the move direction (boolean)
         """
-        if self.isPy3File() or self.isPy2File() or self.isRubyFile():
+        if self.getPyVersion() or self.isRubyFile():
             lineNo = self.getCursorPosition()[0]
             line = self.text(lineNo)
             if line.strip().startswith(("class ", "def ", "module ")):
@@ -4591,13 +4520,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.getPyVersion():
                 self.menuActs["Show"].setEnabled(True)
             else:
                 self.menuActs["Show"].setEnabled(False)
             if self.fileName and \
-               (self.isPy3File() or self.isPy2File() or self.isRubyFile()):
+               (self.getPyVersion() or self.isRubyFile()):
                 self.menuActs["Diagrams"].setEnabled(True)
             else:
                 self.menuActs["Diagrams"].setEnabled(False)
@@ -4731,8 +4659,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.getPyVersion() or self.isRubyFile()):
             self.marginMenuActs["Breakpoint"].setEnabled(True)
             self.marginMenuActs["TempBreakpoint"].setEnabled(True)
             if self.markersAtLine(self.line) & self.breakpointMask:
--- a/UI/UserInterface.py	Sat Mar 22 18:51:14 2014 +0100
+++ b/UI/UserInterface.py	Thu Mar 27 21:27:08 2014 +0100
@@ -5377,8 +5377,7 @@
                     self.utEditorOpen = cap & HasUnittest
                     return
             
-            if self.viewmanager.getOpenEditor(fn).isPy3File() or \
-               self.viewmanager.getOpenEditor(fn).isPy2File():
+            if self.viewmanager.getOpenEditor(fn).getPyVersion():
                 self.utScriptAct.setEnabled(True)
                 self.utEditorOpen = True
         
@@ -5404,7 +5403,7 @@
                     self.utEditorOpen = cap & HasUnittest
                     return
             
-            if editor.isPy3File() or editor.isPy2File():
+            if editor.getPyVersion():
                 self.utScriptAct.setEnabled(True)
                 self.utEditorOpen = True
                 return
--- a/Utilities/__init__.py	Sat Mar 22 18:51:14 2014 +0100
+++ b/Utilities/__init__.py	Thu Mar 27 21:27:08 2014 +0100
@@ -1303,21 +1303,38 @@
     @param filename name of the file with extension (str)
     @param source of the file (str)
     @keyparam editor if the file is opened already (Editor object)
-    @return flag if file is Python2 or Python3 (int)
+    @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')
-
-    pyVer = 0
-    if editor and editor.getLanguage() in pyAssignment:
-        pyVer = pyAssignment.get(editor.getLanguage())
-    elif "FileType" in flags:
+    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)):
@@ -1335,7 +1352,9 @@
     
     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
 
 
--- a/ViewManager/ViewManager.py	Sat Mar 22 18:51:14 2014 +0100
+++ b/ViewManager/ViewManager.py	Thu Mar 27 21:27:08 2014 +0100
@@ -6263,7 +6263,7 @@
             else:
                 self.autoCompleteFromAPIsAct.setEnabled(False)
             
-            if editor.isPy3File() or editor.isPy2File() or editor.isRubyFile():
+            if editor.getPyVersion() or editor.isRubyFile():
                 self.gotoPreviousDefAct.setEnabled(True)
                 self.gotoNextDefAct.setEnabled(True)
             else:

eric ide

mercurial