mirror of
https://github.com/SELinuxProject/setools
synced 2025-05-03 16:49:40 +00:00
Most come from the union-attr check, throwing errors because PyQt returns, for example, "QObjecet | None", so code is using attributes on the PyQt object that don't exist on None. In some cases, classes gained new overridden method implementations that do type narrowing to ensure a non-None object is returned. This also includes a new QListView subclass with the above overrides. With the new class, there is some light refactoring in ListCriteriaWidget, moving the selection methods to the new class. The test code (unit tests and module __main__) simply ignore union-attr errors, since we want that kind of runtime error to pop loudly. There are some remaining issues, but they seem to be issues in the PyQt5 typing. Signed-off-by: Chris PeBenito <pebenito@ieee.org>
85 lines
2.9 KiB
Python
85 lines
2.9 KiB
Python
# SPDX-License-Identifier: LGPL-2.1-only
|
|
|
|
from PyQt5 import QtWidgets
|
|
|
|
from .. import models
|
|
from .list import ListCriteriaWidget
|
|
from .name import NameCriteriaWidget
|
|
|
|
# Regex for exact matches to types/attrs
|
|
VALIDATE_EXACT = r"[A-Za-z0-9._-]*"
|
|
|
|
__all__ = ("BooleanListCriteriaWidget", "BooleanNameCriteriaWidget",)
|
|
|
|
|
|
class BooleanListCriteriaWidget(ListCriteriaWidget):
|
|
|
|
"""A widget providing a QListView widget for selecting zero or more Booleans."""
|
|
|
|
def __init__(self, title: str, query, attrname: str, enable_equal: bool = True,
|
|
parent: QtWidgets.QWidget | None = None) -> None:
|
|
|
|
model = models.BooleanTable(data=sorted(query.policy.bools()))
|
|
|
|
super().__init__(title, query, attrname, model, enable_equal=enable_equal, parent=parent)
|
|
|
|
self.criteria_any.setToolTip("Any selected Boolean will match.")
|
|
self.criteria_any.setWhatsThis("<b>Any selected Boolean will match.</b>")
|
|
|
|
if enable_equal:
|
|
self.criteria_equal.setToolTip("The selected Booleans must exactly match.")
|
|
self.criteria_equal.setWhatsThis("<b>The selected Booleans must exactly match.</b>")
|
|
|
|
|
|
class BooleanNameCriteriaWidget(NameCriteriaWidget):
|
|
|
|
"""
|
|
Widget providing a QLineEdit for the user to enter a Boolean name, with
|
|
the criteria saved to the attributes of the specified query.
|
|
"""
|
|
|
|
def __init__(self, title: str, query, attrname: str,
|
|
parent: QtWidgets.QWidget | None = None,
|
|
enable_regex: bool = True, required: bool = False):
|
|
|
|
completion: list[str] = sorted(b.name for b in query.policy.bools())
|
|
|
|
super().__init__(title, query, attrname, completion, VALIDATE_EXACT,
|
|
enable_regex=enable_regex, required=required, parent=parent)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
import sys
|
|
import logging
|
|
import warnings
|
|
import setools
|
|
import pprint
|
|
|
|
logging.basicConfig(level=logging.DEBUG,
|
|
format='%(asctime)s|%(levelname)s|%(name)s|%(message)s')
|
|
warnings.simplefilter("default")
|
|
|
|
p = setools.SELinuxPolicy()
|
|
q1 = setools.TERuleQuery(p)
|
|
q2 = setools.BoolQuery(p)
|
|
|
|
app = QtWidgets.QApplication(sys.argv)
|
|
mw = QtWidgets.QMainWindow()
|
|
window = QtWidgets.QWidget(mw)
|
|
layout = QtWidgets.QHBoxLayout(window)
|
|
widget1 = BooleanListCriteriaWidget("Test Booleans list", q1, "boolean", window)
|
|
widget2 = BooleanNameCriteriaWidget("Test Booleans linedit", q2, "name", window)
|
|
layout.addWidget(widget1)
|
|
layout.addWidget(widget2)
|
|
window.setToolTip("test tooltip")
|
|
window.setWhatsThis("test whats this")
|
|
mw.setCentralWidget(window)
|
|
mw.resize(window.size())
|
|
whatsthis = QtWidgets.QWhatsThis.createAction(mw)
|
|
mw.menuBar().addAction(whatsthis) # type: ignore[union-attr]
|
|
mw.show()
|
|
rc = app.exec_()
|
|
print("Query settings:")
|
|
pprint.pprint(q1.boolean)
|
|
sys.exit(rc)
|