ProjectDjango/Project.py

changeset 161
208fced62e00
parent 159
e08458938716
child 163
1622130167bb
--- a/ProjectDjango/Project.py	Sat May 23 11:14:54 2020 +0200
+++ b/ProjectDjango/Project.py	Mon Jun 22 19:32:12 2020 +0200
@@ -1099,17 +1099,18 @@
         Public method to add our hook methods.
         """
         if self.__e5project.getProjectType() == "Django":
-            self.__formsBrowser = \
+            self.__formsBrowser = (
                 e5App().getObject("ProjectBrowser").getProjectBrowser("forms")
+            )
             self.__formsBrowser.addHookMethodAndMenuEntry(
                 "newForm",
                 self.newForm, self.tr("New template..."))
             
             self.__e5project.projectLanguageAddedByCode.connect(
                 self.__projectLanguageAdded)
-            self.__translationsBrowser = \
-                e5App().getObject("ProjectBrowser")\
-                .getProjectBrowser("translations")
+            self.__translationsBrowser = (
+                e5App().getObject("ProjectBrowser")
+                .getProjectBrowser("translations"))
             self.__translationsBrowser.addHookMethodAndMenuEntry(
                 "generateAll",
                 self.updateCatalogs, self.tr("Update all catalogs"))
@@ -1279,6 +1280,7 @@
         
         return paths
     
+    # TODO: eliminate Python2
     def supportedPythonVariants(self):
         """
         Public method to get the supported Python variants.
@@ -1286,7 +1288,7 @@
         @return list of supported Python variants (list of strings)
         """
         variants = []
-        for variant in 'Python2', 'Python3':
+        for variant in ['Python3']:
             virtEnv = self.__getVirtualEnvironment(variant)
             if virtEnv:
                 if self.__getDjangoAdminCommand(variant):
@@ -1315,27 +1317,23 @@
         
         return variants
     
+    # TODO: eliminate Python2
     def __isSuitableForVariant(self, variant, line0):
         """
         Private method to test, if a detected command file is suitable for the
         given Python variant.
         
         @param variant Python variant to test for
-        @type str (one of Python2 or Python3)
+        @type str
         @param line0 first line of the executable
         @type str
+        @return flag indicating a suitable file was found
+        @rtype bool
         """
-        assert variant in ("Python2", "Python3")
-        
         l0 = line0.lower()
         ok = (variant.lower() in l0 or
               "{0}.".format(variant[-1]) in l0)
-        if variant == "Python2":
-            ok |= "python3" not in l0 and "python" in l0
-            ok |= "pypy2" in l0
-            ok |= "pypy3" not in l0 and "pypy" in l0
-        else:
-            ok |= "pypy3" in l0
+        ok |= "pypy3" in l0
         
         return ok
     
@@ -1344,7 +1342,7 @@
         Private method to get the path of the virtual environment.
         
         @param language Python variant to get the virtual environment
-            for (string, one of '', 'Python2' or 'Python3')
+            for (string, one of '' or 'Python3')
         @return path of the virtual environment (string)
         """
         if not language:
@@ -1353,9 +1351,6 @@
             if language == "Python3":
                 venvName = self.__plugin.getPreferences(
                     "VirtualEnvironmentNamePy3")
-            elif language == "Python2":
-                venvName = self.__plugin.getPreferences(
-                    "VirtualEnvironmentNamePy2")
             else:
                 venvName = ""
             if venvName:
@@ -1373,8 +1368,6 @@
             # backward compatibility
             if language == "Python3":
                 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy3")
-            elif language == "Python2":
-                virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy2")
             else:
                 virtEnv = ""
         if virtEnv and not os.path.exists(virtEnv):
@@ -1386,7 +1379,7 @@
         Private method to get the path of the debugger environment.
         
         @param language Python variant to get the debugger environment
