Project/CreateDialogCodeDialog.py

changeset 6590
8cad6f50ff4c
parent 6548
21f8260753b5
child 6645
ad476851d7e0
diff -r 613426e62983 -r 8cad6f50ff4c Project/CreateDialogCodeDialog.py
--- a/Project/CreateDialogCodeDialog.py	Fri Nov 16 20:00:03 2018 +0100
+++ b/Project/CreateDialogCodeDialog.py	Sat Nov 17 12:45:58 2018 +0100
@@ -18,7 +18,7 @@
 import xml.etree.ElementTree
 
 from PyQt5.QtCore import QMetaObject, QByteArray, QRegExp, Qt, pyqtSlot, \
-    QMetaMethod, QSortFilterProxyModel, QProcess
+    QMetaMethod, QSortFilterProxyModel, QProcess, QProcessEnvironment
 from PyQt5.QtGui import QStandardItemModel, QBrush, QStandardItem
 from PyQt5.QtWidgets import QWidget, QDialog, QDialogButtonBox, QAction
 from PyQt5 import uic
@@ -146,7 +146,75 @@
         @return flag indicating an initialzation error (boolean)
         """
         return self.__initError
+    
+    def __runUicLoadUi(self, command):
+        """
+        Private method to run the UicLoadUi.py script with the given command
+        and return the output.
         
+        @param command uic command to be run
+        @type str
+        @return tuple of process output and error flag
+        @rtype tuple of (str, bool)
+        """
+        venvName = self.project.getDebugProperty("VIRTUALENV")
+        venvManager = e5App().getObject("VirtualEnvManager")
+        interpreter = venvManager.getVirtualenvInterpreter(venvName)
+        execPath = venvManager.getVirtualenvExecPath(venvName)
+        
+        env = QProcessEnvironment.systemEnvironment()
+        if execPath:
+            if env.contains("PATH"):
+                env.insert(
+                    "PATH", os.pathsep.join([execPath, env.value("PATH")])
+                )
+            else:
+                env.insert("PATH", execPath)
+        
+        loadUi = os.path.join(os.path.dirname(__file__), "UicLoadUi.py")
+        args = [
+            loadUi,
+            command,
+            self.formFile,
+            self.project.getProjectPath(),
+        ]
+        
+        uicText = ""
+        ok = False
+        
+        proc = QProcess()
+        proc.setWorkingDirectory(self.project.getProjectPath())
+        proc.setProcessEnvironment(env)
+        proc.start(interpreter, args)
+        started = proc.waitForStarted(5000)
+        finished = proc.waitForFinished(30000)
+        if started and finished:
+            output = proc.readAllStandardOutput()
+            outText = str(output, "utf-8", "replace")
+            if proc.exitCode() == 0:
+                ok = True
+                uicText = outText.strip()
+            else:
+                E5MessageBox.critical(
+                    self,
+                    self.tr("uic error"),
+                    self.tr(
+                        """<p>There was an error loading the form <b>{0}</b>"""
+                        """.</p><p>{1}</p>""").format(
+                        self.formFile, outText)
+                )
+        else:
+            E5MessageBox.critical(
+                self,
+                self.tr("uic error"),
+                self.tr(
+                    """<p>The project specific Python interpreter <b>{0}</b>"""
+                    """ could not be started or did not finish within 30"""
+                    """ seconds.</p>""").format(interpreter)
+            )
+        
+        return uicText, ok
+    
     def __objectNameExternal(self):
         """
         Private method to get the object name of a form via an external
