Utilities/__init__.py

branch
BgService
changeset 3442
927186c0d409
parent 3209
c5432abceb25
child 3456
96232974dcdb
--- 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
 
 

eric ide

mercurial