-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathconfig.py
73 lines (53 loc) · 1.77 KB
/
config.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
import datetime
import sqlalchemy as sa
from webservices.env import env
from webservices.common.models import db
def get_cycle_start(year):
"""Round year down to the first year of the two-year election cycle. Used
when filtering original data for election cycle.
"""
return year if year % 2 == 1 else year - 1
def get_cycle_end(year):
"""Round year up to the last year of the two-year election cycle. Used
when querying partitioned itemized data for election cycle.
"""
return year if year % 2 == 0 else year + 1
CURRENT_YEAR = datetime.datetime.now().year
SQL_CONFIG = {
"START_YEAR": get_cycle_start(1980),
"START_YEAR_AGGREGATE": get_cycle_start(2008),
"END_YEAR_ITEMIZED": get_cycle_start(CURRENT_YEAR),
}
REQUIRED_CREDS = (
"SQLA_CONN",
"FEC_SLACK_TOKEN",
)
REQUIRED_SERVICES = ("aws-elasticache-redis", "s3", "aws-elasticsearch")
REQUIRED_TABLES = tuple(db.Model.metadata.tables.keys()) + (
"ofec_pacronyms",
"ofec_nicknames",
"ofec_zips_districts",
)
def load_table(name):
try:
return sa.Table(name, db.metadata, autoload_with=db.engine)
except sa.exc.NoSuchTableError:
return None
def check_keys(keys, getter, formatter):
missing = [key for key in keys if getter(key) is None]
if missing:
print(formatter.format(", ".join(keys)))
return missing
CHECKS = [
(REQUIRED_CREDS, env.get_credential, "Missing creds {}"),
(
REQUIRED_SERVICES,
lambda service: env.get_service(label=service),
"Missing services {}",
),
(REQUIRED_TABLES, load_table, "Missing tables {}"),
]
def check_config():
results = [check_keys(*check) for check in CHECKS]
if any(results):
raise RuntimeError("Invalid configuration: {0}".format(results))