ProjectDjango/Project.py

changeset 163
1622130167bb
parent 161
208fced62e00
child 165
80afe6639c6c
equal deleted inserted replaced
162:2df96a504e66 163:1622130167bb
5 5
6 """ 6 """
7 Module implementing the Django project support. 7 Module implementing the Django project support.
8 """ 8 """
9 9
10 from __future__ import unicode_literals
11 try:
12 str = unicode # __IGNORE_WARNING__
13 except NameError:
14 pass
15
16 import sys
17 import os 10 import os
18 import re 11 import re
19 import shutil 12 import shutil
20 13
21 from PyQt5.QtCore import QObject, QTimer, QUrl, QFileInfo 14 from PyQt5.QtCore import QObject, QTimer, QUrl, QFileInfo
115 self.__plugin = plugin 108 self.__plugin = plugin
116 self.__iconSuffix = iconSuffix 109 self.__iconSuffix = iconSuffix
117 self.__ui = parent 110 self.__ui = parent
118 111
119 self.__e5project = e5App().getObject("Project") 112 self.__e5project = e5App().getObject("Project")
120 try: 113 self.__virtualEnvManager = e5App().getObject("VirtualEnvManager")
121 self.__virtualEnvManager = e5App().getObject("VirtualEnvManager")
122 except KeyError:
123 self.__virtualEnvManager = None
124 self.__hooksInstalled = False 114 self.__hooksInstalled = False
125 115
126 self.__menus = {} # dictionary with references to menus 116 self.__menus = {} # dictionary with references to menus
127 117
128 self.__serverProc = None 118 self.__serverProc = None
1278 if os.access(exe, os.X_OK) and exe not in paths: 1268 if os.access(exe, os.X_OK) and exe not in paths:
1279 paths.append(exe) 1269 paths.append(exe)
1280 1270
1281 return paths 1271 return paths
1282 1272
1283 # TODO: eliminate Python2
1284 def supportedPythonVariants(self): 1273 def supportedPythonVariants(self):
1285 """ 1274 """
1286 Public method to get the supported Python variants. 1275 Public method to get the supported Python variants.
1287 1276
1288 @return list of supported Python variants (list of strings) 1277 @return list of supported Python variants (list of strings)
1315 variants.append(variant) 1304 variants.append(variant)
1316 break 1305 break
1317 1306
1318 return variants 1307 return variants
1319 1308
1320 # TODO: eliminate Python2
1321 def __isSuitableForVariant(self, variant, line0): 1309 def __isSuitableForVariant(self, variant, line0):
1322 """ 1310 """
1323 Private method to test, if a detected command file is suitable for the 1311 Private method to test, if a detected command file is suitable for the
1324 given Python variant. 1312 given Python variant.
1325 1313
1345 for (string, one of '' or 'Python3') 1333 for (string, one of '' or 'Python3')
1346 @return path of the virtual environment (string) 1334 @return path of the virtual environment (string)
1347 """ 1335 """
1348 if not language: 1336 if not language:
1349 language = self.__e5project.getProjectLanguage() 1337 language = self.__e5project.getProjectLanguage()
1350 if self.__virtualEnvManager: 1338 if language == "Python3":
1351 if language == "Python3": 1339 venvName = self.__plugin.getPreferences(
1352 venvName = self.__plugin.getPreferences( 1340 "VirtualEnvironmentNamePy3")
1353 "VirtualEnvironmentNamePy3")
1354 else:
1355 venvName = ""
1356 if venvName:
1357 virtEnv = self.__virtualEnvManager.getVirtualenvDirectory(
1358 venvName)
1359 if not virtEnv:
1360 virtEnv = os.path.dirname(
1361 self.__virtualEnvManager.getVirtualenvInterpreter(
1362 venvName))
1363 if virtEnv.endswith(("Scripts", "bin")):
1364 virtEnv = os.path.dirname(virtEnv)
1365 else:
1366 virtEnv = ""
1367 else: 1341 else:
1368 # backward compatibility 1342 venvName = ""
1369 if language == "Python3": 1343 if venvName:
1370 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy3") 1344 virtEnv = self.__virtualEnvManager.getVirtualenvDirectory(
1371 else: 1345 venvName)
1372 virtEnv = "" 1346 if not virtEnv:
1347 virtEnv = os.path.dirname(
1348 self.__virtualEnvManager.getVirtualenvInterpreter(
1349 venvName))
1350 if virtEnv.endswith(("Scripts", "bin")):
1351 virtEnv = os.path.dirname(virtEnv)
1352 else:
1353 virtEnv = ""
1373 if virtEnv and not os.path.exists(virtEnv): 1354 if virtEnv and not os.path.exists(virtEnv):
1374 virtEnv = "" 1355 virtEnv = ""
1375 return virtEnv 1356 return virtEnv
1376 1357
1377 def __getDebugEnvironment(self, language=""): 1358 def __getDebugEnvironment(self, language=""):
1382 for (string, one of '' or 'Python3') 1363 for (string, one of '' or 'Python3')
1383 @return path of the debugger environment (string) 1364 @return path of the debugger environment (string)
1384 """ 1365 """
1385 if not language: 1366 if not language:
1386 language = self.__e5project.getProjectLanguage() 1367 language = self.__e5project.getProjectLanguage()
1387 if self.__virtualEnvManager: 1368 debugEnv = self.__getVirtualEnvironment(language)
1388 debugEnv = self.__getVirtualEnvironment(language) 1369 if not debugEnv:
1389 if not debugEnv:
1390 if language == "Python3":
1391 venvName = Preferences.getDebugger("Python3VirtualEnv")
1392 else:
1393 venvName = ""
1394
1395 if venvName:
1396 debugEnv = self.__virtualEnvManager.getVirtualenvDirectory(
1397 venvName)
1398 else:
1399 debugEnv = ""
1400 else:
1401 # backward compatibility
1402 if language == "Python3": 1370 if language == "Python3":
1403 debugEnv = Preferences.getDebugger("Python3Interpreter") 1371 venvName = Preferences.getDebugger("Python3VirtualEnv")
1404 if not debugEnv and sys.version_info[0] >= 3:
1405 debugEnv = sys.executable
1406 else: 1372 else:
1407 debugEnv = sys.executable 1373 venvName = ""
1408 debugEnv = os.path.dirname(debugEnv) 1374
1409 if debugEnv and not os.path.exists(debugEnv): 1375 if venvName:
1410 if language == "Python3" and sys.version_info[0] >= 3: 1376 debugEnv = self.__virtualEnvManager.getVirtualenvDirectory(
1411 debugEnv = sys.exec_prefix 1377 venvName)
1412 else: 1378 else:
1413 debugEnv = "" 1379 debugEnv = ""
1414 return debugEnv 1380 return debugEnv
1415 1381
1416 def __getDjangoAdminCommand(self, language=""): 1382 def __getDjangoAdminCommand(self, language=""):
1417 """ 1383 """
1418 Private method to build a django-admin.py command. 1384 Private method to build a django-admin.py command.
1486 Private method to build the Python command. 1452 Private method to build the Python command.
1487 1453
1488 @return python command (string) 1454 @return python command (string)
1489 """ 1455 """
1490 language = self.__e5project.getProjectLanguage() 1456 language = self.__e5project.getProjectLanguage()
1491 if self.__virtualEnvManager: 1457 if language == "Python3":
1492 if language == "Python3": 1458 venvName = self.__plugin.getPreferences(
1493 venvName = self.__plugin.getPreferences( 1459 "VirtualEnvironmentNamePy3")
1494 "VirtualEnvironmentNamePy3") 1460 if not venvName:
1495 if not venvName: 1461 # if none configured, use the global one
1496 # if none configured, use the global one 1462 venvName = Preferences.getDebugger("Python3VirtualEnv")
1497 venvName = Preferences.getDebugger("Python3VirtualEnv")
1498 else:
1499 venvName = ""
1500 if venvName:
1501 python = self.__virtualEnvManager.getVirtualenvInterpreter(
1502 venvName)
1503 else:
1504 python = ""
1505 else: 1463 else:
1506 # backward compatibility 1464 venvName = ""
1507 virtualEnv = self.__getVirtualEnvironment() 1465 if venvName:
1508 if isWindowsPlatform(): 1466 python = self.__virtualEnvManager.getVirtualenvInterpreter(
1509 pythonExeList = ["python.exe", "pypy.exe"] 1467 venvName)
1510 if not virtualEnv: 1468 else:
1511 virtualEnv = self.__getDebugEnvironment(language) 1469 python = ""
1512 for pythonExe in pythonExeList:
1513 for python in [
1514 os.path.join(virtualEnv, "Scripts", pythonExe),
1515 os.path.join(virtualEnv, "bin", pythonExe),
1516 os.path.join(virtualEnv, pythonExe)
1517 ]:
1518 if os.path.exists(python):
1519 break
1520 else:
1521 python = ""
1522
1523 if python:
1524 break
1525 else:
1526 python = ""
1527 else:
1528 pythonExeList = ["python3", "pypy3"]
1529 if not virtualEnv:
1530 virtualEnv = self.__getDebugEnvironment(language)
1531
1532 for pythonExe in pythonExeList:
1533 for python in [
1534 os.path.join(virtualEnv, "bin", pythonExe),
1535 # omit the version character
1536 os.path.join(virtualEnv, "bin", pythonExe)[:-1],
1537 os.path.join(virtualEnv, pythonExe),
1538 # omit the version character
1539 os.path.join(virtualEnv, pythonExe)[:-1],
1540 ]:
1541 if os.path.exists(python):
1542 break
1543 else:
1544 python = ""
1545
1546 if python:
1547 break
1548 else:
1549 python = ""
1550 1470
1551 return python 1471 return python
1552 1472
1553 def __djangoInfo(self): 1473 def __djangoInfo(self):
1554 """ 1474 """
2092 curSite = self.__currentSite 2012 curSite = self.__currentSite
2093 self.selectSiteAct.setText( 2013 self.selectSiteAct.setText(
2094 self.tr('&Current Django project ({0})').format(curSite)) 2014 self.tr('&Current Django project ({0})').format(curSite))
2095 2015
2096 if self.__currentSite is None: 2016 if self.__currentSite is None:
2097 try: 2017 self.__e5project.setTranslationPattern("")
2098 self.__e5project.setTranslationPattern("")
2099 except AttributeError:
2100 # backward compatibility
2101 self.__e5project.pdata["TRANSLATIONPATTERN"] = []
2102 else: 2018 else:
2103 try: 2019 self.__e5project.setTranslationPattern(
2104 self.__e5project.setTranslationPattern( 2020 os.path.join(site, "locale", "%language%", "LC_MESSAGES",
2105 os.path.join(site, "locale", "%language%", "LC_MESSAGES", 2021 "django.po")
2106 "django.po") 2022 )
2107 )
2108 except AttributeError:
2109 # backward compatibility
2110 self.__e5project.pdata["TRANSLATIONPATTERN"] = [
2111 os.path.join(site, "locale", "%language%", "LC_MESSAGES",
2112 "django.po")
2113 ]
2114 2023
2115 def __site(self): 2024 def __site(self):
2116 """ 2025 """
2117 Private method to get the name of the current site. 2026 Private method to get the name of the current site.
2118 2027

eric ide

mercurial