CondaInterface/__init__.py

branch
conda
changeset 6697
2f5c951bdf14
parent 6681
9c1513b488ef
child 6724
ca89c7d94c94
--- a/CondaInterface/__init__.py	Sun Feb 03 16:31:53 2019 +0100
+++ b/CondaInterface/__init__.py	Sun Feb 03 16:59:36 2019 +0100
@@ -13,7 +13,7 @@
 except NameError:
     pass
 
-import re
+import json
 
 from PyQt5.QtCore import QCoreApplication, QProcess
 
@@ -21,23 +21,24 @@
 
 __CondaVersion = tuple()
 __CondaVersionStr = ""
+__CondaRootPrefix = ""
+
+__initialized = False
 
 
-def __determineCondaVersion():
+def __initializeCondaInterface():
     """
-    Private module function to get the conda version via the conda executable.
+    Private module function to (re-)initialize the conda interface.
     """
-    global __CondaVersionStr, __CondaVersion
+    global __CondaVersionStr, __CondaVersion, __CondaRootPrefix, __initialized
     
-    if not __CondaVersion:
+    if not __initialized:
         exe = Preferences.getConda("CondaExecutable")
         if not exe:
             exe = "conda"
         
-        versionRe = re.compile(r"""^conda.*?(\d+\.\d+\.\d+).*""")
-        
         proc = QProcess()
-        proc.start(exe, ["--version"])
+        proc.start(exe, ["info", "--json"])
         if not proc.waitForStarted(15000):
             __CondaVersionStr = QCoreApplication.translate(
                 "CondaInterface",
@@ -47,16 +48,21 @@
             output = str(proc.readAllStandardOutput(),
                          Preferences.getSystem("IOEncoding"),
                          'replace').strip()
-            match = re.match(versionRe, output)
-            if match:
-                __CondaVersionStr = match.group(1)
-                __CondaVersion = tuple(
-                    int(i) for i in __CondaVersionStr.split(".")
-                )
-            else:
+            try:
+                jsonDict = json.loads(output)
+            except Exception:
                 __CondaVersionStr = QCoreApplication.translate(
                     "CondaInterface",
-                    '<conda returned strange version info.')
+                    '<conda returned invalid data.')
+                return
+            
+            __CondaVersionStr = jsonDict["conda_version"]
+            __CondaVersion = tuple(
+                int(i) for i in __CondaVersionStr.split(".")
+            )
+            __CondaRootPrefix = jsonDict["root_prefix"]
+            
+            __initialized = True
 
 
 def condaVersion():
@@ -66,7 +72,7 @@
     @return tuple containing the conda version
     @rtype tuple of (int, int, int)
     """
-    __determineCondaVersion()
+    __initializeCondaInterface()
     return __CondaVersion
 
 
@@ -77,5 +83,25 @@
     @return conda version as a string
     @rtype str
     """
-    __determineCondaVersion()
+    __initializeCondaInterface()
     return __CondaVersionStr
+
+
+def rootPrefix():
+    """
+    Module function to get the root prefix.
+    
+    @return root prefix
+    @rtype str
+    """
+    __initializeCondaInterface()
+    return __CondaRootPrefix
+
+
+def resetInterface():
+    """
+    Module function to reset the conda interface.
+    """
+    global __initialized
+    
+    __initialized = False

eric ide

mercurial