VirtualenvManager, DebuggerPropertiesDialog, DebuggerInterfacePython: added code to use the default virtual environment, if none has been configured yet.

Sun, 16 Sep 2018 13:36:01 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 16 Sep 2018 13:36:01 +0200
changeset 6503
e617c58807e8
parent 6502
07070e95be4d
child 6504
154eda18e9b1

VirtualenvManager, DebuggerPropertiesDialog, DebuggerInterfacePython: added code to use the default virtual environment, if none has been configured yet.

APIs/Python3/eric6.api file | annotate | diff | comparison | revisions
Debugger/DebugServer.py file | annotate | diff | comparison | revisions
Debugger/DebuggerInterfacePython.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/eric6.VirtualEnv.VirtualenvManager.html file | annotate | diff | comparison | revisions
Project/DebuggerPropertiesDialog.py file | annotate | diff | comparison | revisions
VirtualEnv/VirtualenvManager.py file | annotate | diff | comparison | revisions
changelog file | annotate | diff | comparison | revisions
--- a/APIs/Python3/eric6.api	Sun Sep 16 13:32:57 2018 +0200
+++ b/APIs/Python3/eric6.api	Sun Sep 16 13:36:01 2018 +0200
@@ -10643,9 +10643,11 @@
 eric6.VirtualEnv.VirtualenvInterpreterSelectionDialog.VirtualenvInterpreterSelectionDialog.getData?4()
 eric6.VirtualEnv.VirtualenvInterpreterSelectionDialog.VirtualenvInterpreterSelectionDialog.on_pythonExecPicker_textChanged?4(txt)
 eric6.VirtualEnv.VirtualenvInterpreterSelectionDialog.VirtualenvInterpreterSelectionDialog?1(venvName, venvDirectory, parent=None)
+eric6.VirtualEnv.VirtualenvManager.VirtualenvManager.DefaultKey?7
 eric6.VirtualEnv.VirtualenvManager.VirtualenvManager.addVirtualEnv?4(venvName, venvDirectory, venvInterpreter="", venvVariant=3, isGlobal=False)
 eric6.VirtualEnv.VirtualenvManager.VirtualenvManager.createVirtualEnv?4()
 eric6.VirtualEnv.VirtualenvManager.VirtualenvManager.deleteVirtualEnvs?4(venvNames)
+eric6.VirtualEnv.VirtualenvManager.VirtualenvManager.getDefaultEnvironment?4()
 eric6.VirtualEnv.VirtualenvManager.VirtualenvManager.getEnvironmentEntries?4()
 eric6.VirtualEnv.VirtualenvManager.VirtualenvManager.getVirtualenvDirectory?4(venvName)
 eric6.VirtualEnv.VirtualenvManager.VirtualenvManager.getVirtualenvInterpreter?4(venvName)
--- a/Debugger/DebugServer.py	Sun Sep 16 13:32:57 2018 +0200
+++ b/Debugger/DebugServer.py	Sun Sep 16 13:36:01 2018 +0200
@@ -14,6 +14,7 @@
     pass
 
 import os
+import sys
 
 from PyQt5.QtCore import pyqtSignal, QModelIndex
 from PyQt5.QtNetwork import QTcpServer, QHostAddress, QHostInfo, \
@@ -210,7 +211,6 @@
         self.clientType = \
             Preferences.Prefs.settings.value('DebugClient/Type')
         if self.clientType is None:
-            import sys
             if sys.version_info[0] == 2:
                 self.clientType = 'Python2'
             else:
@@ -218,10 +218,10 @@
         # Change clientType if dependent interpreter does not exist anymore
         # (maybe deinstalled,...)
         elif self.clientType == 'Python2' and Preferences.getDebugger(
-                "Python2VirtualEnv") == '':
+                "Python2VirtualEnv") == '' and sys.version_info[0] == 3:
             self.clientType = 'Python3'
         elif self.clientType == 'Python3' and Preferences.getDebugger(
-                "Python3VirtualEnv") == '':
+                "Python3VirtualEnv") == '' and sys.version_info[0] == 2:
             self.clientType = 'Python2'
         
         self.lastClientType = ''
--- a/Debugger/DebuggerInterfacePython.py	Sun Sep 16 13:32:57 2018 +0200
+++ b/Debugger/DebuggerInterfacePython.py	Sun Sep 16 13:36:01 2018 +0200
@@ -1202,13 +1202,15 @@
             py3Exts.append(".{0}".format(ext))
     
     registryData = []
