PluginProjectDjango.py

changeset 29
76d04dd67367
parent 26
2dd206cd1aa2
child 31
a632d10fdb7c
equal deleted inserted replaced
28:df55ca77e25f 29:76d04dd67367
26 # Start-of-Header 26 # Start-of-Header
27 name = "Django Project Plugin" 27 name = "Django Project Plugin"
28 author = "Detlev Offenbach <detlev@die-offenbachs.de>" 28 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
29 autoactivate = True 29 autoactivate = True
30 deactivateable = True 30 deactivateable = True
31 version = "3.2.0" 31 version = "3.2.1"
32 className = "ProjectDjangoPlugin" 32 className = "ProjectDjangoPlugin"
33 packageName = "ProjectDjango" 33 packageName = "ProjectDjango"
34 shortDescription = "Project support for Django projects." 34 shortDescription = "Project support for Django projects."
35 longDescription = """This plugin implements project support for Django projects.""" 35 longDescription = \
36 """This plugin implements project support for Django projects."""
36 needsRestart = False 37 needsRestart = False
37 pyqtApi = 2 38 pyqtApi = 2
38 # End-of-Header 39 # End-of-Header
39 40
40 error = "" 41 error = ""
44 45
45 def apiFiles(language): 46 def apiFiles(language):
46 """ 47 """
47 Module function to return the API files made available by this plugin. 48 Module function to return the API files made available by this plugin.
48 49
50 @param language language to get APIs for (string)
49 @return list of API filenames (list of string) 51 @return list of API filenames (list of string)
50 """ 52 """
51 if language in ["Python3", "Python2"]: 53 if language in ["Python3", "Python2"]:
52 apisDir = \ 54 apisDir = \
53 os.path.join(os.path.dirname(__file__), "ProjectDjango", "APIs") 55 os.path.join(os.path.dirname(__file__), "ProjectDjango", "APIs")
59 61
60 def createDjangoPage(configDlg): 62 def createDjangoPage(configDlg):
61 """ 63 """
62 Module function to create the Django configuration page. 64 Module function to create the Django configuration page.
63 65
66 @param configDlg reference to the configuration dialog
64 @return reference to the configuration page 67 @return reference to the configuration page
65 """ 68 """
66 global djangoPluginObject 69 global djangoPluginObject
67 from ProjectDjango.ConfigurationPage.DjangoPage import DjangoPage 70 from ProjectDjango.ConfigurationPage.DjangoPage import DjangoPage
68 page = DjangoPage(djangoPluginObject) 71 page = DjangoPage(djangoPluginObject)
75 78
76 @return dictionary containing the relevant data 79 @return dictionary containing the relevant data
77 """ 80 """
78 if e5App().getObject("UserInterface").versionIsNewer('5.0.99', '20120101'): 81 if e5App().getObject("UserInterface").versionIsNewer('5.0.99', '20120101'):
79 return { 82 return {
80 "djangoPage": \ 83 "djangoPage": [
81 [QCoreApplication.translate("ProjectDjangoPlugin", "Django"), 84 QCoreApplication.translate("ProjectDjangoPlugin", "Django"),
82 os.path.join("ProjectDjango", "icons", "django.png"), 85 os.path.join("ProjectDjango", "icons", "django.png"),
83 createDjangoPage, None, None], 86 createDjangoPage, None, None],
84 } 87 }
85 else: 88 else:
86 return {} 89 return {}
87 90
88 91
165 global error 168 global error
166 169
167 if self.__ui.versionIsNewer('5.0.99', '20120101'): 170 if self.__ui.versionIsNewer('5.0.99', '20120101'):
168 error = "" 171 error = ""
169 else: 172 else:
170 error = self.trUtf8("eric5 version is too old, {0}, {1} or newer needed.")\ 173 error = self.trUtf8("eric5 version is too old, {0}, {1} or newer"
171 .format("5.1.0", "20120101") 174 " needed.").format("5.1.0", "20120101")
172 return False 175 return False
173 176
174 return True 177 return True
175 178
176 def activate(self): 179 def activate(self):
193 196
194 self.__supportedVariants = self.__object.supportedPythonVariants() 197 self.__supportedVariants = self.__object.supportedPythonVariants()
195 198
196 if self.__supportedVariants: 199 if self.__supportedVariants:
197 try: 200 try:
198 self.__e5project.registerProjectType("Django", self.trUtf8("Django"), 201 self.__e5project.registerProjectType(
202 "Django", self.trUtf8("Django"),
199 self.fileTypesCallback, 203 self.fileTypesCallback,
200 lexerAssociationCallback=self.lexerAssociationCallback, 204 lexerAssociationCallback=self.lexerAssociationCallback,
201 binaryTranslationsCallback=self.binaryTranslationsCallback, 205 binaryTranslationsCallback=self.binaryTranslationsCallback,
202 progLanguages=self.__supportedVariants[:]) 206 progLanguages=self.__supportedVariants[:])
203 except TypeError: 207 except TypeError:
204 # for backward compatibility 208 # for backward compatibility
205 self.__e5project.registerProjectType("Django", self.trUtf8("Django"), 209 self.__e5project.registerProjectType(
210 "Django", self.trUtf8("Django"),
206 self.fileTypesCallback, 211 self.fileTypesCallback,
207 lexerAssociationCallback=self.lexerAssociationCallback, 212 lexerAssociationCallback=self.lexerAssociationCallback,
208 binaryTranslationsCallback=self.binaryTranslationsCallback) 213 binaryTranslationsCallback=self.binaryTranslationsCallback)
209 214
210 from Project.ProjectBrowser import SourcesBrowserFlag, FormsBrowserFlag, \ 215 from Project.ProjectBrowser import SourcesBrowserFlag, \
211 TranslationsBrowserFlag, OthersBrowserFlag 216 FormsBrowserFlag, TranslationsBrowserFlag, OthersBrowserFlag
212 Preferences.setProjectBrowserFlagsDefault("Django", 217 Preferences.setProjectBrowserFlagsDefault(
213 SourcesBrowserFlag | \ 218 "Django",
214 FormsBrowserFlag | \ 219 SourcesBrowserFlag | FormsBrowserFlag |
215 TranslationsBrowserFlag | \ 220 TranslationsBrowserFlag | OthersBrowserFlag,
216 OthersBrowserFlag,
217 ) 221 )
218 222
219 if self.__e5project.isOpen(): 223 if self.__e5project.isOpen():
220 self.__projectOpened() 224 self.__projectOpened()
221 self.__object.projectOpenedHooks() 225 self.__object.projectOpenedHooks()
222 226
223 e5App().getObject("Project").projectOpened.connect(self.__projectOpened) 227 e5App().getObject("Project").projectOpened.connect(
224 e5App().getObject("Project").projectClosed.connect(self.__projectClosed) 228 self.__projectOpened)
225 e5App().getObject("Project").newProject.connect(self.__projectOpened) 229 e5App().getObject("Project").projectClosed.connect(
230 self.__projectClosed)
231 e5App().getObject("Project").newProject.connect(
232 self.__projectOpened)
226 233
227 e5App().getObject("Project").projectOpenedHooks.connect( 234 e5App().getObject("Project").projectOpenedHooks.connect(
228 self.__object.projectOpenedHooks) 235 self.__object.projectOpenedHooks)
229 e5App().getObject("Project").projectClosedHooks.connect( 236 e5App().getObject("Project").projectClosedHooks.connect(
230 self.__object.projectClosedHooks) 237 self.__object.projectClosedHooks)
240 """ 247 """
241 Public method to deactivate this plugin. 248 Public method to deactivate this plugin.
242 """ 249 """
243 e5App().unregisterPluginObject("ProjectDjango") 250 e5App().unregisterPluginObject("ProjectDjango")
244 251
245 e5App().getObject("Project").projectOpened.disconnect(self.__projectOpened) 252 e5App().getObject("Project").projectOpened.disconnect(
246 e5App().getObject("Project").projectClosed.disconnect(self.__projectClosed) 253 self.__projectOpened)
247 e5App().getObject("Project").newProject.disconnect(self.__projectOpened) 254 e5App().getObject("Project").projectClosed.disconnect(
255 self.__projectClosed)
256 e5App().getObject("Project").newProject.disconnect(
257 self.__projectOpened)
248 258
249 e5App().getObject("Project").projectOpenedHooks.disconnect( 259 e5App().getObject("Project").projectOpenedHooks.disconnect(
250 self.__object.projectOpenedHooks) 260 self.__object.projectOpenedHooks)
251 e5App().getObject("Project").projectClosedHooks.disconnect( 261 e5App().getObject("Project").projectClosedHooks.disconnect(
252 self.__object.projectClosedHooks) 262 self.__object.projectClosedHooks)
268 Private method to load the translation file. 278 Private method to load the translation file.
269 """ 279 """
270 if self.__ui is not None: 280 if self.__ui is not None:
271 loc = self.__ui.getLocale() 281 loc = self.__ui.getLocale()
272 if loc and loc != "C": 282 if loc and loc != "C":
273 locale_dir = \ 283 locale_dir = os.path.join(
274 os.path.join(os.path.dirname(__file__), "ProjectDjango", "i18n") 284 os.path.dirname(__file__), "ProjectDjango", "i18n")
275 translation = "django_%s" % loc 285 translation = "django_%s" % loc
276 translator = QTranslator(None) 286 translator = QTranslator(None)
277 loaded = translator.load(translation, locale_dir) 287 loaded = translator.load(translation, locale_dir)
278 if loaded: 288 if loaded:
279 self.__translator = translator 289 self.__translator = translator
280 e5App().installTranslator(self.__translator) 290 e5App().installTranslator(self.__translator)
281 else: 291 else:
282 print("Warning: translation file '{0}' could not be loaded.".format( 292 print("Warning: translation file '{0}' could not be"
283 translation)) 293 " loaded.".format(translation))
284 print("Using default.") 294 print("Using default.")
285 295
286 def __projectOpened(self): 296 def __projectOpened(self):
287 """ 297 """
288 Private slot to handle the projectOpened signal. 298 Private slot to handle the projectOpened signal.
289 """ 299 """
290 if self.__e5project.getProjectType() == "Django": 300 if self.__e5project.getProjectType() == "Django":
291 projectAct = self.__ui.getMenuBarAction("project") 301 projectAct = self.__ui.getMenuBarAction("project")
292 actions = self.__ui.menuBar().actions() 302 actions = self.__ui.menuBar().actions()
293 insertAct = actions[actions.index(projectAct) + 1] 303 insertAct = actions[actions.index(projectAct) + 1]
294 self.__mainAct = self.__ui.menuBar().insertMenu(insertAct, self.__mainMenu) 304 self.__mainAct = self.__ui.menuBar().insertMenu(
305 insertAct, self.__mainMenu)
295 306
296 def __projectClosed(self): 307 def __projectClosed(self):
297 """ 308 """
298 Private slot to handle the projectClosed signal. 309 Private slot to handle the projectClosed signal.
299 """ 310 """
322 fileTypes = {} 333 fileTypes = {}
323 return fileTypes 334 return fileTypes
324 335
325 def lexerAssociationCallback(self, filename): 336 def lexerAssociationCallback(self, filename):
326 """ 337 """
327 Public method to get the lexer association of the Django project type for 338 Public method to get the lexer association of the Django project type
328 a file. 339 for a file.
329 340
330 @param filename name of the file (string) 341 @param filename name of the file (string)
331 @return name of the lexer (string) (Pygments lexers are prefixed with 'Pygments|') 342 @return name of the lexer (string) (Pygments lexers are prefixed with
343 'Pygments|')
332 """ 344 """
333 for pattern, language in self.lexerAssociations.items(): 345 for pattern, language in self.lexerAssociations.items():
334 if fnmatch.fnmatch(filename, pattern): 346 if fnmatch.fnmatch(filename, pattern):
335 return language 347 return language
336 348
351 def getPreferences(self, key): 363 def getPreferences(self, key):
352 """ 364 """
353 Public method to retrieve the various settings. 365 Public method to retrieve the various settings.
354 366
355 @param key the key of the value to get 367 @param key the key of the value to get
356 @param prefClass preferences class used as the storage area
357 @return the requested setting 368 @return the requested setting
358 """ 369 """
359 if key in ["RecentNumberApps"]: 370 if key in ["RecentNumberApps"]:
360 return int(Preferences.Prefs.settings.value(self.PreferencesKey + "/" + key, 371 return int(Preferences.Prefs.settings.value(
361 self.__defaults[key])) 372 self.PreferencesKey + "/" + key, self.__defaults[key]))
362 elif key in ["UsePlainPythonPy2", "UsePlainPythonPy3", "UseIPv6"]: 373 elif key in ["UsePlainPythonPy2", "UsePlainPythonPy3", "UseIPv6"]:
363 return Preferences.toBool(Preferences.Prefs.settings.value( 374 return Preferences.toBool(Preferences.Prefs.settings.value(
364 self.PreferencesKey + "/" + key, self.__defaults[key])) 375 self.PreferencesKey + "/" + key, self.__defaults[key]))
365 else: 376 else:
366 return Preferences.Prefs.settings.value(self.PreferencesKey + "/" + key, 377 return Preferences.Prefs.settings.value(
367 self.__defaults[key]) 378 self.PreferencesKey + "/" + key, self.__defaults[key])
368 379
369 def setPreferences(self, key, value): 380 def setPreferences(self, key, value):
370 """ 381 """
371 Public method to store the various settings. 382 Public method to store the various settings.
372 383
373 @param key the key of the setting to be set (string) 384 @param key the key of the setting to be set (string)
374 @param value the value to be set 385 @param value the value to be set
375 @param prefClass preferences class used as the storage area 386 """
376 """ 387 Preferences.Prefs.settings.setValue(
377 Preferences.Prefs.settings.setValue(self.PreferencesKey + "/" + key, value) 388 self.PreferencesKey + "/" + key, value)
378 389
379 if key in ["VirtualEnvironmentPy2", "VirtualEnvironmentPy3"]: 390 if key in ["VirtualEnvironmentPy2", "VirtualEnvironmentPy3"]:
380 self.__reregisterProjectType() 391 self.__reregisterProjectType()
381 elif key == "TranslationsEditor": 392 elif key == "TranslationsEditor":
382 if self.__object: 393 if self.__object:
393 404
394 # step 2: register again with new language settings 405 # step 2: register again with new language settings
395 self.__supportedVariants = supportedVariants 406 self.__supportedVariants = supportedVariants
396 if self.__supportedVariants: 407 if self.__supportedVariants:
397 try: 408 try:
398 self.__e5project.registerProjectType("Django", self.trUtf8("Django"), 409 self.__e5project.registerProjectType(
410 "Django", self.trUtf8("Django"),
399 self.fileTypesCallback, 411 self.fileTypesCallback,
400 lexerAssociationCallback=self.lexerAssociationCallback, 412 lexerAssociationCallback=self.lexerAssociationCallback,
401 binaryTranslationsCallback=self.binaryTranslationsCallback, 413 binaryTranslationsCallback=
414 self.binaryTranslationsCallback,
402 progLanguages=self.__supportedVariants[:]) 415 progLanguages=self.__supportedVariants[:])
403 except TypeError: 416 except TypeError:
404 # for backward compatibility 417 # for backward compatibility
405 self.__e5project.registerProjectType("Django", self.trUtf8("Django"), 418 self.__e5project.registerProjectType(
419 "Django", self.trUtf8("Django"),
406 self.fileTypesCallback, 420 self.fileTypesCallback,
407 lexerAssociationCallback=self.lexerAssociationCallback, 421 lexerAssociationCallback=self.lexerAssociationCallback,
408 binaryTranslationsCallback=self.binaryTranslationsCallback) 422 binaryTranslationsCallback=
423 self.binaryTranslationsCallback)

eric ide

mercurial