5 |
5 |
6 """ |
6 """ |
7 Module implementing a dialog to show the licenses of an environment. |
7 Module implementing a dialog to show the licenses of an environment. |
8 """ |
8 """ |
9 |
9 |
|
10 import os |
|
11 |
10 from PyQt6.QtCore import pyqtSlot, Qt |
12 from PyQt6.QtCore import pyqtSlot, Qt |
11 from PyQt6.QtWidgets import QDialog, QTreeWidgetItem |
13 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QTreeWidgetItem |
12 |
14 |
13 from EricGui.EricOverrideCursor import EricOverrideCursor |
15 from EricGui.EricOverrideCursor import EricOverrideCursor |
|
16 from EricWidgets import EricFileDialog, EricMessageBox |
14 |
17 |
15 from .Ui_PipLicensesDialog import Ui_PipLicensesDialog |
18 from .Ui_PipLicensesDialog import Ui_PipLicensesDialog |
16 |
19 |
17 |
20 |
18 class PipLicensesDialog(QDialog, Ui_PipLicensesDialog): |
21 class PipLicensesDialog(QDialog, Ui_PipLicensesDialog): |
47 super().__init__(parent) |
50 super().__init__(parent) |
48 self.setupUi(self) |
51 self.setupUi(self) |
49 |
52 |
50 self.__pip = pip |
53 self.__pip = pip |
51 self.__environment = environment |
54 self.__environment = environment |
|
55 |
|
56 self.__saveCSVButton = self.buttonBox.addButton( |
|
57 self.tr("Save as CSV..."), QDialogButtonBox.ButtonRole.ActionRole) |
|
58 self.__saveCSVButton.clicked.connect(self.__saveAsCSV) |
|
59 |
|
60 self.buttonBox.button( |
|
61 QDialogButtonBox.StandardButton.Close).setDefault(True) |
52 |
62 |
53 self.localCheckBox.setChecked(localPackages) |
63 self.localCheckBox.setChecked(localPackages) |
54 self.userCheckBox.setChecked(usersite) |
64 self.userCheckBox.setChecked(usersite) |
55 |
65 |
56 self.localCheckBox.toggled.connect(self.__refreshLicenses) |
66 self.localCheckBox.toggled.connect(self.__refreshLicenses) |
113 PipLicensesDialog.SummaryLicenseColumn, |
123 PipLicensesDialog.SummaryLicenseColumn, |
114 Qt.SortOrder.AscendingOrder) |
124 Qt.SortOrder.AscendingOrder) |
115 for col in range(self.summaryList.columnCount()): |
125 for col in range(self.summaryList.columnCount()): |
116 self.summaryList.resizeColumnToContents(col) |
126 self.summaryList.resizeColumnToContents(col) |
117 self.summaryList.setUpdatesEnabled(True) |
127 self.summaryList.setUpdatesEnabled(True) |
|
128 |
|
129 enable = bool(self.licensesList.topLevelItemCount()) |
|
130 self.__saveCSVButton.setEnabled(enable) |
|
131 |
|
132 @pyqtSlot() |
|
133 def __saveAsCSV(self): |
|
134 """ |
|
135 Private slot to save the license information as a CSV file. |
|
136 """ |
|
137 import csv |
|
138 |
|
139 fileName, selectedFilter = EricFileDialog.getSaveFileNameAndFilter( |
|
140 self, |
|
141 self.tr("Save as CSV"), |
|
142 os.path.expanduser("~"), |
|
143 self.tr("CSV Files (*.csv);;All Files (*)"), |
|
144 None, |
|
145 EricFileDialog.DontConfirmOverwrite |
|
146 ) |
|
147 if fileName: |
|
148 ext = os.path.splitext(fileName)[1] |
|
149 if not ext: |
|
150 ex = selectedFilter.split("(*")[1].split(")")[0] |
|
151 if ex: |
|
152 fileName += ex |
|
153 |
|
154 try: |
|
155 with open(fileName, "w", newline="", |
|
156 encoding="utf-8") as csvFile: |
|
157 fieldNames = ["Name", "Version", "License"] |
|
158 writer = csv.DictWriter(csvFile, fieldnames=fieldNames) |
|
159 |
|
160 writer.writeheader() |
|
161 for row in range(self.licensesList.topLevelItemCount()): |
|
162 itm = self.licensesList.topLevelItem(row) |
|
163 writer.writerow({ |
|
164 "Name": itm.text(0), |
|
165 "Version": itm.text(1), |
|
166 "License": itm.text(2), |
|
167 }) |
|
168 except OSError as err: |
|
169 EricMessageBox.critical( |
|
170 self, |
|
171 self.tr("Save as CSV"), |
|
172 self.tr( |
|
173 """<p>The license information could not be saved""" |
|
174 """ into the CSV file <b>{0}</b>.</p>""" |
|
175 """<p>Reason: {1}</p>""" |
|
176 ).format(fileName, str(err)) |
|
177 ) |