Skip to content

Commit

Permalink
DiffResultDescriptor: Use the diff function directly.
Browse files Browse the repository at this point in the history
Don't indirectly specify the function by name.

Signed-off-by: Chris PeBenito <[email protected]>
  • Loading branch information
pebenito committed Apr 2, 2024
1 parent 681f6b4 commit b8a281c
Show file tree
Hide file tree
Showing 25 changed files with 165 additions and 162 deletions.
8 changes: 4 additions & 4 deletions setools/diff/bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ class BooleansDifference(Difference):

"""Determine the difference in type attributes between two policies."""

added_booleans = DiffResultDescriptor[Boolean]("diff_booleans")
removed_booleans = DiffResultDescriptor[Boolean]("diff_booleans")
modified_booleans = DiffResultDescriptor[ModifiedBoolean]("diff_booleans")

def diff_booleans(self) -> None:
"""Generate the difference in type attributes between the policies."""

Expand All @@ -70,6 +66,10 @@ def diff_booleans(self) -> None:
right_boolean.state,
left_boolean.state))

added_booleans = DiffResultDescriptor[Boolean](diff_booleans)
removed_booleans = DiffResultDescriptor[Boolean](diff_booleans)
modified_booleans = DiffResultDescriptor[ModifiedBoolean](diff_booleans)

#
# Internal functions
#
Expand Down
16 changes: 8 additions & 8 deletions setools/diff/bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ class BoundsDifference(Difference):

"""Determine the difference in *bounds between two policies."""

added_typebounds = DiffResultDescriptor[Bounds]("diff_typebounds")
removed_typebounds = DiffResultDescriptor[Bounds]("diff_typebounds")
modified_typebounds = DiffResultDescriptor[ModifiedBounds]("diff_typebounds")

# Lists of rules for each policy
_left_typebounds: list[Bounds] | None = None
_right_typebounds: list[Bounds] | None = None

def diff_typebounds(self) -> None:
"""Generate the difference in typebound rules between the policies."""

Expand All @@ -56,6 +48,14 @@ def diff_typebounds(self) -> None:
self.modified_typebounds.append(ModifiedBounds(
left_bound, right_bound.parent, left_bound.parent))

added_typebounds = DiffResultDescriptor[Bounds](diff_typebounds)
removed_typebounds = DiffResultDescriptor[Bounds](diff_typebounds)
modified_typebounds = DiffResultDescriptor[ModifiedBounds](diff_typebounds)

# Lists of rules for each policy
_left_typebounds: list[Bounds] | None = None
_right_typebounds: list[Bounds] | None = None

#
# Internal functions
#
Expand Down
8 changes: 4 additions & 4 deletions setools/diff/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ class CommonDifference(Difference):
between two policies.
"""

added_commons = DiffResultDescriptor[policyrep.Common]("diff_commons")
removed_commons = DiffResultDescriptor[policyrep.Common]("diff_commons")
modified_commons = DiffResultDescriptor[ModifiedCommon]("diff_commons")

def diff_commons(self) -> None:
"""Generate the difference in commons between the policies."""

Expand All @@ -56,6 +52,10 @@ def diff_commons(self) -> None:
removed_perms,
matched_perms))

added_commons = DiffResultDescriptor[policyrep.Common](diff_commons)
removed_commons = DiffResultDescriptor[policyrep.Common](diff_commons)
modified_commons = DiffResultDescriptor[ModifiedCommon](diff_commons)

#
# Internal functions
#
Expand Down
33 changes: 16 additions & 17 deletions setools/diff/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,6 @@ class ConstraintsDifference(Difference):
be reflected as a difference.
"""

added_constrains = DiffResultDescriptor[policyrep.Constraint]("diff_constrains")
removed_constrains = DiffResultDescriptor[policyrep.Constraint]("diff_constrains")

added_mlsconstrains = DiffResultDescriptor[policyrep.Constraint]("diff_mlsconstrains")
removed_mlsconstrains = DiffResultDescriptor[policyrep.Constraint]("diff_mlsconstrains")

added_validatetrans = DiffResultDescriptor[policyrep.Validatetrans]("diff_validatetrans")
removed_validatetrans = DiffResultDescriptor[policyrep.Validatetrans]("diff_validatetrans")

added_mlsvalidatetrans = DiffResultDescriptor[policyrep.Validatetrans]("diff_mlsvalidatetrans")
removed_mlsvalidatetrans = DiffResultDescriptor[policyrep.Validatetrans](
"diff_mlsvalidatetrans")

