eric7/PipInterface/PipLicensesDialog.py

branch
eric7
changeset 9014
cdf68c00881d
parent 9003
6bc210cd5726
child 9103
8ac26b4c4316
child 9111
4ac66b6c33a4
--- a/eric7/PipInterface/PipLicensesDialog.py	Sat Apr 02 16:01:33 2022 +0200
+++ b/eric7/PipInterface/PipLicensesDialog.py	Sat Apr 02 17:07:56 2022 +0200
@@ -7,10 +7,13 @@
 Module implementing a dialog to show the licenses of an environment.
 """
 
+import os
+
 from PyQt6.QtCore import pyqtSlot, Qt
-from PyQt6.QtWidgets import QDialog, QTreeWidgetItem
+from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QTreeWidgetItem
 
 from EricGui.EricOverrideCursor import EricOverrideCursor
+from EricWidgets import EricFileDialog, EricMessageBox
 
 from .Ui_PipLicensesDialog import Ui_PipLicensesDialog
 
@@ -50,6 +53,13 @@
         self.__pip = pip
         self.__environment = environment
         
+        self.__saveCSVButton = self.buttonBox.addButton(
+            self.tr("Save as CSV..."), QDialogButtonBox.ButtonRole.ActionRole)
+        self.__saveCSVButton.clicked.connect(self.__saveAsCSV)
+        
+        self.buttonBox.button(
+            QDialogButtonBox.StandardButton.Close).setDefault(True)
+        
         self.localCheckBox.setChecked(localPackages)
         self.userCheckBox.setChecked(usersite)
         
@@ -115,3 +125,53 @@
             for col in range(self.summaryList.columnCount()):
                 self.summaryList.resizeColumnToContents(col)
             self.summaryList.setUpdatesEnabled(True)
+        
+        enable = bool(self.licensesList.topLevelItemCount())
+        self.__saveCSVButton.setEnabled(enable)
+    
+    @pyqtSlot()
+    def __saveAsCSV(self):
+        """
+        Private slot to save the license information as a CSV file.
+        """
+        import csv
+        
+        fileName, selectedFilter = EricFileDialog.getSaveFileNameAndFilter(
+            self,
+            self.tr("Save as CSV"),
+            os.path.expanduser("~"),
+            self.tr("CSV Files (*.csv);;All Files (*)"),
+            None,
+            EricFileDialog.DontConfirmOverwrite
+        )
+        if fileName:
+            ext = os.path.splitext(fileName)[1]
+            if not ext:
+                ex = selectedFilter.split("(*")[1].split(")")[0]
+                if ex:
+                    fileName += ex
+            
+            try:
+                with open(fileName, "w", newline="",
+                          encoding="utf-8") as csvFile:
+                    fieldNames = ["Name", "Version", "License"]
+                    writer = csv.DictWriter(csvFile, fieldnames=fieldNames)
+                    
+                    writer.writeheader()
+                    for row in range(self.licensesList.topLevelItemCount()):
+                        itm = self.licensesList.topLevelItem(row)
+                        writer.writerow({
+                            "Name": itm.text(0),
+                            "Version": itm.text(1),
+                            "License": itm.text(2),
+                        })
+            except OSError as err:
+                EricMessageBox.critical(
+                    self,
+                    self.tr("Save as CSV"),
+                    self.tr(
+                        """<p>The license information could not be saved"""
+                        """ into the CSV file <b>{0}</b>.</p>"""
+                        """<p>Reason: {1}</p>"""
+                    ).format(fileName, str(err))
+                )

eric ide

mercurial