ProjectFlask/RoutesDialog.py

Sun, 30 May 2021 17:33:37 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 30 May 2021 17:33:37 +0200
branch
eric7
changeset 64
0ee58185b8df
parent 61
fe1e8783a95f
child 66
0d3168d0e310
permissions
-rw-r--r--

Ported the plug-in to PyQt6 for eric7.

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

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

"""
Module implementing a dialog to show the application routes.
"""

from PyQt6.QtCore import pyqtSlot, QProcess
from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QTreeWidgetItem

from EricGui.EricOverrideCursor import EricOverrideCursor, EricOverridenCursor
from EricWidgets import EricMessageBox

from .Ui_RoutesDialog import Ui_RoutesDialog


class RoutesDialog(QDialog, Ui_RoutesDialog):
    """
    Class implementing a dialog to show the application routes.
    """
    def __init__(self, project, parent=None):
        """
        Constructor
        
        @param project reference to the project object
        @type Project
        @param parent reference to the parent widget
        @type QWidget
        """
        super().__init__(parent)
        self.setupUi(self)
        
        self.__refreshButton = self.buttonBox.addButton(
            self.tr("Refresh"), QDialogButtonBox.ButtonRole.ActionRole)
        self.__refreshButton.clicked.connect(self.showRoutes)
        
        self.__project = project
        self.__process = None
    
    def showRoutes(self):
        """
        Public method to show the list of routes.
        
        @return flag indicating success
        @rtype bool
        """
        workdir, env = self.__project.prepareRuntimeEnvironment()
        if env is not None:
            command = self.__project.getFlaskCommand()
            
            self.__process = QProcess()
            self.__process.setProcessEnvironment(env)
            self.__process.setWorkingDirectory(workdir)
            self.__process.setProcessChannelMode(
                QProcess.ProcessChannelMode.MergedChannels)
            
            args = ["routes"]
            if self.matchButton.isChecked():
                sortorder = "match"
            elif self.endpointButton.isChecked():
                sortorder = "endpoint"
            elif self.methodsButton.isChecked():
                sortorder = "methods"
            elif self.ruleButton.isChecked():
                sortorder = "rule"
            else:
                sortorder = ""
            if sortorder:
                args += ["--sort", sortorder]
            if self.allMethodsCheckBox.isChecked():
                args.append("--all-methods")
            
            with EricOverrideCursor():
                self.__process.start(command, args)
                ok = self.__process.waitForStarted(10000)
                if ok:
                    ok = self.__process.waitForFinished(10000)
                    if ok:
                        out = str(self.__process.readAllStandardOutput(),
                                  "utf-8")
                        self.__processOutput(out)
                    else:
                        with EricOverridenCursor():
                            EricMessageBox.critical(
                                None,
                                self.tr("Flask Routes"),
                                self.tr("""The Flask process did not finish"""
                                        """ within 10 seconds."""))
                else:
                    with EricOverridenCursor():
                        EricMessageBox.critical(
                            None,
                            self.tr("Flask Routes"),
                            self.tr("""The Flask process could not be"""
                                    """ started."""))
                for column in range(self.routesList.columnCount()):
                    self.routesList.resizeColumnToContents(column)
            return ok
        else:
            return False
    
    def __processOutput(self, output):
        """
        Private method to process the flask output and populate the routes
        list.
        
        @param output output of the flask process
        @type str
        """
        self.routesList.clear()
        
        lines = output.splitlines()
        widths = []
        for line in lines:
            if not widths:
                continue
            elif line.lstrip().startswith("--"):
                widths = [len(part) for part in line.split()]
                continue
            else:
                parts = []
                for width in widths:
                    parts.append(line[:width].strip())
                    line = line[width:].lstrip()
                    
                QTreeWidgetItem(self.routesList, parts)
    
    @pyqtSlot(bool)
    def on_matchButton_toggled(self, checked):
        """
        Private slot handling the selection of the 'match' sort order.
        
        @param checked state of the button
        @type bool
        """
        if checked:
            self.showRoutes()
    
    @pyqtSlot(bool)
    def on_endpointButton_toggled(self, checked):
        """
        Private slot handling the selection of the 'endpoint' sort order.
        
        @param checked state of the button
        @type bool
        """
        if checked:
            self.showRoutes()
    
    @pyqtSlot(bool)
    def on_methodsButton_toggled(self, checked):
        """
        Private slot handling the selection of the 'methods' sort order.
        
        @param checked state of the button
        @type bool
        """
        if checked:
            self.showRoutes()
    
    @pyqtSlot(bool)
    def on_ruleButton_toggled(self, checked):
        """
        Private slot handling the selection of the 'rule' sort order.
        
        @param checked state of the button
        @type bool
        """
        if checked:
            self.showRoutes()
    
    @pyqtSlot(bool)
    def on_allMethodsCheckBox_toggled(self, checked):
        """
        Private slot handling the selection to show all methods.
        
        @param checked state of the button
        @type bool
        """
        self.showRoutes()

eric ide

mercurial