# Lists of rules for each policy
_left_constraints: RuleList[policyrep.ConstraintRuletype, policyrep.AnyConstraint] = None
_right_constraints: RuleList[policyrep.ConstraintRuletype, policyrep.AnyConstraint] = None

def diff_constrains(self) -> None:
"""Generate the difference in constraint rules between the policies."""

Expand Down Expand Up @@ -121,6 +104,22 @@ def diff_mlsvalidatetrans(self) -> None:
(ConstraintWrapper(c) for c in self._right_constraints[
policyrep.ConstraintRuletype.mlsvalidatetrans]))

added_constrains = DiffResultDescriptor[policyrep.Constraint](diff_constrains)
removed_constrains = DiffResultDescriptor[policyrep.Constraint](diff_constrains)

added_mlsconstrains = DiffResultDescriptor[policyrep.Constraint](diff_mlsconstrains)
removed_mlsconstrains = DiffResultDescriptor[policyrep.Constraint](diff_mlsconstrains)

added_validatetrans = DiffResultDescriptor[policyrep.Validatetrans](diff_validatetrans)
removed_validatetrans = DiffResultDescriptor[policyrep.Validatetrans](diff_validatetrans)

added_mlsvalidatetrans = DiffResultDescriptor[policyrep.Validatetrans](diff_mlsvalidatetrans)
removed_mlsvalidatetrans = DiffResultDescriptor[policyrep.Validatetrans](diff_mlsvalidatetrans)

# Lists of rules for each policy
_left_constraints: RuleList[policyrep.ConstraintRuletype, policyrep.AnyConstraint] = None
_right_constraints: RuleList[policyrep.ConstraintRuletype, policyrep.AnyConstraint] = None

#
# Internal functions
#
Expand Down
8 changes: 4 additions & 4 deletions setools/diff/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ class DefaultsDifference(Difference):

"""Determine the difference in default_* between two policies."""

added_defaults = DiffResultDescriptor[AnyDefault]("diff_defaults")
removed_defaults = DiffResultDescriptor[AnyDefault]("diff_defaults")
modified_defaults = DiffResultDescriptor[ModifiedDefault]("diff_defaults")

def diff_defaults(self) -> None:
"""Generate the difference in type defaults between the policies."""

Expand Down Expand Up @@ -73,6 +69,10 @@ def diff_defaults(self) -> None:
added_default_range,
removed_default_range))

added_defaults = DiffResultDescriptor[AnyDefault](diff_defaults)
removed_defaults = DiffResultDescriptor[AnyDefault](diff_defaults)
modified_defaults = DiffResultDescriptor[ModifiedDefault](diff_defaults)

#
# Internal functions
#
Expand Down
10 changes: 7 additions & 3 deletions setools/diff/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: LGPL-2.1-only
#

from collections.abc import Callable
import typing

T = typing.TypeVar("T")
Expand All @@ -15,7 +16,7 @@ class DiffResultDescriptor(typing.Generic[T]):
# @properties could be used instead, but there are so
# many result attributes, this will keep the code cleaner.

def __init__(self, diff_function: str) -> None:
def __init__(self, diff_function: Callable[[typing.Any], None]) -> None:
self.diff_function = diff_function
self.name: str

Expand All @@ -27,8 +28,11 @@ def __get__(self, obj, objtype=None) -> list[T]:
raise AttributeError

if getattr(obj, self.name, None) is None:
diff = getattr(obj, self.diff_function)
diff()
# must specify obj here since the function is not bound to a
# a particular instance. Otherwise you get:
# TypeError: av_diff_template.<locals>.diff() missing 1 required
# positional argument: 'self'
self.diff_function(obj)

return getattr(obj, self.name)

Expand Down
8 changes: 4 additions & 4 deletions setools/diff/fsuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class FSUsesDifference(Difference):

"""Determine the difference in fs_use_* rules between two policies."""

added_fs_uses = DiffResultDescriptor[FSUse]("diff_fs_uses")
removed_fs_uses = DiffResultDescriptor[FSUse]("diff_fs_uses")
modified_fs_uses = DiffResultDescriptor[ModifiedFSUse]("diff_fs_uses")

def diff_fs_uses(self) -> None:
"""Generate the difference in fs_use rules between the policies."""

Expand All @@ -49,6 +45,10 @@ def diff_fs_uses(self) -> None:
right_rule.context,
left_rule.context))

