This repository has been archived by the owner on Jun 24, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
state.py
114 lines (87 loc) · 3.87 KB
/
state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# The contents of this file are subject to the Common Public Attribution
# License Version 1.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://code.reddit.com/LICENSE. The License is based on the Mozilla Public
# License Version 1.1, but Sections 14 and 15 have been added to cover use of
# software over a computer network and provide for limited attribution for the
# Original Developer. In addition, Exhibit A has been modified to be consistent
# with Exhibit B.
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
#
# The Original Code is reddit.
#
# The Original Developer is the Initial Developer. The Initial Developer of
# the Original Code is reddit Inc.
#
# All portions of the code written by reddit are Copyright (c) 2006-2015 reddit
# Inc. All Rights Reserved.
###############################################################################
import json
from pylons import g
class FeatureState(object):
"""A FeatureState is the state of a feature and its condition in the world.
It determines if this feature is enabled given the world provided.
"""
# Special values for globally enabled properties - no need to interrogate
# the world for these values.
GLOBALLY_ON = "on"
GLOBALLY_OFF = "off"
# constant config blocks
DISABLED_CFG = {"enabled": GLOBALLY_OFF}
ENABLED_CFG = {"enabled": GLOBALLY_ON}
def __init__(self, name, world):
self.name = name
self.world = world
self.config = self._parse_config(name)
def _parse_config(self, name):
"""Find and parse a config from our live config with this given name.
:param name string - a given feature name
:return dict - a dictionary with at least "enabled". May include more
depending on the enabled type.
"""
config_name = "feature_%s" % name
config_str = self.world.live_config(config_name)
if not config_str or config_str == FeatureState.GLOBALLY_OFF:
return self.DISABLED_CFG
if config_str == FeatureState.GLOBALLY_ON:
return self.ENABLED_CFG
try:
config = json.loads(config_str)
except (ValueError, TypeError) as e:
g.log.warning("Could not load config for name %r - %r",
config_name, e)
return self.DISABLED_CFG
if not isinstance(config, dict):
g.log.warning("Config not dict, on or off: %r", config_name)
return self.DISABLED_CFG
return config
def is_enabled(self, user=None, subreddit=None, subdomain=None):
cfg = self.config
world = self.world
if cfg.get('enabled') == self.GLOBALLY_ON:
return True
if cfg.get('enabled') == self.GLOBALLY_OFF:
return False
url_flag = cfg.get('url')
if url_flag and url_flag in world.url_features():
return True
if cfg.get('admin') and world.is_admin(user):
return True
if cfg.get('employee') and world.is_employee(user):
return True
if cfg.get('gold') and world.has_gold(user):
return True
users = [u.lower() for u in cfg.get('users', [])]
if users and user and user.name.lower() in users:
return True
subreddits = [s.lower() for s in cfg.get('subreddits', [])]
if subreddits and subreddit and subreddit.lower() in subreddits:
return True
subdomains = [s.lower() for s in cfg.get('subdomains', [])]
if subdomains and subdomain and subdomain.lower() in subdomains:
return True
# Unknown value, default to off.
return False