Dynamic Pacing: Waffle switch

This commit is contained in:
Nimisha Asthagiri
2017-10-23 18:26:49 -04:00
parent 2211ae335b
commit cd1eaf3010
5 changed files with 47 additions and 8 deletions

View File

@@ -48,10 +48,14 @@ To test WaffleSwitchNamespace, use the provided context managers. For example:
import logging
from abc import ABCMeta
from contextlib import contextmanager
import six
from opaque_keys.edx.keys import CourseKey
from request_cache import get_cache as get_request_cache, get_request
from waffle import flag_is_active, switch_is_active
from request_cache import get_cache as get_request_cache
from request_cache import get_request
log = logging.getLogger(__name__)
@@ -165,6 +169,31 @@ class WaffleSwitchNamespace(WaffleNamespace):
return self._get_request_cache().setdefault('switches', {})
class WaffleSwitch(object):
"""
Represents a single waffle switch, using a cached namespace.
"""
def __init__(self, waffle_namespace, switch_name):
"""
Arguments:
waffle_namespace (WaffleSwitchNamespace | String): Namespace for this switch.
switch_name (String): The name of the switch (without namespacing).
"""
if isinstance(waffle_namespace, six.string_types):
waffle_namespace = WaffleSwitchNamespace(name=waffle_namespace)
self.waffle_namespace = waffle_namespace
self.switch_name = switch_name
def is_enabled(self):
return self.waffle_namespace.is_enabled(self.switch_name)
@contextmanager
def override(self, active=True):
with self.waffle_namespace.override(self.switch_name, active):
yield
class WaffleFlagNamespace(WaffleNamespace):
"""
Provides a single namespace for a set of waffle flags.