--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/VultureChecker/EditWhiteListDialog.py Tue Oct 06 19:29:52 2015 +0200 @@ -0,0 +1,196 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing the whitelist edit dialog. +""" + +from __future__ import unicode_literals + +from PyQt5.QtCore import pyqtSlot +from PyQt5.QtWidgets import QDialog, QInputDialog, QLineEdit + +from .Ui_EditWhiteListDialog import Ui_EditWhiteListDialog + + +class EditWhiteListDialog(QDialog, Ui_EditWhiteListDialog): + """ + Class implementing the whitelist edit dialog. + """ + def __init__(self, whitelists, parent=None): + """ + Constructor + + @param whitelists dictionary containing the whitelists + @type dict of list of str + @param parent reference to the parent widget + @type QWidget + """ + super(EditWhiteListDialog, self).__init__(parent) + self.setupUi(self) + + self.__lists = [ + self.classesList, + self.functionsList, + self.attributesList, + self.variablesList, + self.propertiesList, + self.patternsList, + ] + + self.classesList.addItems(whitelists["class"]) + self.functionsList.addItems(whitelists["function"]) + self.attributesList.addItems(whitelists["attribute"]) + self.variablesList.addItems(whitelists["variable"]) + self.propertiesList.addItems(whitelists["property"]) + self.patternsList.addItems(whitelists["__patterns__"]) + + self.listsWidget.setCurrentIndex(self.listsWidget.count() - 1) + + @pyqtSlot() + def on_patternsList_itemSelectionChanged(self): + """ + Private slot to react upon a change of selection in the patterns list. + """ + self.__setButtonEnabledStates() + + @pyqtSlot() + def on_propertiesList_itemSelectionChanged(self): + """ + Private slot to react upon a change of selection in the properties + list. + """ + self.__setButtonEnabledStates() + + @pyqtSlot() + def on_variablesList_itemSelectionChanged(self): + """ + Private slot to react upon a change of selection in the variables list. + """ + self.__setButtonEnabledStates() + + @pyqtSlot() + def on_attributesList_itemSelectionChanged(self): + """ + Private slot to react upon a change of selection in the attributes + list. + """ + self.__setButtonEnabledStates() + + @pyqtSlot() + def on_functionsList_itemSelectionChanged(self): + """ + Private slot to react upon a change of selection in the functions list. + """ + self.__setButtonEnabledStates() + + @pyqtSlot() + def on_classesList_itemSelectionChanged(self): + """ + Private slot to react upon a change of selection in the classes list. + """ + self.__setButtonEnabledStates() + + def __isPattern(self, name): + """ + Private method to check, if a name is a wildcard pattern. + + @param name name to be checked + @type str + @return flag indicating a wildcard pattern + @rtype bool + """ + isPattern = False + for char in "*?[": + if char in name: + isPattern = True + break + return isPattern + + @pyqtSlot() + def on_addButton_clicked(self): + """ + Private slot to add an entry to the current list. + """ + name, ok = QInputDialog.getText( + self, + self.tr("Add Whitelist"), + self.tr("Enter a name or wildcard pattern to the current" + " whitelist:"), + QLineEdit.Normal) + if ok and bool(name): + curr = self.__lists[self.listsWidget.currentIndex()] + if curr is self.patternsList or self.__isPattern(name): + self.patternsList.addItem(name) + else: + curr.addItem(name) + + @pyqtSlot() + def on_removeButton_clicked(self): + """ + Private slot to remove the selected entries from the current list. + """ + curr = self.__lists[self.listsWidget.currentIndex()] + for itm in curr.selectedItems(): + row = curr.row(itm) + curr.takeItem(row) + del itm + + @pyqtSlot() + def on_removeAllButton_clicked(self): + """ + Private slot to remove all entries from the current list. + """ + curr = self.__lists[self.listsWidget.currentIndex()] + curr.clear() + + @pyqtSlot(int) + def on_listsWidget_currentChanged(self, index): + """ + Private slot handling the selection of tab. + + @param index index of the selected tab + @type int + """ + self.__setButtonEnabledStates() + + def __setButtonEnabledStates(self): + """ + Private slot to set the state of various buttons. + """ + curr = self.__lists[self.listsWidget.currentIndex()] + self.removeButton.setEnabled(len(curr.selectedItems()) > 0) + self.removeAllButton.setEnabled(curr.count() > 0) + + def __getWhiteList(self, listWidget): + """ + Private method to get the whitelisted names from a list widget. + + @param listWidget reference to the list widget + @type QListWidget + @return whitelisted names + @rtype list of str + """ + whitelist = [] + for row in range(listWidget.count()): + whitelist.append(listWidget.item(row).text()) + return whitelist + + def getWhiteLists(self): + """ + Public methods to retrieve the various whitelists. + + @return dictionary containing the whitelists + @rtype dict of list of str + """ + whitelists = { + "class": self.__getWhiteList(self.classesList), + "function": self.__getWhiteList(self.functionsList), + "attribute": self.__getWhiteList(self.attributesList), + "variable": self.__getWhiteList(self.variablesList), + "property": self.__getWhiteList(self.propertiesList), + "__patterns__": self.__getWhiteList(self.patternsList), + } + return whitelists