-    if py2Exts and Preferences.getDebugger("Python2VirtualEnv"):
+    if py2Exts and (Preferences.getDebugger("Python2VirtualEnv") or
+                    sys.version_info[0] == 2):
         registryData.append(
             ("Python2", ClientDefaultCapabilities, py2Exts,
              createDebuggerInterfacePython2)
         )
     
-    if py3Exts and Preferences.getDebugger("Python3VirtualEnv"):
+    if py3Exts and (Preferences.getDebugger("Python3VirtualEnv") or
+                    sys.version_info[0] == 3):
         registryData.append(
             ("Python3", ClientDefaultCapabilities, py3Exts,
                 createDebuggerInterfacePython3)
Binary file Documentation/Help/source.qch has changed
--- a/Documentation/Help/source.qhp	Sun Sep 16 13:32:57 2018 +0200
+++ b/Documentation/Help/source.qhp	Sun Sep 16 13:36:01 2018 +0200
@@ -17522,6 +17522,7 @@
       <keyword name="VirtualenvManager.addVirtualEnv" id="VirtualenvManager.addVirtualEnv" ref="eric6.VirtualEnv.VirtualenvManager.html#VirtualenvManager.addVirtualEnv" />
       <keyword name="VirtualenvManager.createVirtualEnv" id="VirtualenvManager.createVirtualEnv" ref="eric6.VirtualEnv.VirtualenvManager.html#VirtualenvManager.createVirtualEnv" />
       <keyword name="VirtualenvManager.deleteVirtualEnvs" id="VirtualenvManager.deleteVirtualEnvs" ref="eric6.VirtualEnv.VirtualenvManager.html#VirtualenvManager.deleteVirtualEnvs" />
+      <keyword name="VirtualenvManager.getDefaultEnvironment" id="VirtualenvManager.getDefaultEnvironment" ref="eric6.VirtualEnv.VirtualenvManager.html#VirtualenvManager.getDefaultEnvironment" />
       <keyword name="VirtualenvManager.getEnvironmentEntries" id="VirtualenvManager.getEnvironmentEntries" ref="eric6.VirtualEnv.VirtualenvManager.html#VirtualenvManager.getEnvironmentEntries" />
       <keyword name="VirtualenvManager.getVirtualenvDirectory" id="VirtualenvManager.getVirtualenvDirectory" ref="eric6.VirtualEnv.VirtualenvManager.html#VirtualenvManager.getVirtualenvDirectory" />
       <keyword name="VirtualenvManager.getVirtualenvInterpreter" id="VirtualenvManager.getVirtualenvInterpreter" ref="eric6.VirtualEnv.VirtualenvManager.html#VirtualenvManager.getVirtualenvInterpreter" />
--- a/Documentation/Source/eric6.VirtualEnv.VirtualenvManager.html	Sun Sep 16 13:32:57 2018 +0200
+++ b/Documentation/Source/eric6.VirtualEnv.VirtualenvManager.html	Sun Sep 16 13:36:01 2018 +0200
@@ -48,7 +48,7 @@
 QObject
 <h3>Class Attributes</h3>
 <table>
-<tr><td>None</td></tr>
+<tr><td>DefaultKey</td></tr>
 </table>
 <h3>Class Methods</h3>
 <table>
@@ -78,6 +78,9 @@
 <td><a href="#VirtualenvManager.deleteVirtualEnvs">deleteVirtualEnvs</a></td>
 <td>Public method to delete virtual environments from the list and disk.</td>
 </tr><tr>
+<td><a href="#VirtualenvManager.getDefaultEnvironment">getDefaultEnvironment</a></td>
+<td>Public method to get the default virtual environment.</td>
+</tr><tr>
 <td><a href="#VirtualenvManager.getEnvironmentEntries">getEnvironmentEntries</a></td>
 <td>Public method to get a dictionary containing the defined virtual environment entries.</td>
 </tr><tr>
@@ -197,6 +200,26 @@
 <dd>
 list of logical names for the virtual environments
 </dd>
+</dl><a NAME="VirtualenvManager.getDefaultEnvironment" ID="VirtualenvManager.getDefaultEnvironment"></a>
+<h4>VirtualenvManager.getDefaultEnvironment</h4>
+<b>getDefaultEnvironment</b>(<i></i>)
+<p>
+        Public method to get the default virtual environment.
+</p><p>
+        Default is an environment with the key '<default>' or the first one
+        having an interpreter matching sys.executable (i.e. the one used to
+        execute eric6 with)
+</p><dl>
+<dt>Returns:</dt>
+<dd>
+tuple containing the environment name and a dictionary
+            containing a copy of the default virtual environment
+</dd>
+</dl><dl>
+<dt>Return Type:</dt>
+<dd>
+tuple of (str, dict)
+</dd>
 </dl><a NAME="VirtualenvManager.getEnvironmentEntries" ID="VirtualenvManager.getEnvironmentEntries"></a>
 <h4>VirtualenvManager.getEnvironmentEntries</h4>
 <b>getEnvironmentEntries</b>(<i></i>)
--- a/Project/DebuggerPropertiesDialog.py	Sun Sep 16 13:32:57 2018 +0200
+++ b/Project/DebuggerPropertiesDialog.py	Sun Sep 16 13:36:01 2018 +0200
@@ -55,10 +55,10 @@
         self.debugClientClearHistoryButton.setIcon(
             UI.PixmapCache.getIcon("editDelete.png"))
         
+        venvManager = e5App().getObject("VirtualEnvManager")
+        
         self.venvComboBox.addItem("")
-        self.venvComboBox.addItems(
-            sorted(e5App().getObject("VirtualEnvManager")
-                   .getVirtualenvNames()))
+        self.venvComboBox.addItems(sorted(venvManager.getVirtualenvNames()))
         
         self.translationLocalCompleter = E5DirCompleter(
             self.translationLocalEdit)
@@ -75,6 +75,8 @@
                 venvName = Preferences.getDebugger("Python3VirtualEnv")
             else:
                 venvName = ""
+            if not venvName:
+                venvName, _ = venvManager.getDefaultEnvironment()
             if venvName:
                 venvIndex = max(0, self.venvComboBox.findText(venvName))
             else:
--- a/VirtualEnv/VirtualenvManager.py	Sun Sep 16 13:32:57 2018 +0200
+++ b/VirtualEnv/VirtualenvManager.py	Sun Sep 16 13:36:01 2018 +0200
@@ -27,6 +27,8 @@
     """
     Class implementing an object to manage Python virtual environments.
     """
+    DefaultKey = "<default>"
+    
     def __init__(self, parent=None):
         """
         Constructor
@@ -76,7 +78,7 @@
                 break
         if not found:
             # add an environment entry for the default interpreter
-            self.__virtualEnvironments["<default>"] = {
+            self.__virtualEnvironments[VirtualenvManager.DefaultKey] = {
                 "path": "",
                 "interpreter": defaultPy,
                 "variant": sys.version_info[0],
@@ -94,6 +96,37 @@
             json.dumps(self.__virtualEnvironments)
         )
     
+    def getDefaultEnvironment(self):
+        """
+        Public method to get the default virtual environment.
+        
+        Default is an environment with the key '<default>' or the first one
+        having an interpreter matching sys.executable (i.e. the one used to
+        execute eric6 with)
+        
+        @return tuple containing the environment name and a dictionary
+            containing a copy of the default virtual environment
+        @rtype tuple of (str, dict)
+        """
+        if VirtualenvManager.DefaultKey in self.__virtualEnvironments:
+            return (
+                VirtualenvManager.DefaultKey,
+                copy.copy(
+                    self.__virtualEnvironments[VirtualenvManager.DefaultKey])
+            )
+        
+        else:
+            defaultPy = sys.executable.replace("w.exe", ".exe")
+            for venvName in self.__virtualEnvironments:
+                if (defaultPy ==
+                        self.__virtualEnvironments[venvName]["interpreter"]):
+                    return (
+                        venvName,
+                        copy.copy(self.__virtualEnvironments[venvName])
+                    )
+        
+        return ("", {})
+    
     @pyqtSlot()
     def createVirtualEnv(self):
         """
--- a/changelog	Sun Sep 16 13:32:57 2018 +0200
+++ b/changelog	Sun Sep 16 13:36:01 2018 +0200
@@ -5,6 +5,9 @@
 - General
   -- changed the extension of the generated script files for Windows from
      '.bat' to '.cmd'
+- Debugger
+  -- added code to use the default virtual environment, if none has been
+     configured yet
 - Tasks
   -- added code to mark a line as not containing a task with '__NO-TASK__'
 

eric ide

mercurial