From 3cbb8ec07d7ed00f91b735fbf5aac8e639bbf917 Mon Sep 17 00:00:00 2001 From: cecinestpasunepipe <110607403+cecinestpasunepipe@users.noreply.github.com> Date: Tue, 14 Nov 2023 16:15:41 +0100 Subject: [PATCH] Implement suggestions --- dissect/util/feature.py | 25 ++++++++----------------- tests/test_feature.py | 19 ++++--------------- 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/dissect/util/feature.py b/dissect/util/feature.py index 153f8a7..ddc645a 100644 --- a/dissect/util/feature.py +++ b/dissect/util/feature.py @@ -6,16 +6,13 @@ # Register feature flags in a central place to avoid chaos class Feature(Enum): - NOVICE = "novice" + ADVANCED = "advanced" LATEST = "latest" BETA = "beta" -# This set defines the valid flags -DISSECT_FEATURE_SET = set(item for item in Feature) - # Defines the default flags (as strings) -DISSECT_FEATURES_DEFAULT = "novice/latest" +DISSECT_FEATURES_DEFAULT = "latest" # Defines the environment variable to read the flags from DISSECT_FEATURES_ENV = "DISSECT_FEATURES" @@ -25,16 +22,9 @@ class FeatureException(RuntimeError): pass -def check_flags(flags: list[Feature]) -> list[Feature]: - for flag in flags: - if flag not in DISSECT_FEATURE_SET: - raise FeatureException(f"Invalid feature flag: {flag} choose from: {DISSECT_FEATURE_SET}") - return flags - - @functools.cache def feature_flags() -> list[Feature]: - return check_flags([Feature(name) for name in os.getenv(DISSECT_FEATURES_ENV, DISSECT_FEATURES_DEFAULT).split("/")]) + return [Feature(name) for name in os.getenv(DISSECT_FEATURES_ENV, DISSECT_FEATURES_DEFAULT).split("/")] @functools.cache @@ -47,12 +37,13 @@ def feature_disabled_stub() -> None: def feature(flag: Feature, alternative: Optional[Callable] = feature_disabled_stub) -> Callable: - """Usage: + """ + Usage:: - @feature(F_SOME_FLAG, altfunc) - def my_func( ... ) -> ... + @feature(Feature.SOME_FLAG, fallback) + def my_func( ... ) -> ... - Where F_SOME_FLAG is the feature you want to check for and + Where SOME_FLAG is the feature you want to check for and altfunc is the alternative function to serve if the feature flag is NOT set. """ diff --git a/tests/test_feature.py b/tests/test_feature.py index 8def8ab..d55d0a7 100644 --- a/tests/test_feature.py +++ b/tests/test_feature.py @@ -1,12 +1,6 @@ import pytest -from dissect.util.feature import ( - Feature, - FeatureException, - check_flags, - feature, - feature_enabled, -) +from dissect.util.feature import Feature, FeatureException, feature, feature_enabled def test_feature_flags() -> None: @@ -17,8 +11,8 @@ def fallback(): def experimental(): return True - @feature(Feature.NOVICE, fallback) - def novice(): + @feature(Feature.ADVANCED, fallback) + def advanced(): return True @feature(Feature.LATEST) @@ -30,17 +24,12 @@ def expert(): return True assert experimental() is False - assert novice() is True + assert advanced() is False assert latest() is True with pytest.raises(FeatureException): assert expert() is True -def test_feature_flag_verification() -> None: - with pytest.raises(FeatureException): - check_flags(["chaotic"]) - - def test_feature_flag_inline() -> None: assert feature_enabled(Feature.BETA) is False assert feature_enabled(Feature.LATEST) is True