-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
77 lines (69 loc) · 2.35 KB
/
helpers.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
import brackets
import espn
import os
import users
import reddit_auth
import redis
import static_hack
import sys
##########################################################
## Helper methods
def load_config(app):
try:
app.config.from_object('config.settings')
# Allow overriding any specific config entry with environment variable
# named BRACKET_[CONFIG_NAME]. This does some hacky parsing to determine
# the value type, since env vars all come in as strings.
import config
config_keys = list(filter(lambda x: not x.startswith('_'), dir(config.settings)))
for key in config_keys:
env_field_name = 'BRACKET_' + key
if env_field_name in os.environ:
value = os.environ[env_field_name]
# Lazy-hack parsing
if value == 'True':
value = True
elif value == 'False':
value = False
elif value.isnumeric():
value = int(value)
app.config[key] = value
print('overriding', key, 'from environment')
except ImportError:
sys.stderr.write('\n'.join([
'Could not find config file. If this is your first time running this',
'program, try the following:',
'',
' cp config.py.EXAMPLE config.py',
' python app.py',
'',
'',
]))
sys.exit(-1)
def build_reddit_auth_instance(app):
return reddit_auth.RedditAuth(
login_host = app.config['LOGIN_HOST'],
user_agent = app.config['USER_AGENT'],
consumer_key = app.config['CONSUMER_KEY'],
consumer_secret = app.config['CONSUMER_SECRET'])
def build_database_connection(app):
return users.Users(
host = app.config['MYSQL_HOST'],
username = app.config['MYSQL_USERNAME'],
password = app.config['MYSQL_PASSWORD'],
database = app.config['MYSQL_DATABASE'],
year = app.config['YEAR'])
def build_brackets_manager(user_manager):
return brackets.Brackets(user_manager)
def start_espn_manager(app):
espn_manager = espn.Espn(
build_strict_redis(app),
# Start separate connection for ESPN, since it's on background thread
build_database_connection(app),
year = app.config['YEAR'])
espn_manager.start()
return espn_manager
def build_strict_redis(app):
return static_hack.get_redis_HACK(
callback = lambda: redis.StrictRedis(
host=app.config['REDIS_HOST'], port=app.config['REDIS_PORT']))