-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature flag, and docs and improve tests
Signed-off-by: John Zielke <[email protected]>
- Loading branch information
1 parent
5de6a84
commit 22518de
Showing
4 changed files
with
198 additions
and
41 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
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,61 @@ | ||
# Copyright (c) MONAI Consortium | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from __future__ import annotations | ||
|
||
import os | ||
from contextlib import contextmanager | ||
|
||
FEATURE_FLAG_PREFIX = "MONAI_FEATURE_ENABLED_" | ||
|
||
|
||
class FeatureFlag: | ||
def __init__(self, name: str, *, default: bool = False): | ||
self.name = name | ||
self._enabled = None | ||
self.default = default | ||
|
||
def _get_from_env(self): | ||
return os.getenv(FEATURE_FLAG_PREFIX + self.name, None) | ||
|
||
@property | ||
def enabled(self): | ||
if self._enabled is None: | ||
env = self._get_from_env() | ||
if env is None: | ||
self._enabled = self.default | ||
else: | ||
self._enabled = env.lower() in ["true", "1", "yes"] | ||
return self._enabled | ||
|
||
@enabled.setter | ||
def enabled(self, value: bool): | ||
self._enabled = value | ||
|
||
def enable(self): | ||
self.enabled = True | ||
|
||
def disable(self): | ||
self.enabled = False | ||
|
||
def __str__(self): | ||
return f"{self.name}: {self.enabled}, default: {self.default}" | ||
|
||
|
||
@contextmanager | ||
def with_feature_flag(feature_flag: FeatureFlag, enabled: bool): | ||
original = feature_flag.enabled | ||
feature_flag.enabled = enabled | ||
try: | ||
yield | ||
finally: | ||
feature_flag.enabled = original |
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