ProjectDjango/DjangoCheckOptionsDialog.py

Fri, 12 Jul 2024 08:31:14 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 12 Jul 2024 08:31:14 +0200
branch
eric7
changeset 193
bf4c1a7833b4
parent 191
7b03dfe583e3
child 197
2667e16a3379
permissions
-rw-r--r--

Fixed an issue causing the 'New Template' function to fail.

# -*- coding: utf-8 -*-

# Copyright (c) 2016 - 2024 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a dialog to enter the options for a check operation.
"""

import os

from PyQt6.QtCore import QProcess, Qt, pyqtSlot
from PyQt6.QtWidgets import QDialog

from eric7 import Preferences

try:
    from eric7.EricGui import EricPixmapCache
except ImportError:
    from UI import PixmapCache as EricPixmapCache

from eric7.EricWidgets import EricFileDialog

try:
    from eric7.SystemUtilities.FileSystemUtilities import (
        relativeUniversalPath,
        toNativeSeparators,
    )
except ImportError:
    # imports for eric < 23.1
    from eric7.Utilities import relativeUniversalPath, toNativeSeparators

from .Ui_DjangoCheckOptionsDialog import Ui_DjangoCheckOptionsDialog


class DjangoCheckOptionsDialog(QDialog, Ui_DjangoCheckOptionsDialog):
    """
    Class implementing a dialog to enter the options for a check operation.
    """

    def __init__(self, python, path, apps, deployMode, parent=None):
        """
        Constructor

        @param python path of the Python executable
        @type str
        @param path site path to run the manage.py script with
        @type str
        @param apps list of recently used application strings
        @type list of str
        @param deployMode flag indicating to activate the deployment mode
        @type bool
        @param parent reference to the parent widget
        @type QWidget
        """
        super().__init__(parent)
        self.setupUi(self)

        self.settingsFileButton.setIcon(EricPixmapCache.getIcon("open"))

        self.__python = python
        self.__path = path

        self.appsComboBox.addItems([""] + apps)

        self.deployCheckBox.setChecked(deployMode)
        self.on_deployCheckBox_toggled(deployMode)

    @pyqtSlot(bool)
    def on_deployCheckBox_toggled(self, checked):
        """
        Private slot handling a change of the deploy check box.

        @param checked state of the check box
        @type bool
        """
        self.settingsFileGroup.setEnabled(checked)
        self.__populateTagsList(checked)

    @pyqtSlot()
    def on_settingsFileButton_clicked(self):
        """
        Private slot to select a settings file via a file selection dialog.
        """
        path = self.__moduleToPath(self.settingsFileEdit.text())
        if not path:
            path = self.__path
        settingsFile = EricFileDialog.getOpenFileName(
            self, self.tr("Select settings file"), path, self.tr("Python Files (*.py)")
        )

        if settingsFile:
            self.settingsFileEdit.setText(self.__pathToModule(settingsFile))

    def __pathToModule(self, path):
        """
        Private method to convert a file path including a .py extension to a
        module name.

        @param path file path to be converted
        @type str
        @return module name
        @rtype str
        """
        start = self.__path[:-1] if self.__path.endswith(("/", "\\")) else self.__path
        relPath = relativeUniversalPath(path, start)
        mod = os.path.splitext(relPath)[0].replace("/", ".")
        return mod

    def __moduleToPath(self, moduleName):
        """
        Private method to convert a module name to an file path.

        @param moduleName module name to be converted
        @type str
        @return file path
        @rtype str
        """
        if moduleName:
            mod = "{0}.py".format(moduleName.replace(".", "/"))
            if not os.path.isabs(mod):
                mod = os.path.join(self.__path, mod)

            path = toNativeSeparators(mod)
        else:
            path = ""
        return path

    def __populateTagsList(self, deployMode):
        """
        Private slot to populate the tags list.

        @param deployMode flag indicating the deployment mode
        @type bool
        """
        # step 1: save the selected tags
        selectedTags = []
        for itm in self.tagsList.selectedItems():
            selectedTags.append(itm.text())

        # step 2: clear the list
        self.tagsList.clear()

        # step 3: get the available tags and populate the list
        args = []
        args.append("manage.py")
        args.append("check")
        args.append("--list-tags")
        if deployMode:
            args.append("--deploy")

        proc = QProcess()
        if self.__path:
            proc.setWorkingDirectory(self.__path)
        proc.start(self.__python, args)
        if proc.waitForStarted() and proc.waitForFinished():
            output = str(
                proc.readAllStandardOutput(),
                Preferences.getSystem("IOEncoding"),
                "replace",
            )
            for line in output.splitlines():
                self.tagsList.addItem(line.strip())

        # step 4: re-select tags
        for tag in selectedTags:
            items = self.tagsList.findItems(tag, Qt.MatchFlag.MatchCaseSensitive)
            if items:
                items[0].setSelected(True)

    def getData(self):
        """
        Public method to get the options for the check operation.

        @return tuple containing the deployment flag, list of selected tags,
            applications string and the settings file
        @rtype tuple of bool, list of str, str and str
        """
        selectedTags = []
        for itm in self.tagsList.selectedItems():
            selectedTags.append(itm.text())

        return (
            self.deployCheckBox.isChecked(),
            selectedTags,
            self.appsComboBox.currentText(),
            self.settingsFileEdit.text(),
        )

eric ide

mercurial