-            for (string, one of '', 'Python2' or 'Python3')
+            for (string, one of '' or 'Python3')
         @return path of the debugger environment (string)
         """
         if not language:
@@ -1396,8 +1389,6 @@
             if not debugEnv:
                 if language == "Python3":
                     venvName = Preferences.getDebugger("Python3VirtualEnv")
-                elif language == "Python2":
-                    venvName = Preferences.getDebugger("Python2VirtualEnv")
                 else:
                     venvName = ""
                 
@@ -1410,18 +1401,13 @@
             # backward compatibility
             if language == "Python3":
                 debugEnv = Preferences.getDebugger("Python3Interpreter")
-                if not debugEnv and sys.version_info[0] == 3:
-                    debugEnv = sys.executable
-            elif language == "Python2":
-                debugEnv = Preferences.getDebugger("PythonInterpreter")
-                if not debugEnv and sys.version_info[0] == 2:
+                if not debugEnv and sys.version_info[0] >= 3:
                     debugEnv = sys.executable
             else:
                 debugEnv = sys.executable
             debugEnv = os.path.dirname(debugEnv)
             if debugEnv and not os.path.exists(debugEnv):
-                if (language == "Python3" and sys.version_info[0] == 3) or \
-                   (language == "Python2" and sys.version_info[0] == 2):
+                if language == "Python3" and sys.version_info[0] >= 3:
                     debugEnv = sys.exec_prefix
                 else:
                     debugEnv = ""
@@ -1432,7 +1418,7 @@
         Private method to build a django-admin.py command.
         
         @param language Python variant to get the django-admin.py
-            command for (string, one of '', 'Python2' or 'Python3')
+            command for (string, one of '' or 'Python3')
         @return full django-admin.py command (string)
         """
         if not language:
@@ -1478,11 +1464,7 @@
                 else:
                     cmd = ""
             else:
-                if language == "Python2":
-                    cmds = ["django-admin2.py", "django-admin2",
-                            "django-admin.py-2.7", "django-admin.py-2.6"
-                            ]
-                elif language == "Python3":
+                if language == "Python3":
                     cmds = ["django-admin3.py", "django-admin3",
                             "django-admin.py-3.7", "django-admin.py-3.6",
                             "django-admin.py-3.5", "django-admin.py-3.4",
@@ -1513,12 +1495,6 @@
                 if not venvName:
                     # if none configured, use the global one
                     venvName = Preferences.getDebugger("Python3VirtualEnv")
-            elif language == "Python2":
-                venvName = self.__plugin.getPreferences(
-                    "VirtualEnvironmentNamePy2")
-                if not venvName:
-                    # if none configured, use the global one
-                    venvName = Preferences.getDebugger("Python2VirtualEnv")
             else:
                 venvName = ""
             if venvName:
@@ -1549,10 +1525,7 @@
                 else:
                     python = ""
             else:
-                if language == "Python3":
-                    pythonExeList = ["python3", "pypy3"]
-                elif language == "Python2":
-                    pythonExeList = ["python2", "pypy2"]
+                pythonExeList = ["python3", "pypy3"]
                 if not virtualEnv:
                     virtualEnv = self.__getDebugEnvironment(language)
                 
@@ -1708,8 +1681,8 @@
         
         maxRecentApps = self.__plugin.getPreferences("RecentNumberApps")
         if len(self.__recentApplications) > maxRecentApps:
-            self.__recentApplications = \
-                self.__recentApplications[:maxRecentApps]
+            self.__recentApplications = (
+                self.__recentApplications[:maxRecentApps])
         self.__saveRecentApplications()
     
     def __loadRecentTestData(self):
@@ -1766,8 +1739,8 @@
         maxRecentTestData = self.__plugin.getPreferences(
             "RecentNumberTestData")
         if len(self.__recentTestData[key]) > maxRecentTestData:
-            self.__recentTestData[key] = \
-                self.__recentTestData[key][:maxRecentTestData]
+            self.__recentTestData[key] = (
+                self.__recentTestData[key][:maxRecentTestData])
         self.__saveRecentTestData()
     
     def getProjectPath(self):
@@ -1920,8 +1893,10 @@
             if not os.path.exists(i18nPath):
                 os.makedirs(i18nPath)
             
-            if os.path.join(path, projectName) == \
-                    self.__e5project.getProjectPath():
+            if (
+                os.path.join(path, projectName) ==
+                self.__e5project.getProjectPath()
+            ):
                 self.__setCurrentSite("")
             else:
                 self.__setCurrentSite(projectName)
@@ -1981,7 +1956,7 @@
                     self.tr("""<p>The <b>django-admin.py</b> script"""
                             """ is not in the path."""
                             """ Aborting...</p>"""))
