Skip to content

Commit

Permalink
Implement suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
cecinestpasunepipe committed Nov 14, 2023
1 parent 6db3e22 commit 3cbb8ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
25 changes: 8 additions & 17 deletions dissect/util/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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.
"""
Expand Down
19 changes: 4 additions & 15 deletions tests/test_feature.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)
Expand All @@ -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

0 comments on commit 3cbb8ec

Please sign in to comment.