1
# Copyright 2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""FeatureFlagApplication allows access to information about feature flags."""
8
'IFeatureFlagApplication',
9
'FeatureFlagApplication',
12
from zope.component import getUtility
13
from zope.interface import implements
15
from canonical.launchpad.webapp.interfaces import ILaunchpadApplication
16
from lp.registry.interfaces.person import IPersonSet
17
from lp.services.features.flags import FeatureController
18
from lp.services.features.rulesource import StormFeatureRuleSource
19
from lp.services.features.scopes import (
27
class IFeatureFlagApplication(ILaunchpadApplication):
28
"""Mailing lists application root."""
30
def getFeatureFlag(flag_name, username=None, scopes=()):
31
"""Return the value of the given feature flag.
33
:param flag_name: The name of the flag to query.
34
:param username: If supplied, the name of a Person to use in
35
evaluating the 'team:' scope.
36
:param scopes: A list of scopes to consider active. The 'default'
37
scope is always considered to be active, and does not need to be
42
class FeatureFlagApplication:
44
implements(IFeatureFlagApplication)
46
def getFeatureFlag(self, flag_name, active_scopes=()):
47
scopes = list(default_scopes)
48
for scope_name in active_scopes:
49
if scope_name.startswith('user:'):
50
person = getUtility(IPersonSet).getByName(
51
scope_name[len('user:'):])
52
if person is not None:
53
scopes.append(TeamScope(person))
55
scopes.append(FixedScope(scope_name))
56
flag_name = unicode(flag_name)
57
controller = FeatureController(
58
MultiScopeHandler(scopes).lookup, StormFeatureRuleSource())
59
return controller.getFlag(flag_name)