-                return
+                return False
         else:
             args.append(self.__getPythonExecutable())
             args.append("manage.py")
@@ -2202,8 +2177,10 @@
         """
         Private slot connected to the finished signal.
         """
-        if self.__serverProc is not None and \
-           self.__serverProc.state() != QProcess.NotRunning:
+        if (
+            self.__serverProc is not None and
+            self.__serverProc.state() != QProcess.NotRunning
+        ):
             self.__serverProc.terminate()
             QTimer.singleShot(2000, self.__serverProc.kill)
             self.__serverProc.waitForFinished(3000)
@@ -2258,8 +2235,8 @@
         Preferences.Prefs.rsettings.sync()
         rdb = Preferences.Prefs.rsettings.value(self.RecentDatabaseNamesKey)
         if rdb is not None:
-            maxRecentDatabaseNames = \
-                self.__plugin.getPreferences("RecentNumberDatabaseNames")
+            maxRecentDatabaseNames = (
+                self.__plugin.getPreferences("RecentNumberDatabaseNames"))
             self.__recentDatabaseNames = rdb[:maxRecentDatabaseNames]
     
     def __saveRecentDatabaseNames(self):
@@ -2291,11 +2268,11 @@
             self.__recentDatabaseNames.remove(dbName)
         self.__recentDatabaseNames.insert(0, dbName)
         
-        maxRecentDatabaseNames = \
-            self.__plugin.getPreferences("RecentNumberDatabaseNames")
+        maxRecentDatabaseNames = (
+            self.__plugin.getPreferences("RecentNumberDatabaseNames"))
         if len(self.__recentDatabaseNames) > maxRecentDatabaseNames:
-            self.__recentDatabaseNames = \
-                self.__recentDatabaseNames[:maxRecentDatabaseNames]
+            self.__recentDatabaseNames = (
+                self.__recentDatabaseNames[:maxRecentDatabaseNames])
         self.__saveRecentDatabaseNames()
     
     def __selectDatabaseName(self):
@@ -2565,8 +2542,9 @@
         
         title = self.tr("SQL Migrate")
         
-        from .DjangoMigrationSelectionDialog import \
+        from .DjangoMigrationSelectionDialog import (
             DjangoMigrationSelectionDialog
+        )
         dlg = DjangoMigrationSelectionDialog(migrations,
                                              migrationRequired=True,
                                              suffix=self.__iconSuffix)
@@ -2646,8 +2624,9 @@
                 self.tr("""No migrations available."""))
             return
         
-        from .DjangoMigrationSelectionDialog import \
+        from .DjangoMigrationSelectionDialog import (
             DjangoMigrationSelectionDialog
+        )
         dlg = DjangoMigrationSelectionDialog(migrations,
                                              suffix=self.__iconSuffix)
         if dlg.exec_() == QDialog.Accepted:
@@ -2824,8 +2803,9 @@
                 self.tr("""No migrations available."""))
             return
         
-        from .DjangoSquashMigrationSelectionDialog import \
+        from .DjangoSquashMigrationSelectionDialog import (
             DjangoSquashMigrationSelectionDialog
+        )
         dlg = DjangoSquashMigrationSelectionDialog(
             migrations, self, self.__iconSuffix)
         if dlg.exec_() == QDialog.Accepted:
@@ -2905,23 +2885,13 @@
             args.append(self.__getPythonExecutable())
             args.append("manage.py")
             args.append("shell")
-            language = self.__e5project.getProjectLanguage()
-            if language == "Python2":
-                if self.getDjangoVersion() < (1, 10, 0):
-                    if (self.__plugin.getPreferences("Python2ConsoleType") ==
-                            "python"):
-                        args.append("--plain")
-                else:
-                    args.append("--interface={0}".format(
-                        self.__plugin.getPreferences("Python2ConsoleType")))
+            if self.getDjangoVersion() < (1, 10, 0):
+                if (self.__plugin.getPreferences("Python3ConsoleType") ==
+                        "python"):
+                    args.append("--plain")
             else:
-                if self.getDjangoVersion() < (1, 10, 0):
-                    if (self.__plugin.getPreferences("Python3ConsoleType") ==
-                            "python"):
-                        args.append("--plain")
-                else:
-                    args.append("--interface={0}".format(
-                        self.__plugin.getPreferences("Python3ConsoleType")))
+                args.append("--interface={0}".format(
+                    self.__plugin.getPreferences("Python3ConsoleType")))
             try:
                 wd = self.__sitePath()
                 self.__adjustWorkingDirectory(args, wd)
@@ -2943,8 +2913,9 @@
         
         title = self.tr("Send Test Email")
         
-        from .DjangoSendTestEmailDataDialog import \
+        from .DjangoSendTestEmailDataDialog import (
             DjangoSendTestEmailDataDialog
+        )
         dlg = DjangoSendTestEmailDataDialog(self.__ui)
         if dlg.exec_() == QDialog.Accepted:
             managers, admins, recipients = dlg.getData()
@@ -3106,8 +3077,8 @@
                 self, self.__plugin.getPreferences("KeepTestDatabase"),
                 self.__ui)
             if dlg.exec_() == QDialog.Accepted:
-                labels, pattern, tags, excludeTags, keep, reverse = \
-                    dlg.getData()
+                labels, pattern, tags, excludeTags, keep, reverse = (
+                    dlg.getData())
                 
                 self.__plugin.setPreferences("KeepTestDatabase", keep)
                 
@@ -3146,8 +3117,9 @@
         consoleCmd = self.__isSpawningConsole(
             self.__plugin.getPreferences("ConsoleCommand"))[1]
         if consoleCmd:
-            from .DjangoRunTestServerDataDialog import \
+            from .DjangoRunTestServerDataDialog import (
                 DjangoRunTestServerDataDialog
+            )
             dlg = DjangoRunTestServerDataDialog(self, self.__ui)
             if dlg.exec_() == QDialog.Accepted:
                 fixtures = dlg.getData()
@@ -3178,8 +3150,8 @@
                         self.__testServerProc.setWorkingDirectory(
                             self.__sitePath())
                         self.__testServerProc.start(args[0], args[1:])
-                        serverProcStarted = \
-                            self.__testServerProc.waitForStarted()
+                        serverProcStarted = (
+                            self.__testServerProc.waitForStarted())
                     if not serverProcStarted:
                         E5MessageBox.critical(
                             None,
@@ -3193,8 +3165,10 @@
         """
         Private slot connected to the finished signal of the test server.
         """
-        if self.__testServerProc is not None and \
-           self.__testServerProc.state() != QProcess.NotRunning:
+        if (
+            self.__testServerProc is not None and
+            self.__testServerProc.state() != QProcess.NotRunning
+        ):
             self.__testServerProc.terminate()
             QTimer.singleShot(2000, self.__testServerProc.kill)
             self.__testServerProc.waitForFinished(3000)
@@ -3300,8 +3274,10 @@
         @return extracted locale (string) or None
         """
         if self.__e5project.getTranslationPattern():
-            pattern = self.__e5project.getTranslationPattern()\
+            pattern = (
+                self.__e5project.getTranslationPattern()
                 .replace("%language%", "(.*?)")
+            )
             match = re.search(pattern, filename)
             if match is not None:
                 loc = match.group(1)
@@ -3350,8 +3326,10 @@
         
         @param code language code of the new language (string)
         """
-        title = self.tr("Initializing message catalog for '{0}'")\
+        title = (
+            self.tr("Initializing message catalog for '{0}'")
             .format(code)
+        )
         
         args = []
         args.append(self.__getPythonExecutable())
@@ -3377,8 +3355,10 @@
         if res:
             dia.exec_()
             
-            langFile = self.__e5project.getTranslationPattern()\
+            langFile = (
+                self.__e5project.getTranslationPattern()
                 .replace("%language%", code)
+            )
             self.__e5project.appendFile(langFile)
     
     def updateSelectedCatalogs(self, filenames):

eric ide

mercurial