added_fs_uses = DiffResultDescriptor[FSUse](diff_fs_uses)
removed_fs_uses = DiffResultDescriptor[FSUse](diff_fs_uses)
modified_fs_uses = DiffResultDescriptor[ModifiedFSUse](diff_fs_uses)

#
# Internal functions
#
Expand Down
8 changes: 4 additions & 4 deletions setools/diff/genfscon.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class GenfsconsDifference(Difference):

"""Determine the difference in genfscon rules between two policies."""

added_genfscons = DiffResultDescriptor[Genfscon]("diff_genfscons")
removed_genfscons = DiffResultDescriptor[Genfscon]("diff_genfscons")
modified_genfscons = DiffResultDescriptor[ModifiedGenfscon]("diff_genfscons")

def diff_genfscons(self) -> None:
"""Generate the difference in genfscon rules between the policies."""

Expand All @@ -49,6 +45,10 @@ def diff_genfscons(self) -> None:
right_rule.context,
left_rule.context))

added_genfscons = DiffResultDescriptor[Genfscon](diff_genfscons)
removed_genfscons = DiffResultDescriptor[Genfscon](diff_genfscons)
modified_genfscons = DiffResultDescriptor[ModifiedGenfscon](diff_genfscons)

#
# Internal functions
#
Expand Down
8 changes: 4 additions & 4 deletions setools/diff/ibendportcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class IbendportconsDifference(Difference):

"""Determine the difference in ibendportcons between two policies."""

added_ibendportcons = DiffResultDescriptor[Ibendportcon]("diff_ibendportcons")
removed_ibendportcons = DiffResultDescriptor[Ibendportcon]("diff_ibendportcons")
modified_ibendportcons = DiffResultDescriptor[ModifiedIbendportcon]("diff_ibendportcons")

def diff_ibendportcons(self) -> None:
"""Generate the difference in ibendportcons between the policies."""

Expand All @@ -49,6 +45,10 @@ def diff_ibendportcons(self) -> None:
self.modified_ibendportcons.append(
ModifiedIbendportcon(left_ibep, right_ibep.context, left_ibep.context))

added_ibendportcons = DiffResultDescriptor[Ibendportcon](diff_ibendportcons)
removed_ibendportcons = DiffResultDescriptor[Ibendportcon](diff_ibendportcons)
modified_ibendportcons = DiffResultDescriptor[ModifiedIbendportcon](diff_ibendportcons)

#
# Internal functions
#
Expand Down
8 changes: 4 additions & 4 deletions setools/diff/ibpkeycon.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class IbpkeyconsDifference(Difference):

"""Determine the difference in ibpkeycons between two policies."""

added_ibpkeycons = DiffResultDescriptor[Ibpkeycon]("diff_ibpkeycons")
removed_ibpkeycons = DiffResultDescriptor[Ibpkeycon]("diff_ibpkeycons")
modified_ibpkeycons = DiffResultDescriptor[ModifiedIbpkeycon]("diff_ibpkeycons")

def diff_ibpkeycons(self) -> None:
"""Generate the difference in ibpkeycons between the policies."""

Expand All @@ -49,6 +45,10 @@ def diff_ibpkeycons(self) -> None:
self.modified_ibpkeycons.append(
ModifiedIbpkeycon(left_ibpkey, right_ibpkey.context, left_ibpkey.context))

added_ibpkeycons = DiffResultDescriptor[Ibpkeycon](diff_ibpkeycons)
removed_ibpkeycons = DiffResultDescriptor[Ibpkeycon](diff_ibpkeycons)
modified_ibpkeycons = DiffResultDescriptor[ModifiedIbpkeycon](diff_ibpkeycons)

#
# Internal functions
#
Expand Down
8 changes: 4 additions & 4 deletions setools/diff/initsid.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class InitialSIDsDifference(Difference):

"""Determine the difference in initsids between two policies."""

added_initialsids = DiffResultDescriptor[policyrep.InitialSID]("diff_initialsids")
removed_initialsids = DiffResultDescriptor[policyrep.InitialSID]("diff_initialsids")
modified_initialsids = DiffResultDescriptor[ModifiedInitialSID]("diff_initialsids")

def diff_initialsids(self) -> None:
"""Generate the difference in initial SIDs between the policies."""

Expand All @@ -48,6 +44,10 @@ def diff_initialsids(self) -> None:
self.modified_initialsids.append(ModifiedInitialSID(
left_initialsid, right_initialsid.context, left_initialsid.context))

