ProjectFlask/FlaskBabelExtension/PyBabelProjectExtension.py

changeset 26
5aac667c4f0f
parent 18
d76a0939be6a
child 27
b73e9af0d496
equal deleted inserted replaced
25:04803a0e2297 26:5aac667c4f0f
9 9
10 import os 10 import os
11 import re 11 import re
12 12
13 from PyQt5.QtCore import pyqtSlot, QObject, QProcess 13 from PyQt5.QtCore import pyqtSlot, QObject, QProcess
14 from PyQt5.QtWidgets import QDialog 14 from PyQt5.QtWidgets import QDialog, QMenu
15 15
16 from E5Gui import E5MessageBox 16 from E5Gui import E5MessageBox
17 from E5Gui.E5Application import e5App 17 from E5Gui.E5Application import e5App
18 from E5Gui.E5Action import E5Action
18 19
19 from .PyBabelCommandDialog import PyBabelCommandDialog 20 from .PyBabelCommandDialog import PyBabelCommandDialog
20 21
21 import Utilities 22 import Utilities
22 23
43 44
44 self.__e5project = e5App().getObject("Project") 45 self.__e5project = e5App().getObject("Project")
45 self.__virtualEnvManager = e5App().getObject("VirtualEnvManager") 46 self.__virtualEnvManager = e5App().getObject("VirtualEnvManager")
46 47
47 self.__hooksInstalled = False 48 self.__hooksInstalled = False
49
50 def initActions(self):
51 """
52 Public method to define the Flask actions.
53 """
54 self.actions = []
55
56 self.pybabelConfigAct = E5Action(
57 self.tr('Configure PyBabel'),
58 self.tr('Configure Py&Babel'),
59 0, 0,
60 self, 'flask_config_pybabel')
61 self.pybabelConfigAct.setStatusTip(self.tr(
62 'Shows a dialog to edit the configuration for pybabel'))
63 self.pybabelConfigAct.setWhatsThis(self.tr(
64 """<b>Configure PyBabel</b>"""
65 """<p>Shows a dialog to edit the configuration for pybabel.</p>"""
66 ))
67 self.pybabelConfigAct.triggered.connect(
68 self.__configurePyBabel)
69 self.actions.append(self.pybabelConfigAct)
70
71 # TODO: add action to install flask-babel
72
73 def initMenu(self):
74 """
75 Public method to initialize the Flask menu.
76
77 @return the menu generated
78 @rtype QMenu
79 """
80 menu = QMenu(self.tr("Translations"))
81 menu.setTearOffEnabled(True)
82
83 menu.addAction(self.pybabelConfigAct)
84
85 return menu
48 86
49 def registerOpenHook(self): 87 def registerOpenHook(self):
50 """ 88 """
51 Public method to register the open hook to open a translations file 89 Public method to register the open hook to open a translations file
52 in a translations editor. 90 in a translations editor.
127 165
128 def determineCapability(self): 166 def determineCapability(self):
129 """ 167 """
130 Public method to determine the availability of flask-babel. 168 Public method to determine the availability of flask-babel.
131 """ 169 """
132 self.__project.setCapability("pybabel", self.flaskBabelAvailable()) 170 available = self.flaskBabelAvailable()
133 171 self.__project.setCapability("pybabel", available)
172
173 self.pybabelConfigAct.setEnabled(available)
174 # TODO: disable install action, if flask-babel is available
175
134 ################################################################## 176 ##################################################################
135 ## slots and methods below implement general functionality 177 ## slots and methods below implement general functionality
136 ################################################################## 178 ##################################################################
137 179
138 def getBabelCommand(self): 180 def getBabelCommand(self):
169 return True 211 return True
170 212
171 return False 213 return False
172 214
173 @pyqtSlot() 215 @pyqtSlot()
174 def configurePyBabel(self): 216 def __configurePyBabel(self):
175 """ 217 """
176 Public slot to show a dialog to edit the pybabel configuration. 218 Private slot to show a dialog to edit the pybabel configuration.
177 """ 219 """
178 from .PyBabelConfigDialog import PyBabelConfigDialog 220 from .PyBabelConfigDialog import PyBabelConfigDialog
179 221
180 config = self.__project.getData("pybabel", "") 222 config = self.__project.getData("pybabel", "")
181 dlg = PyBabelConfigDialog(config) 223 dlg = PyBabelConfigDialog(config)
224 @param configFile name of the configuration file to be created 266 @param configFile name of the configuration file to be created
225 @type str 267 @type str
226 @return flag indicating successful configuration file creation 268 @return flag indicating successful configuration file creation
227 @rtype bool 269 @rtype bool
228 """ 270 """
229 _, app = self.getApplication() 271 _, app = self.__project.getApplication()
230 if app.endswith(".py"): 272 if app.endswith(".py"):
231 template = ( 273 template = (
232 "[python: {0}]\n" 274 "[python: {0}]\n"
233 "[jinja2: templates/**.html]\n" 275 "[jinja2: templates/**.html]\n"
234 "extensions=jinja2.ext.autoescape,jinja2.ext.with_\n" 276 "extensions=jinja2.ext.autoescape,jinja2.ext.with_\n"

eric ide

mercurial