forked from cs161-staff/extensions
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenvironment.py
More file actions
137 lines (108 loc) · 4.74 KB
/
environment.py
File metadata and controls
137 lines (108 loc) · 4.74 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
from typing import Optional
from dotenv import dotenv_values
from src.sheets import Sheet
PREFIX = "flextensions_"
DEFAULT_COURSE_NAME = "PLEASE SET A COURSE NAME"
DEFAULT_REPLY_TO_EMAIL = "PLEASE SET REPLY-TO EMAIL"
DEFAULT_AUTO_APPROVE_THRESHOLD = 1
DEFAULT_AUTO_APPROVE_THRESHOLD_DSP = 1
DEFAULT_APPROVE_ASSIGNMENT_THRESHOLD = 1
DEFAULT_MAX_TOTAL_REQUESTED_EXTENSIONS_THRESHOLD = 3
DEFAULT_EMAIL_FROM = "[{}] <{}@berkeley.edu>".format(DEFAULT_COURSE_NAME, DEFAULT_COURSE_NAME)
DEFAULT_EMAIL_SUBJECT = "[CS 000] Extension Request Update"
DEFAULT_EMAIL_SIGNATURE = "{} Staff".format(DEFAULT_COURSE_NAME)
DEFAULT_EXTEND_GRADESCOPE_ASSIGNMENTS = "No"
class Environment:
@staticmethod
def clear():
keys = os.environ.keys()
for key in keys:
if key.startswith(PREFIX):
del os.environ[key]
@staticmethod
def contains(key: str) -> bool:
return os.getenv(PREFIX + key) is not None and str(os.getenv(PREFIX + key)).strip() != ""
@staticmethod
def _safe_get(key: str, default: str = None) -> Optional[str]:
if os.getenv(PREFIX + key):
data = str(os.getenv(PREFIX + key)).strip()
if data:
return data
return default
# @staticmethod
# def get(key: str) -> Any:
# if not os.getenv(PREFIX + key):
# raise ConfigurationError("Environment variable not set: " + key)
# return os.getenv(PREFIX + key)
@staticmethod
def get_auto_approve_threshold() -> int:
return int(Environment._safe_get("AUTO_APPROVE_THRESHOLD", DEFAULT_AUTO_APPROVE_THRESHOLD))
@staticmethod
def get_auto_approve_threshold_dsp() -> int:
return int(Environment._safe_get("AUTO_APPROVE_THRESHOLD_DSP", DEFAULT_AUTO_APPROVE_THRESHOLD_DSP))
@staticmethod
def get_max_total_requested_extensions_threshold() -> int:
return int(Environment._safe_get("MAX_TOTAL_REQUESTED_EXTENSIONS_THRESHOLD", DEFAULT_MAX_TOTAL_REQUESTED_EXTENSIONS_THRESHOLD))
@staticmethod
def get_auto_approve_assignment_threshold() -> int:
return int(Environment._safe_get("AUTO_APPROVE_ASSIGNMENT_THRESHOLD", DEFAULT_APPROVE_ASSIGNMENT_THRESHOLD))
@staticmethod
def get_course_name() -> str:
return Environment._safe_get("COURSE_NAME", DEFAULT_COURSE_NAME)
@staticmethod
def get_reply_to_email() -> str:
return Environment._safe_get("REPLY_TO_EMAIL", DEFAULT_REPLY_TO_EMAIL)
@staticmethod
def get_email_from() -> str:
return Environment._safe_get("EMAIL_FROM", DEFAULT_EMAIL_FROM)
@staticmethod
def get_email_subject() -> str:
return Environment._safe_get("EMAIL_SUBJECT", DEFAULT_EMAIL_SUBJECT)
@staticmethod
def get_email_signature() -> str:
return Environment._safe_get("EMAIL_SIGNATURE", DEFAULT_EMAIL_SIGNATURE)
@staticmethod
def get_email_cc() -> Optional[str]:
return Environment._safe_get("EMAIL_CC")
@staticmethod
def get_slack_endpoint() -> Optional[str]:
return Environment._safe_get("SLACK_ENDPOINT")
@staticmethod
def get_slack_debug_endpoint() -> Optional[str]:
return Environment._safe_get("SLACK_DEBUG_ENDPOINT")
@staticmethod
def get_slack_tag_list() -> Optional[str]:
return Environment._safe_get("SLACK_TAG_LIST")
@staticmethod
def get_extend_gradescope_assignments() -> bool:
return Environment._safe_get("EXTEND_GRADESCOPE_ASSIGNMENTS", DEFAULT_EXTEND_GRADESCOPE_ASSIGNMENTS)
@staticmethod
def get_gradescope_email() -> Optional[str]:
return Environment._safe_get("GRADESCOPE_EMAIL")
@staticmethod
def get_gradescope_password() -> Optional[str]:
return Environment._safe_get("GRADESCOPE_PASSWORD")
@staticmethod
def get_spreadsheet_url() -> Optional[str]:
return Environment._safe_get("SPREADSHEET_URL")
@staticmethod
def configure_env_vars(sheet: Sheet):
"""
Reads environment variables from the "Environment Variables" sheet, and stores them into this process's
environment variables for downstream use. Expects two columns: a "key" column, and a "value"
"""
records = sheet.get_all_records()
for record in records:
key = record.get("key")
value = record.get("value")
if not key:
continue
os.environ[PREFIX + key] = str(value)
# Load local environment variables now from .env, which override remote provided variables for debugging
if os.path.exists(".env-pytest"):
for key, value in dotenv_values(".env-pytest").items():
if key == "APP_MASTER_SECRET":
os.environ[key] = value
else:
os.environ[PREFIX + key] = value