@@ -155,33 +223,11 @@
         @return object name
         @rtype str
         """
-        interpreter = self.project.getDebugProperty("INTERPRETER")
         objectName = ""
         
-        loadUi = os.path.join(os.path.dirname(__file__), "UicLoadUi.py")
-        args = [
-            loadUi,
-            "object_name",
-            self.formFile,
-            self.project.getProjectPath(),
-        ]
-        
-        proc = QProcess()
-        proc.start(interpreter, args)
-        finished = proc.waitForFinished(30000)
-        if finished:
-            text = proc.readAllStandardOutput()
-            if proc.exitCode() == 0:
-                objectName = str(text, "utf-8", "replace").strip()
-            else:
-                errorText = str(text, "utf-8", "replace")
-                E5MessageBox.critical(
-                    self,
-                    self.tr("uic error"),
-                    self.tr(
-                        """<p>There was an error loading the form <b>{0}</b>"""
-                        """.</p><p>{1}</p>""").format(
-                        self.formFile, errorText))
+        output, ok = self.__runUicLoadUi("object_name")
+        if ok and output:
+            objectName = output
         
         return objectName
     
@@ -192,7 +238,7 @@
         @return object name
         @rtype str
         """
-        if self.project.getDebugProperty("INTERPRETER"):
+        if self.project.getDebugProperty("VIRTUALENV"):
             return self.__objectNameExternal()
         else:
             try:
@@ -217,33 +263,11 @@
         @return class name
         @rtype str
         """
-        interpreter = self.project.getDebugProperty("INTERPRETER")
         className = ""
         
-        loadUi = os.path.join(os.path.dirname(__file__), "UicLoadUi.py")
-        args = [
-            loadUi,
-            "class_name",
-            self.formFile,
-            self.project.getProjectPath(),
-        ]
-        
-        proc = QProcess()
-        proc.start(interpreter, args)
-        finished = proc.waitForFinished(30000)
-        if finished:
-            text = proc.readAllStandardOutput()
-            if proc.exitCode() == 0:
-                className = str(text, "utf-8", "replace").strip()
-            else:
-                errorText = str(text, "utf-8", "replace")
-                E5MessageBox.critical(
-                    self,
-                    self.tr("uic error"),
-                    self.tr(
-                        """<p>There was an error loading the form <b>{0}</b>"""
-                        """.</p><p>{1}</p>""").format(
-                        self.formFile, errorText))
+        output, ok = self.__runUicLoadUi("class_name")
+        if ok and output:
+            className = output
         
         return className
     
@@ -254,8 +278,8 @@
         @return class name
         @rtype str
         """
-        if self.project.getDebugProperty("INTERPRETER"):
-            return self.__objectNameExternal()
+        if self.project.getDebugProperty("VIRTUALENV"):
+            return self.__classNameExternal()
         else:
             try:
                 dlg = uic.loadUi(
@@ -327,36 +351,9 @@
         Private slot to update the slots tree display getting the data via an
         external interpreter.
         """
-        interpreter = self.project.getDebugProperty("INTERPRETER")
-        objectsList = []
-        
-        loadUi = os.path.join(os.path.dirname(__file__), "UicLoadUi.py")
-        args = [
-            loadUi,
-            "signatures",
-            self.formFile,
-            self.project.getProjectPath(),
-        ]
-        
-        proc = QProcess()
-        proc.start(interpreter, args)
-        finished = proc.waitForFinished(30000)
-        if not finished:
-            return
-        
-        text = proc.readAllStandardOutput()
-        if proc.exitCode() != 0:
-            errorText = str(text, "utf-8", "replace")
-            E5MessageBox.critical(
-                self,
-                self.tr("uic error"),
-                self.tr(
-                    """<p>There was an error loading the form <b>{0}</b>"""
-                    """.</p><p>{1}</p>""").format(
-                    self.formFile, errorText))
-        else:
-            objectsListStr = str(text, "utf-8", "replace").strip()
-            objectsList = json.loads(objectsListStr)
+        output, ok = self.__runUicLoadUi("signatures")
+        if ok and output:
+            objectsList = json.loads(output.strip())
             
             signatureList = self.__signatures()
             
@@ -406,7 +403,7 @@
         """
         self.filterEdit.clear()
         
-        if self.project.getDebugProperty("INTERPRETER"):
+        if self.project.getDebugProperty("VIRTUALENV"):
             self.__updateSlotsModelExternal()
         else:
             try:

eric ide

mercurial