ProjectDjango/Project.py

changeset 9
8fe581309106
parent 8
07d64408f2b7
child 10
77b4a30cdbbf
equal deleted inserted replaced
8:07d64408f2b7 9:8fe581309106
10 import sys 10 import sys
11 import os 11 import os
12 import re 12 import re
13 13
14 from PyQt4.QtCore import QObject, QProcess, QTimer, QUrl, QFileInfo 14 from PyQt4.QtCore import QObject, QProcess, QTimer, QUrl, QFileInfo
15 from PyQt4.QtGui import QMenu, QInputDialog, QLineEdit, QDesktopServices, QDialog, \ 15 from PyQt4.QtGui import QMenu, QInputDialog, QLineEdit, QDesktopServices, QDialog
16 QFileDialog
17 16
18 from E5Gui.E5Application import e5App 17 from E5Gui.E5Application import e5App
19 from E5Gui import E5MessageBox, E5FileDialog 18 from E5Gui import E5MessageBox, E5FileDialog
20 from E5Gui.E5Action import E5Action 19 from E5Gui.E5Action import E5Action
21 20
21 from Globals import isWindowsPlatform
22
22 from .DjangoDialog import DjangoDialog 23 from .DjangoDialog import DjangoDialog
23 24
24 import Preferences 25 import Preferences
25 import Utilities 26 import Utilities
27 import UI.PixmapCache
26 28
27 29
28 class DjangoNoSiteSelectedException(Exception): 30 class DjangoNoSiteSelectedException(Exception):
29 """ 31 """
30 Exception thrown to signal, that there is no current site. 32 Exception thrown to signal, that there is no current site.
57 self.__serverProc = None 59 self.__serverProc = None
58 self.__testServerProc = None 60 self.__testServerProc = None
59 61
60 self.__recentApplications = [] 62 self.__recentApplications = []
61 self.__loadRecentApplications() 63 self.__loadRecentApplications()
64
65 self.__djangoVersion = ""
62 66
63 def initActions(self): 67 def initActions(self):
64 """ 68 """
65 Public method to define the Pyramid actions. 69 Public method to define the Pyramid actions.
66 """ 70 """
757 self.__ui, 761 self.__ui,
758 self.trUtf8("New Form"), 762 self.trUtf8("New Form"),
759 path, 763 path,
760 filter, 764 filter,
761 None, 765 None,
762 QFileDialog.Options(QFileDialog.DontConfirmOverwrite)) 766 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
763 767
764 if not fname: 768 if not fname:
765 # user aborted or didn't enter a filename 769 # user aborted or didn't enter a filename
766 return 770 return
767 771
821 """ 825 """
822 if self.__serverProc is not None: 826 if self.__serverProc is not None:
823 self.__serverProcFinished() 827 self.__serverProcFinished()
824 self.__setCurrentSite(None) 828 self.__setCurrentSite(None)
825 829
830 def __getVirtualEnvironment(self):
831 """
832 Private method to get the path of the virtual environment.
833
834 @return path of the virtual environment (string)
835 """
836 language = self.__e5project.getProjectLanguage()
837 if language == "Python3":
838 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy3")
839 elif language == "Python2":
840 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy2")
841 else:
842 virtEnv = ""
843 if virtEnv and not os.path.exists(virtEnv):
844 virtEnv = ""
845 return virtEnv
846
847 def __getDjangoAdminCommand(self):
848 """
849 Public method to build a django-admin.py command.
850
851 @param cmd command (string)
852 @return full pyramid command (string)
853 """
854 virtualEnv = self.__getVirtualEnvironment()
855 if virtualEnv:
856 if isWindowsPlatform():
857 cmd = os.path.join(virtualEnv, "Scripts", "django-admin.py")
858 else:
859 cmds = [
860 os.path.join(virtualEnv, "bin", "django-admin.py"),
861 os.path.join(virtualEnv, "bin", "django-admin"),
862 os.path.join(virtualEnv, "local", "bin", "django-admin.py"),
863 os.path.join(virtualEnv, "local", "bin", "django-admin"),
864 ]
865 for cmd in cmds:
866 if os.path.exists(cmd):
867 break
868 else:
869 ""
870 else:
871 if isWindowsPlatform():
872 cmd = os.path.join(sys.exec_prefix, "Scripts", "django-admin.py")
873 else:
874 if Utilities.isinpath("django-admin.py"):
875 cmd = "django-admin.py"
876 elif Utilities.isinpath("django-admin"):
877 cmd = "django-admin"
878 else:
879 # fall back
880 cmd = "django-admin.py"
881 return cmd
882
883 def __getPythonExecutable(self):
884 """
885 Public method to build the Python command.
886
887 @return python command (string)
888 """
889 python = "python"
890 if isWindowsPlatform():
891 python += ".exe"
892 else:
893 language = self.__e5project.getProjectLanguage()
894 if language == "Python3":
895 python = "python3"
896 elif language == "Python2":
897 python = "python2"
898 virtualEnv = self.__getVirtualEnvironment()
899 if virtualEnv:
900 if isWindowsPlatform():
901 python = os.path.join(virtualEnv, "Scripts", python)
902 if not os.path.exists(python):
903 python = os.path.join(virtualEnv, python)
904 else:
905 python = os.path.join(virtualEnv, "bin", python)
906
907 return python
908
826 def __djangoInfo(self): 909 def __djangoInfo(self):
827 """ 910 """
828 Private slot to show some info about Django. 911 Private slot to show some info about Django.
829 """ 912 """
830 from django import VERSION 913 version = self.getDjangoVersion()
831 version = '.'.join([str(i) for i in VERSION[:-1]]) 914 url = "http://www.djangoproject.com"
832 if VERSION[-1]: 915
833 version += '-' + VERSION[-1] 916 msgBox = E5MessageBox.E5MessageBox(E5MessageBox.Question,
834
835 E5MessageBox.about(self.__ui,
836 self.trUtf8("About Django"), 917 self.trUtf8("About Django"),
837 self.trUtf8( 918 self.trUtf8(
838 "<p>Django is a high-level Python Web framework that encourages rapid " 919 "<p>Django is a high-level Python Web framework that encourages rapid "
839 "development and clean, pragmatic design.</p>" 920 "development and clean, pragmatic design.</p>"
840 "<p><table>" 921 "<p><table>"
841 "<tr><td>Version:</td><td>{0}</td></tr>" 922 "<tr><td>Version:</td><td>{0}</td></tr>"
842 "<tr><td>URL:</td><td><a href=\"http://www.djangoproject.com\">" 923 "<tr><td>URL:</td><td><a href=\"{1}\">"
843 "http://www.djangoproject.com</a></td></tr>" 924 "{1}</a></td></tr>"
844 "</table></p>" 925 "</table></p>"
845 ).format(version) 926 ).format(version, url),
846 ) 927 modal=True,
928 buttons=E5MessageBox.Ok)
929 msgBox.setIconPixmap(UI.PixmapCache.getPixmap(
930 os.path.join("ProjectDjango", "icons", "django64.png")))
931 msgBox.exec_()
847 932
848 def getDjangoVersion(self): 933 def getDjangoVersion(self):
849 """ 934 """
850 Public method to get the Django version. 935 Public method to get the Django version.
851 936
852 @return Django version as a tuple 937 @return Django version as a tuple
853 """ 938 """
854 from django import VERSION 939 if not self.__djangoVersion:
855 return VERSION 940 ioEncoding = Preferences.getSystem("IOEncoding")
941 cmd = self.__getDjangoAdminCommand()
942
943 process = QProcess()
944 process.start(cmd, ['--version'])
945 procStarted = process.waitForStarted()
946 if procStarted:
947 finished = process.waitForFinished(30000)
948 if finished and process.exitCode() == 0:
949 output = \
950 str(process.readAllStandardOutput(), ioEncoding, 'replace')
951 self.__djangoVersion = output.splitlines()[0].strip()
952
953 return self.__djangoVersion
856 954
857 def __getApplications(self): 955 def __getApplications(self):
858 """ 956 """
859 Private method to ask the user for a list of application names. 957 Private method to ask the user for a list of application names.
860 958
920 Public method to get the path of the eric5 project. 1018 Public method to get the path of the eric5 project.
921 1019
922 @return path of the eric5 project (string) 1020 @return path of the eric5 project (string)
923 """ 1021 """
924 return self.__e5project.getProjectPath() 1022 return self.__e5project.getProjectPath()
925
926 def __getPythonExecutable(self):
927 """
928 Private method to determine the name of the Python executable.
929
930 @return Python executable (string)
931 """
932 # TODO: make this differentiate between Python2 and Python3
933 return sys.executable.replace("pythonw", "python")
934 1023
935 def __showHelpIndex(self): 1024 def __showHelpIndex(self):
936 """ 1025 """
937 Private slot to show the help index page. 1026 Private slot to show the help index page.
938 """ 1027 """
994 title = self.trUtf8("Start Django Project") 1083 title = self.trUtf8("Start Django Project")
995 1084
996 args = [] 1085 args = []
997 if Utilities.isWindowsPlatform(): 1086 if Utilities.isWindowsPlatform():
998 args.append(self.__getPythonExecutable()) 1087 args.append(self.__getPythonExecutable())
999 args.append(os.path.join(sys.exec_prefix, "Scripts", "django-admin.py")) 1088 args.append(self.__getDjangoAdminCommand())
1000 else: 1089 else:
1001 if Utilities.isinpath("django-admin.py"): 1090 cmd = self.__getDjangoAdminCommand()
1002 args.append("django-admin.py") 1091 if cmd:
1003 elif Utilities.isinpath("django-admin"): 1092 args.append(cmd)
1004 args.append("django-admin")
1005 else: 1093 else:
1006 # fall back 1094 E5MessageBox.critical(self.__ui,
1007 args.append("django-admin.py") 1095 title,
1096 self.trUtf8("""<p>The <b>django-admin.py</b> script is not in"""
1097 """ the path. Aborting...</p>"""))
1098 return
1099
1008 args.append("startproject") 1100 args.append("startproject")
1009 args.append(projectName) 1101 args.append(projectName)
1010 1102
1011 dia = DjangoDialog(title, 1103 dia = DjangoDialog(title,
1012 msgSuccess=self.trUtf8("Django project created successfully.")) 1104 msgSuccess=self.trUtf8("Django project created successfully."))
1060 1152
1061 args = [] 1153 args = []
1062 if isGlobal: 1154 if isGlobal:
1063 if Utilities.isWindowsPlatform(): 1155 if Utilities.isWindowsPlatform():
1064 args.append(self.__getPythonExecutable()) 1156 args.append(self.__getPythonExecutable())
1065 args.append(os.path.join(sys.exec_prefix, "Scripts", "django-admin.py")) 1157 args.append(self.__getDjangoAdminCommand())
1066 else: 1158 else:
1067 if Utilities.isinpath("django-admin.py"): 1159 cmd = self.__getDjangoAdminCommand()
1068 args.append("django-admin.py") 1160 if cmd:
1069 elif Utilities.isinpath("django-admin"): 1161 args.append(cmd)
1070 args.append("django-admin")
1071 else: 1162 else:
1072 # fall back 1163 E5MessageBox.critical(self.__ui,
1073 args.append("django-admin.py") 1164 title,
1165 self.trUtf8("""<p>The <b>django-admin.py</b> script is not in"""
1166 """ the path. Aborting...</p>"""))
1167 return
1074 else: 1168 else:
1075 args.append(self.__getPythonExecutable()) 1169 args.append(self.__getPythonExecutable())
1076 args.append("manage.py") 1170 args.append("manage.py")
1077 try: 1171 try:
1078 path = self.__sitePath() 1172 path = self.__sitePath()
1574 args = Utilities.parseOptionString(consoleCmd) 1668 args = Utilities.parseOptionString(consoleCmd)
1575 args[0] = Utilities.getExecutablePath(args[0]) 1669 args[0] = Utilities.getExecutablePath(args[0])
1576 args.append(self.__getPythonExecutable()) 1670 args.append(self.__getPythonExecutable())
1577 args.append("manage.py") 1671 args.append("manage.py")
1578 args.append("shell") 1672 args.append("shell")
1579 if self.__plugin.getPreferences("UsePlainPython"): 1673 language = self.__e5project.getProjectLanguage()
1580 args.append("--plain") 1674 if language == "Python2":
1675 if self.__plugin.getPreferences("UsePlainPythonPy2"):
1676 args.append("--plain")
1677 else:
1678 if self.__plugin.getPreferences("UsePlainPythonPy3"):
1679 args.append("--plain")
1581 try: 1680 try:
1582 wd = self.__sitePath() 1681 wd = self.__sitePath()
1583 started, pid = QProcess.startDetached(args[0], args[1:], wd) 1682 started, pid = QProcess.startDetached(args[0], args[1:], wd)
1584 if not started: 1683 if not started:
1585 E5MessageBox.critical(None, 1684 E5MessageBox.critical(None,

eric ide

mercurial