Continued implementing pybabel translations support.

Thu, 19 Nov 2020 20:19:55 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 19 Nov 2020 20:19:55 +0100
changeset 15
3f5c05eb2d5f
parent 14
d2da14b2a233
child 16
dd3f6bfb85f7

Continued implementing pybabel translations support.

ProjectFlask/FlaskCommandDialog.py file | annotate | diff | comparison | revisions
ProjectFlask/Project.py file | annotate | diff | comparison | revisions
diff -r d2da14b2a233 -r 3f5c05eb2d5f ProjectFlask/FlaskCommandDialog.py
--- a/ProjectFlask/FlaskCommandDialog.py	Thu Nov 19 18:34:05 2020 +0100
+++ b/ProjectFlask/FlaskCommandDialog.py	Thu Nov 19 20:19:55 2020 +0100
@@ -35,11 +35,14 @@
         
         self.__process = None
         
+        self.successMessage = ""
+        self.errorMessage = ""
+        
         self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True)
         self.buttonBox.button(QDialogButtonBox.Close).setDefault(True)
         self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False)
     
-    def startCommand(self, command, args=None):
+    def startFlaskCommand(self, command, args=None):
         """
         Public method to start a flask command and show its output.
         
@@ -86,6 +89,61 @@
         
         return ok
     
+    def startBabelCommand(self, command, args, title, msgSuccess="",
+                          msgError=""):
+        """
+        Public method to start a pybabel command and show its output.
+        
+        @param command pybabel command to be run
+        @type str
+        @param args list of command line arguments for the command
+        @type list of str
+        @param title window title of the dialog
+        @type str
+        @param msgSuccess success message to be shown
+        @type str
+        @param msgError message to be shown on error
+        @type str
+        @return flag indicating a successful start
+        @rtype bool
+        """
+        self.setWindowTitle(title)
+        
+        self.successMessage = msgSuccess
+        self.errorMessage = msgError
+        
+        workdir, _ = self.__project.getApplication()
+        babelCommand = self.__project.getBabelCommand()
+        
+        self.__process = QProcess()
+        self.__process.setWorkingDirectory(workdir)
+        self.__process.setProcessChannelMode(QProcess.MergedChannels)
+        
+        self.__process.readyReadStandardOutput.connect(self.__readStdOut)
+        self.__process.finished.connect(self.__processFinished)
+        
+        self.outputEdit.clear()
+        
+        babelArgs = [command]
+        if args:
+            babelArgs += args
+        
+        self.__process.start(babelCommand, babelArgs)
+        ok = self.__process.waitForStarted(10000)
+        if not ok:
+            E5MessageBox.critical(
+                None,
+                self.tr("Execute PyBabel Command"),
+                self.tr("""The pybabel process could not be started."""))
+        else:
+            self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False)
+            self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True)
+            self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(True)
+            self.buttonBox.button(QDialogButtonBox.Cancel).setFocus(
+                Qt.OtherFocusReason)
+        
+        return ok
+    
     def closeEvent(self, evt):
         """
         Protected method handling the close event of the dialog.
@@ -105,11 +163,16 @@
             out = str(self.__process.readAllStandardOutput(), "utf-8")
             self.outputEdit.insertPlainText(out)
     
-    @pyqtSlot()
-    def __processFinished(self):
+    def __processFinished(self, exitCode, exitStatus):
         """
-        Private slot handling the finishing of the server process.
+        Private slot connected to the finished signal.
+        
+        @param exitCode exit code of the process
+        @type int
+        @param exitStatus exit status of the process
+        @type QProcess.ExitStatus
         """
+        normal = (exitStatus == QProcess.NormalExit) and (exitCode == 0)
         self.__cancelProcess()
         
         self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False)
@@ -117,6 +180,11 @@
         self.buttonBox.button(QDialogButtonBox.Close).setDefault(True)
         self.buttonBox.button(QDialogButtonBox.Close).setFocus(
             Qt.OtherFocusReason)
+        
+        if normal and self.successMessage:
+            self.outputEdit.insertPlainText(self.successMessage)
+        elif not normal and self.errorMessage:
+            self.outputEdit.insertPlainText(self.errorMessage)
     
     @pyqtSlot()
     def __cancelProcess(self):
diff -r d2da14b2a233 -r 3f5c05eb2d5f ProjectFlask/Project.py
--- a/ProjectFlask/Project.py	Thu Nov 19 18:34:05 2020 +0100
+++ b/ProjectFlask/Project.py	Thu Nov 19 20:19:55 2020 +0100
@@ -23,6 +23,8 @@
 import UI.PixmapCache
 import Utilities
 
+from .FlaskCommandDialog import FlaskCommandDialog
+
 
 class Project(QObject):
     """
@@ -762,10 +764,8 @@
         """
         Private slot showing the result of the database creation.
         """
-        from .FlaskCommandDialog import FlaskCommandDialog
-        
         dlg = FlaskCommandDialog(self)
-        if dlg.startCommand("init-db"):
+        if dlg.startFlaskCommand("init-db"):
             dlg.exec()
     
     ##################################################################
@@ -882,11 +882,62 @@
         # TODO: implement this with pybabel ...
         pass
     
-    def openPOEditor(self):
-        # TODO: implement this with pybabel ...
-        pass
+    def openPOEditor(self, poFile):
+        """
+        Public method to edit the given file in an external .po editor.
+        
+        @param poFile name of the .po file
+        @type str
+        """
+        editor = self.__plugin.getPreferences("TranslationsEditor")
+        if poFile.endswith(".po") and editor:
+            wd, _ = self.getApplication()
+            started, pid = QProcess.startDetached(editor, [poFile], wd)
+            if not started:
+                E5MessageBox.critical(
+                    None,
+                    self.tr('Process Generation Error'),
+                    self.tr('The translations editor process ({0}) could'
+                            ' not be started.').format(
+                        os.path.basename(editor)))
     
     def extractMessages(self):
+        """
+        Public method to extract the messages catalog template file.
+        """
+        title = self.tr("Extract messages")
+        if self.__ensurePybabelConfigured():
+            potFile = self.__e5project.getAbsoluteUniversalPath(
+                self.getData("pybabel", "catalogFile"))
+            
+            try:
+                potFilePath = os.path.dirname(potFile)
+                os.makedirs(potFilePath)
+            except EnvironmentError:
+                pass
+            
+            args = [
+                "-F",
+                self.__e5project.getAbsoluteUniversalPath(
+                    self.getData("pybabel", "configFile"))
+            ]
+            if self.getData("pybabel", "markersList"):
+                for marker in self.getData("pybabel", "markersList"):
+                    args += ["-k", marker]
+            args += [
+                "-o",
+                potFile,
+                "."
+            ]
+            
+            dlg = FlaskCommandDialog(self)
+            res = dlg.startBabelCommand(
+                "extract", args, title,
+                msgSuccess=self.tr("\nMessages extracted successfully.")
+            )
+            if res:
+                dlg.exec()
+                self.__e5project.appendFile(potFile)
         # TODO: implement this with pybabel ...
         pass
     

eric ide

mercurial