added_initialsids = DiffResultDescriptor[policyrep.InitialSID](diff_initialsids)
removed_initialsids = DiffResultDescriptor[policyrep.InitialSID](diff_initialsids)
modified_initialsids = DiffResultDescriptor[ModifiedInitialSID](diff_initialsids)

#
# Internal functions
#
Expand Down
24 changes: 12 additions & 12 deletions setools/diff/mls.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ class CategoriesDifference(Difference):

"""Determine the difference in categories between two policies."""

added_categories = DiffResultDescriptor[Category]("diff_categories")
removed_categories = DiffResultDescriptor[Category]("diff_categories")
modified_categories = DiffResultDescriptor[ModifiedCategory]("diff_categories")

def diff_categories(self) -> None:
"""Generate the difference in categories between the policies."""

Expand All @@ -111,6 +107,10 @@ def diff_categories(self) -> None:
removed_aliases,
matched_aliases))

added_categories = DiffResultDescriptor[Category](diff_categories)
removed_categories = DiffResultDescriptor[Category](diff_categories)
modified_categories = DiffResultDescriptor[ModifiedCategory](diff_categories)

#
# Internal functions
#
Expand All @@ -126,10 +126,6 @@ class SensitivitiesDifference(Difference):

"""Determine the difference in sensitivities between two policies."""

added_sensitivities = DiffResultDescriptor[Sensitivity]("diff_sensitivities")
removed_sensitivities = DiffResultDescriptor[Sensitivity]("diff_sensitivities")
modified_sensitivities = DiffResultDescriptor[ModifiedSensitivity]("diff_sensitivities")

def diff_sensitivities(self) -> None:
"""Generate the difference in sensitivities between the policies."""

Expand All @@ -155,6 +151,10 @@ def diff_sensitivities(self) -> None:
removed_aliases,
matched_aliases))

added_sensitivities = DiffResultDescriptor[Sensitivity](diff_sensitivities)
removed_sensitivities = DiffResultDescriptor[Sensitivity](diff_sensitivities)
modified_sensitivities = DiffResultDescriptor[ModifiedSensitivity](diff_sensitivities)

#
# Internal functions
#
Expand All @@ -170,10 +170,6 @@ class LevelDeclsDifference(Difference):

"""Determine the difference in levels between two policies."""

added_levels = DiffResultDescriptor[LevelDecl]("diff_levels")
removed_levels = DiffResultDescriptor[LevelDecl]("diff_levels")
modified_levels = DiffResultDescriptor[ModifiedLevelDecl]("diff_levels")

def diff_levels(self) -> None:
"""Generate the difference in levels between the policies."""

Expand All @@ -198,6 +194,10 @@ def diff_levels(self) -> None:
self.modified_levels.append(ModifiedLevelDecl(
left_level, added_categories, removed_categories, matched_categories))

added_levels = DiffResultDescriptor[LevelDecl](diff_levels)
removed_levels = DiffResultDescriptor[LevelDecl](diff_levels)
modified_levels = DiffResultDescriptor[ModifiedLevelDecl](diff_levels)

#
# Internal functions
#
Expand Down
16 changes: 8 additions & 8 deletions setools/diff/mlsrules.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ class MLSRulesDifference(Difference):

"""Determine the difference in MLS rules between two policies."""

added_range_transitions = DiffResultDescriptor[MLSRule]("diff_range_transitions")
removed_range_transitions = DiffResultDescriptor[MLSRule]("diff_range_transitions")
modified_range_transitions = DiffResultDescriptor[ModifiedMLSRule]("diff_range_transitions")

# Lists of rules for each policy
_left_mls_rules: RuleList[MLSRuletype, MLSRule] = None
_right_mls_rules: RuleList[MLSRuletype, MLSRule] = None

def diff_range_transitions(self) -> None:
"""Generate the difference in range_transition rules between the policies."""

Expand Down Expand Up @@ -71,6 +63,14 @@ def diff_range_transitions(self) -> None:
self.removed_range_transitions = removed
self.modified_range_transitions = modified

added_range_transitions = DiffResultDescriptor[MLSRule](diff_range_transitions)
removed_range_transitions = DiffResultDescriptor[MLSRule](diff_range_transitions)
modified_range_transitions = DiffResultDescriptor[ModifiedMLSRule](diff_range_transitions)

# Lists of rules for each policy
_left_mls_rules: RuleList[MLSRuletype, MLSRule] = None
_right_mls_rules: RuleList[MLSRuletype, MLSRule] = None

#
# Internal functions
#
Expand Down
Loading

0 comments on commit b8a281c

Please sign in to comment.