forked from SELinuxProject/setools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new BoolQueryTab implementation.
Signed-off-by: Chris PeBenito <pebenito@ieee.org>
- Loading branch information
Showing
4 changed files
with
159 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# SPDX-License-Identifier: LGPL-2.1-only | ||
|
||
from PyQt6 import QtWidgets | ||
import setools | ||
|
||
from . import criteria, models, tab | ||
|
||
__all__ = ("BoolQueryTab",) | ||
|
||
|
||
class BoolQueryTab(tab.TableResultTabWidget): | ||
|
||
"""A boolean query.""" | ||
|
||
section = tab.AnalysisSection.Components | ||
tab_title = "Booleans" | ||
mlsonly = False | ||
|
||
def __init__(self, policy: setools.SELinuxPolicy, _, /, *, | ||
parent: QtWidgets.QWidget | None = None) -> None: | ||
|
||
super().__init__(setools.BoolQuery(policy), None, enable_criteria=True, | ||
enable_browser=True, parent=parent) | ||
|
||
self.setWhatsThis("<b>Search Booleans in a SELinux policy.</b>") | ||
|
||
# | ||
# Set up criteria widgets | ||
# | ||
name = criteria.BooleanNameCriteriaWidget("Name", self.query, "name", | ||
enable_regex=True, | ||
parent=self.criteria_frame) | ||
name.setToolTip("Search for Booleans by name.") | ||
name.setWhatsThis("<p>Search for Booleans by name.</p>") | ||
|
||
state = criteria.BooleanState("Default State", self.query, "default", | ||
enable_any=True, | ||
parent=self.criteria_frame) | ||
state.setToolTip("Search for Booleans by default state.") | ||
state.setWhatsThis("<p>Search for Booleans by default state.</p>") | ||
|
||
# Add widgets to layout | ||
self.criteria_frame_layout.addWidget(name, 0, 0, 1, 1) | ||
self.criteria_frame_layout.addWidget(state, 0, 1, 1, 1) | ||
self.criteria_frame_layout.addWidget(self.buttonBox, 1, 0, 1, 2) | ||
|
||
# Save widget references | ||
self.criteria = (name, state) | ||
|
||
# Set result table's model | ||
self.table_results_model = models.BooleanTable(self.table_results) | ||
|
||
# | ||
# Set up browser | ||
# | ||
self.browser.setModel(models.BooleanTable(self.browser, | ||
data=sorted(self.query.policy.bools()))) | ||
|
||
|
||
if __name__ == '__main__': | ||
import sys | ||
import warnings | ||
import pprint | ||
import logging | ||
|
||
logging.basicConfig(level=logging.DEBUG, | ||
format='%(asctime)s|%(levelname)s|%(name)s|%(message)s') | ||
warnings.simplefilter("default") | ||
|
||
app = QtWidgets.QApplication(sys.argv) | ||
mw = QtWidgets.QMainWindow() | ||
widget = BoolQueryTab(setools.SELinuxPolicy(), mw) | ||
mw.setCentralWidget(widget) | ||
mw.resize(1280, 1024) | ||
whatsthis = QtWidgets.QWhatsThis.createAction(mw) | ||
mw.menuBar().addAction(whatsthis) # type: ignore[union-attr] | ||
mw.setStatusBar(QtWidgets.QStatusBar(mw)) | ||
mw.show() | ||
rc = app.exec() | ||
pprint.pprint(widget.save()) | ||
sys.exit(rc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# SPDX-License-Identifier: GPL-2.0-only | ||
import typing | ||
|
||
from PyQt6 import QtWidgets | ||
from pytestqt.qtbot import QtBot | ||
|
||
import setools | ||
from setoolsgui.widgets.boolquery import BoolQueryTab | ||
|
||
from .criteria.util import build_mock_policy | ||
|
||
|
||
def test_docs(qtbot: QtBot) -> None: | ||
"""Check that docs are provided for the widget.""" | ||
mock_policy = build_mock_policy() | ||
widget = BoolQueryTab(mock_policy, None) | ||
qtbot.addWidget(widget) | ||
|
||
assert widget.whatsThis() | ||
assert widget.table_results.whatsThis() | ||
assert widget.raw_results.whatsThis() | ||
|
||
for w in widget.criteria: | ||
assert w.toolTip() | ||
assert w.whatsThis() | ||
|
||
results = typing.cast(QtWidgets.QTabWidget, widget.results) | ||
for index in range(results.count()): | ||
assert results.tabWhatsThis(index) | ||
|
||
|
||
def test_layout(qtbot: QtBot) -> None: | ||
"""Test the layout of the criteria frame.""" | ||
mock_policy = build_mock_policy() | ||
widget = BoolQueryTab(mock_policy, None) | ||
qtbot.addWidget(widget) | ||
|
||
name, state = widget.criteria | ||
|
||
assert widget.criteria_frame_layout.columnCount() == 2 | ||
assert widget.criteria_frame_layout.rowCount() == 2 | ||
assert widget.criteria_frame_layout.itemAtPosition(0, 0).widget() == name | ||
assert widget.criteria_frame_layout.itemAtPosition(0, 1).widget() == state | ||
assert widget.criteria_frame_layout.itemAtPosition(1, 0).widget() == widget.buttonBox | ||
assert widget.criteria_frame_layout.itemAtPosition(1, 1).widget() == widget.buttonBox | ||
|
||
|
||
def test_criteria_mapping(qtbot: QtBot) -> None: | ||
"""Test that widgets save to the correct query fields.""" | ||
mock_policy = build_mock_policy() | ||
widget = BoolQueryTab(mock_policy, None) | ||
qtbot.addWidget(widget) | ||
|
||
name, state = widget.criteria | ||
|
||
assert isinstance(widget.query, setools.BoolQuery) | ||
assert name.attrname == "name" | ||
assert state.attrname == "default" |