~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/features/xmlrpc.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-09-21 19:10:33 UTC
  • mfrom: (14006.1.1 revert-r13986)
  • Revision ID: launchpad@pqm.canonical.com-20110921191033-9ith5mj5yq8jbvro
[rs=sinzui][bug=855609] Rollback r13986 because team scopes are
        broken.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2011 Canonical Ltd.  This software is licensed under the
2
 
# GNU Affero General Public License version 3 (see the file LICENSE).
3
 
 
4
 
"""FeatureFlagApplication allows access to information about feature flags."""
5
 
 
6
 
__metaclass__ = type
7
 
__all__ = [
8
 
    'IFeatureFlagApplication',
9
 
    'FeatureFlagApplication',
10
 
    ]
11
 
 
12
 
from zope.component import getUtility
13
 
from zope.interface import implements
14
 
 
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 (
20
 
    default_scopes,
21
 
    FixedScope,
22
 
    MultiScopeHandler,
23
 
    TeamScope,
24
 
    )
25
 
 
26
 
 
27
 
class IFeatureFlagApplication(ILaunchpadApplication):
28
 
    """Mailing lists application root."""
29
 
 
30
 
    def getFeatureFlag(flag_name, username=None, scopes=()):
31
 
        """Return the value of the given feature flag.
32
 
 
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
38
 
            included here.
39
 
        """
40
 
 
41
 
 
42
 
class FeatureFlagApplication:
43
 
 
44
 
    implements(IFeatureFlagApplication)
45
 
 
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))
54
 
            else:
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)