Skip to content

Commit 00968e8

Browse files
authored
Merge pull request #162 from pebenito/better-lib-repr
Improve repr of library classes.
2 parents e9422ff + 2d64ca7 commit 00968e8

43 files changed

Lines changed: 236 additions & 11 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

setools/boolquery.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ def default(self, value) -> None:
3838
else:
3939
self._default = bool(value)
4040

41+
def _build_repr_args(self) -> list[str]:
42+
return [f"default={self.default!r}"] + self._build_name_repr_args()
43+
4144
def results(self) -> Iterable[policyrep.Boolean]:
4245
"""Generator which yields all Booleans matching the criteria."""
4346
self.log.info(f"Generating Boolean results from {self.policy}")

setools/boundsquery.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class BoundsQuery(query.PolicyQuery):
2929
child = CriteriaDescriptor[policyrep.Type]("child_regex")
3030
child_regex: bool = False
3131

32+
def _build_repr_args(self) -> list[str]:
33+
return [f"ruletype={self.ruletype!r}", f"parent={self.parent!r}",
34+
f"parent_regex={self.parent_regex!r}", f"child={self.child!r}",
35+
f"child_regex={self.child_regex!r}"]
36+
3237
def results(self) -> Iterable[policyrep.Bounds]:
3338
"""Generator which yields all matching *bounds statements."""
3439
self.log.info(f"Generating bounds results from {self.policy}")

setools/categoryquery.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class CategoryQuery(mixins.MatchAlias, mixins.MatchName, query.PolicyQuery):
2727
will be used on the alias names.
2828
"""
2929

30+
def _build_repr_args(self) -> list[str]:
31+
return self._build_name_repr_args() + self._build_alias_repr_args()
32+
3033
def results(self) -> Iterable[policyrep.Category]:
3134
"""Generator which yields all matching categories."""
3235
self.log.info(f"Generating category results from {self.policy}")

setools/checker/assertrbac.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ def __init__(self, policy: policyrep.SELinuxPolicy, checkname: str,
6363
self.log.info("Overlap in expect_target and exempt_target: "
6464
f"{', '.join(i.name for i in target_exempt_expect_overlap)}")
6565

66+
def __repr__(self) -> str:
67+
return (f"{self.__class__.__name__}(source={self.source}, target={self.target}, "
68+
f"exempt_source={self.exempt_source}, exempt_target={self.exempt_target}, "
69+
f"expect_source={self.expect_source}, expect_target={self.expect_target})")
70+
6671
def run(self) -> list[policyrep.AnyRBACRule | str]:
6772
assert any((self.source, self.target)), "AssertRBAC no options set, this is a bug."
6873

setools/checker/assertte.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ def __init__(self, policy: policyrep.SELinuxPolicy, checkname: str,
7777
self.log.info("Overlap in expect_target and exempt_target: "
7878
f"{', '.join(i.name for i in target_exempt_expect_overlap)}")
7979

80+
def __repr__(self) -> str:
81+
return (f"{self.__class__.__name__}(source={self.source}, target={self.target}, "
82+
f"tclass={self.tclass}, perms={self.perms}, "
83+
f"exempt_source={self.exempt_source}, exempt_target={self.exempt_target}, "
84+
f"expect_source={self.expect_source}, expect_target={self.expect_target})")
85+
8086
def run(self) -> list[policyrep.AnyTERule | str]:
8187
assert any((self.source, self.target, self.tclass, self.perms)), \
8288
"AssertTe no options set, this is a bug."

setools/checker/checker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ def config(self, configpath: str) -> None:
7575
self.checks = checks
7676
self._config = config
7777

78+
def __repr__(self) -> str:
79+
return f"<{self.__class__.__name__}({self.policy!r}, {self.config!r})>"
80+
7881
def run(self, output: typing.TextIO = sys.stdout) -> int:
7982
"""Run all configured checks and print report to the file-like output."""
8083
failures = 0

setools/checker/emptyattr.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ def missing_ok(self, value) -> None:
7373
else:
7474
self._pass_by_missing = False
7575

76+
def __repr__(self) -> str:
77+
return (f"{self.__class__.__name__}(attr={self.attr}, missing_ok={self.missing_ok})")
78+
7679
def run(self) -> list[policyrep.Type]:
7780
self.log.info(f"Checking type attribute {self.attr} is empty.")
7881

setools/checker/roexec.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def __init__(self, policy: policyrep.SELinuxPolicy, checkname: str,
4141
self.exempt_file = config.get(EXEMPT_FILE)
4242
self.exempt_exec_domain = config.get(EXEMPT_EXEC)
4343

44+
def __repr__(self) -> str:
45+
return (f"{self.__class__.__name__}(exempt_write_domain={self.exempt_write_domain}, "
46+
f"exempt_exec_domain={self.exempt_exec_domain}, exempt_file={self.exempt_file})")
47+
4448
def _collect_executables(self) -> defaultdict[policyrep.Type, set[policyrep.AVRule]]:
4549
self.log.debug("Collecting list of executable file types.")
4650
self.log.debug(f"{self.exempt_exec_domain=}")

setools/checker/rokmod.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def __init__(self, policy: policyrep.SELinuxPolicy, checkname: str,
4141
self.exempt_file = config.get(EXEMPT_FILE)
4242
self.exempt_load_domain = config.get(EXEMPT_LOAD)
4343

44+
def __repr__(self) -> str:
45+
return (f"{self.__class__.__name__}(exempt_write_domain={self.exempt_write_domain}, "
46+
f"exempt_load_domain={self.exempt_load_domain}, exempt_file={self.exempt_file})")
47+
4448
def _collect_kernel_mods(self) -> defaultdict[policyrep.Type, set[policyrep.AVRule]]:
4549
self.log.debug("Collecting list of kernel module types.")
4650
self.log.debug(f"{self.exempt_load_domain=}")

setools/commonquery.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class CommonQuery(mixins.MatchPermission, mixins.MatchName, query.PolicyQuery):
3131
on the permission names instead of set logic.
3232
"""
3333

34+
def _build_repr_args(self) -> list[str]:
35+
return self._build_name_repr_args() + self._build_perms_repr_args()
36+
3437
def results(self) -> Iterable[policyrep.Common]:
3538
"""Generator which yields all matching commons."""
3639
self.log.info(f"Generating common results from {self.policy}")

0 commit comments

Comments
 (0)