-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Namespaced waffle switches are now deprecated, just like waffle flags. Namespaced classes are still available as legacy classes.
- Loading branch information
Showing
9 changed files
with
157 additions
and
99 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
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,29 @@ | ||
""" | ||
Base waffle toggle classes. | ||
""" | ||
|
||
from ..base import BaseToggle | ||
|
||
|
||
# pylint: disable=abstract-method | ||
class BaseWaffle(BaseToggle): | ||
""" | ||
Base waffle toggle class, which performs waffle name validation. | ||
""" | ||
|
||
def __init__(self, name, module_name=None): | ||
""" | ||
Base waffle constructor | ||
Arguments: | ||
name (String): The name of the switch. This name must include a dot (".") to indicate namespacing. | ||
module_name (String): The name of the module where the flag is created. This should be ``__name__`` in most | ||
cases. | ||
""" | ||
if "." not in name: | ||
raise ValueError( | ||
"Cannot create non-namespaced '{}' {} instance".format( | ||
name, self.__class__.__name__ | ||
) | ||
) | ||
super().__init__(name, default=False, module_name=module_name) |
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,46 @@ | ||
""" | ||
New-style switch classes: these classes no longer depend on namespaces to be created. | ||
""" | ||
from weakref import WeakSet | ||
|
||
from waffle import switch_is_active | ||
|
||
from .base import BaseWaffle | ||
from .cache import _get_waffle_request_cache | ||
|
||
|
||
class WaffleSwitch(BaseWaffle): | ||
""" | ||
Represents a single waffle switch, using both a global and a request cache. | ||
""" | ||
|
||
_class_instances = WeakSet() | ||
|
||
def is_enabled(self): | ||
""" | ||
Legacy method preserved for backward compatibility. | ||
""" | ||
value = self.get_request_cache() | ||
if value is None: | ||
value = switch_is_active(self.name) | ||
self.set_request_cache(value) | ||
return value | ||
|
||
def get_request_cache(self, default=None): | ||
""" | ||
API for accessing the request cache. In general, users should avoid accessing the namespace cache. | ||
""" | ||
return self._cached_switches.get(self.name, default) | ||
|
||
def set_request_cache(self, value): | ||
""" | ||
Manually set the request cache value. Beware! There be dragons. | ||
""" | ||
self._cached_switches[self.name] = value | ||
|
||
@property | ||
def _cached_switches(self): | ||
""" | ||
Return a dictionary of all namespaced switches in the request cache. | ||
""" | ||
return _get_waffle_request_cache().setdefault("switches", {}) |
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