7 Module implementing the project support for flask-babel. |
7 Module implementing the project support for flask-babel. |
8 """ |
8 """ |
9 |
9 |
10 import os |
10 import os |
11 import re |
11 import re |
|
12 import contextlib |
12 |
13 |
13 from PyQt5.QtCore import pyqtSlot, QObject, QProcess |
14 from PyQt5.QtCore import pyqtSlot, QObject, QProcess |
14 from PyQt5.QtWidgets import QDialog, QMenu |
15 from PyQt5.QtWidgets import QDialog, QMenu |
15 |
16 |
16 from E5Gui import E5MessageBox |
17 from E5Gui import E5MessageBox |
35 @param project reference to the project object |
36 @param project reference to the project object |
36 @type Project |
37 @type Project |
37 @param parent parent |
38 @param parent parent |
38 @type QObject |
39 @type QObject |
39 """ |
40 """ |
40 super(PyBabelProject, self).__init__(parent) |
41 super().__init__(parent) |
41 |
42 |
42 self.__plugin = plugin |
43 self.__plugin = plugin |
43 self.__project = project |
44 self.__project = project |
44 |
45 |
45 self.__e5project = e5App().getObject("Project") |
46 self.__e5project = e5App().getObject("Project") |
198 |
199 |
199 def determineCapability(self): |
200 def determineCapability(self): |
200 """ |
201 """ |
201 Public method to determine the availability of flask-babel. |
202 Public method to determine the availability of flask-babel. |
202 """ |
203 """ |
203 if self.__project.getData("flask", "flask_babel_override"): |
204 available = ( |
204 available = self.__project.getData("flask", |
205 self.__project.getData("flask", "flask_babel_available") |
205 "flask_babel_available") |
206 if self.__project.getData("flask", "flask_babel_override") else |
206 else: |
207 self.__flaskBabelAvailable() |
207 available = self.__flaskBabelAvailable() |
208 ) |
208 self.__project.setCapability("flask-babel", available) |
209 self.__project.setCapability("flask-babel", available) |
209 |
210 |
210 self.pybabelConfigAct.setEnabled(available) |
211 self.pybabelConfigAct.setEnabled(available) |
211 self.pybabelInstallAct.setEnabled(not available) |
212 self.pybabelInstallAct.setEnabled(not available) |
212 |
213 |
302 @type str |
303 @type str |
303 @return flag indicating successful configuration file creation |
304 @return flag indicating successful configuration file creation |
304 @rtype bool |
305 @rtype bool |
305 """ |
306 """ |
306 _, app = self.__project.getApplication() |
307 _, app = self.__project.getApplication() |
307 if app.endswith(".py"): |
308 template = ( |
308 template = ( |
309 ("[python: {0}]\n" |
309 "[python: {0}]\n" |
310 "[jinja2: templates/**.html]\n" |
310 "[jinja2: templates/**.html]\n" |
311 "extensions=jinja2.ext.autoescape,jinja2.ext.with_\n") |
311 "extensions=jinja2.ext.autoescape,jinja2.ext.with_\n" |
312 if app.endswith(".py") else |
312 ) |
313 ("[python: {0}/**.py]\n" |
313 else: |
314 "[jinja2: {0}/templates/**.html]\n" |
314 template = ( |
315 "extensions=jinja2.ext.autoescape,jinja2.ext.with_\n") |
315 "[python: {0}/**.py]\n" |
316 ) |
316 "[jinja2: {0}/templates/**.html]\n" |
|
317 "extensions=jinja2.ext.autoescape,jinja2.ext.with_\n" |
|
318 ) |
|
319 try: |
317 try: |
320 with open(configFile, "w") as f: |
318 with open(configFile, "w") as f: |
321 f.write(template.format(app)) |
319 f.write(template.format(app)) |
322 self.__e5project.appendFile(configFile) |
320 self.__e5project.appendFile(configFile) |
323 E5MessageBox.information( |
321 E5MessageBox.information( |
362 def __checkAvailability(self): |
360 def __checkAvailability(self): |
363 """ |
361 """ |
364 Private slot to check the availability of the 'flask-babel' extension. |
362 Private slot to check the availability of the 'flask-babel' extension. |
365 """ |
363 """ |
366 self.determineCapability() |
364 self.determineCapability() |
367 if self.__project.hasCapability("flask-babel"): |
365 msg = ( |
368 msg = self.tr("The 'flask-babel' extension is installed.") |
366 self.tr("The 'flask-babel' extension is installed.") |
369 else: |
367 if self.__project.hasCapability("flask-babel") else |
370 msg = self.tr("The 'flask-babel' extension is not installed.") |
368 self.tr("The 'flask-babel' extension is not installed.") |
|
369 ) |
371 E5MessageBox.information( |
370 E5MessageBox.information( |
372 None, |
371 None, |
373 self.tr("flask-babel Availability"), |
372 self.tr("flask-babel Availability"), |
374 msg) |
373 msg) |
375 |
374 |
425 if self.__ensurePybabelConfigured(): |
424 if self.__ensurePybabelConfigured(): |
426 workdir = self.__project.getApplication()[0] |
425 workdir = self.__project.getApplication()[0] |
427 potFile = self.__e5project.getAbsoluteUniversalPath( |
426 potFile = self.__e5project.getAbsoluteUniversalPath( |
428 self.__project.getData("flask-babel", "catalogFile")) |
427 self.__project.getData("flask-babel", "catalogFile")) |
429 |
428 |
430 try: |
429 with contextlib.suppress(OSError): |
431 potFilePath = os.path.dirname(potFile) |
430 potFilePath = os.path.dirname(potFile) |
432 os.makedirs(potFilePath) |
431 os.makedirs(potFilePath) |
433 except OSError: |
|
434 pass |
|
435 |
432 |
436 args = [ |
433 args = [ |
437 "-F", |
434 "-F", |
438 os.path.relpath( |
435 os.path.relpath( |
439 self.__e5project.getAbsoluteUniversalPath( |
436 self.__e5project.getAbsoluteUniversalPath( |
440 self.__project.getData("flask-babel", "configFile")), |
437 self.__project.getData("flask-babel", "configFile")), |
441 workdir |
438 workdir |
442 ) |
439 ) |
443 ] |
440 ] |
444 if self.__project.getData("flask-babel", "markersList"): |
441 if self.__project.getData("flask-babel", "markersList"): |
445 for marker in self.__project.getData("flask-babel", "markersList"): |
442 for marker in self.__project.getData("flask-babel", |
|
443 "markersList"): |
446 args += ["-k", marker] |
444 args += ["-k", marker] |
447 args += [ |
445 args += [ |
448 "-o", |
446 "-o", |
449 os.path.relpath(potFile, workdir), |
447 os.path.relpath(potFile, workdir), |
450 "." |
448 "." |