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 |
10 import os |
|
11 import re |
11 |
12 |
12 from PyQt6.QtCore import pyqtSlot, Qt |
13 from PyQt6.QtCore import pyqtSlot, Qt |
13 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QTreeWidgetItem |
14 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QTreeWidgetItem |
14 |
15 |
15 from EricGui.EricOverrideCursor import EricOverrideCursor |
16 from EricGui.EricOverrideCursor import EricOverrideCursor |
51 self.setupUi(self) |
52 self.setupUi(self) |
52 |
53 |
53 self.__pip = pip |
54 self.__pip = pip |
54 self.__environment = environment |
55 self.__environment = environment |
55 |
56 |
|
57 self.__allFilter = self.tr("<All>") |
|
58 |
56 self.__saveCSVButton = self.buttonBox.addButton( |
59 self.__saveCSVButton = self.buttonBox.addButton( |
57 self.tr("Save as CSV..."), QDialogButtonBox.ButtonRole.ActionRole) |
60 self.tr("Save as CSV..."), QDialogButtonBox.ButtonRole.ActionRole) |
58 self.__saveCSVButton.clicked.connect(self.__saveAsCSV) |
61 self.__saveCSVButton.clicked.connect(self.__saveAsCSV) |
59 |
62 |
60 self.buttonBox.button( |
63 self.buttonBox.button( |
72 )) |
75 )) |
73 else: |
76 else: |
74 # That should never happen; play it safe. |
77 # That should never happen; play it safe. |
75 self.environmentLabel.setText(self.tr("No environment specified.")) |
78 self.environmentLabel.setText(self.tr("No environment specified.")) |
76 |
79 |
|
80 self.licenseFilterComboBox.currentTextChanged.connect( |
|
81 self.__filterPackagesByLicense) |
|
82 |
77 self.__refreshLicenses() |
83 self.__refreshLicenses() |
78 |
84 |
79 @pyqtSlot() |
85 @pyqtSlot() |
80 def __refreshLicenses(self): |
86 def __refreshLicenses(self): |
81 """ |
87 """ |
82 Private slot to refresh the license lists. |
88 Private slot to refresh the license lists. |
83 """ |
89 """ |
84 with EricOverrideCursor(): |
90 with EricOverrideCursor(): |
85 self.licensesList.clear() |
91 self.licensesList.clear() |
86 self.summaryList.clear() |
92 self.summaryList.clear() |
|
93 self.licenseFilterComboBox.clear() |
|
94 |
|
95 licensesForFilter = set() |
87 |
96 |
88 # step 1: show the licenses per package |
97 # step 1: show the licenses per package |
89 self.licensesList.setUpdatesEnabled(False) |
98 self.licensesList.setUpdatesEnabled(False) |
90 licenses = self.__pip.getLicenses( |
99 licenses = self.__pip.getLicenses( |
91 self.__environment, |
100 self.__environment, |
116 for lic in licenses: |
125 for lic in licenses: |
117 QTreeWidgetItem(self.summaryList, [ |
126 QTreeWidgetItem(self.summaryList, [ |
118 "{0:4d}".format(lic["Count"]), |
127 "{0:4d}".format(lic["Count"]), |
119 lic["License"].replace("; ", "\n"), |
128 lic["License"].replace("; ", "\n"), |
120 ]) |
129 ]) |
|
130 licensesForFilter |= set(lic["License"].split("; ")) |
121 |
131 |
122 self.summaryList.sortItems( |
132 self.summaryList.sortItems( |
123 PipLicensesDialog.SummaryLicenseColumn, |
133 PipLicensesDialog.SummaryLicenseColumn, |
124 Qt.SortOrder.AscendingOrder) |
134 Qt.SortOrder.AscendingOrder) |
125 for col in range(self.summaryList.columnCount()): |
135 for col in range(self.summaryList.columnCount()): |
126 self.summaryList.resizeColumnToContents(col) |
136 self.summaryList.resizeColumnToContents(col) |
127 self.summaryList.setUpdatesEnabled(True) |
137 self.summaryList.setUpdatesEnabled(True) |
|
138 |
|
139 self.licenseFilterComboBox.addItems( |
|
140 [self.__allFilter] + sorted(licensesForFilter)) |
128 |
141 |
129 enable = bool(self.licensesList.topLevelItemCount()) |
142 enable = bool(self.licensesList.topLevelItemCount()) |
130 self.__saveCSVButton.setEnabled(enable) |
143 self.__saveCSVButton.setEnabled(enable) |
|
144 |
|
145 @pyqtSlot(str) |
|
146 def __filterPackagesByLicense(self, licenseName): |
|
147 """ |
|
148 Private slot to filter the list of packages by license. |
|
149 |
|
150 @param licenseName license name |
|
151 @type str |
|
152 """ |
|
153 pattern = r"\b{0}".format(re.escape(licenseName)) |
|
154 if not licenseName.endswith((")", "]", "}")): |
|
155 pattern += r"\b" |
|
156 regexp = re.compile(pattern) |
|
157 for row in range(self.licensesList.topLevelItemCount()): |
|
158 itm = self.licensesList.topLevelItem(row) |
|
159 if licenseName == self.__allFilter: |
|
160 itm.setHidden(False) |
|
161 else: |
|
162 itm.setHidden( |
|
163 regexp.search( |
|
164 itm.text(PipLicensesDialog.LicensesLicenseColumn) |
|
165 ) is None |
|
166 ) |
131 |
167 |
132 @pyqtSlot() |
168 @pyqtSlot() |
133 def __saveAsCSV(self): |
169 def __saveAsCSV(self): |
134 """ |
170 """ |
135 Private slot to save the license information as a CSV file. |
171 Private slot to save the license information as a CSV file. |