-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
82 lines (69 loc) · 2.26 KB
/
config.py
File metadata and controls
82 lines (69 loc) · 2.26 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
""" This module contains the configuration variables needed by the app"""
import json
def _json_read_key(data, key):
"""
Read the value of a key
Args:
data (str): The value of the parameter
key (str): The name of the parameter
Returns:
data[key]: The value of the key, None if not provided
"""
try:
return data[key]
except KeyError:
return None
def _require_key(value, name):
"""
Check if a key is provided if required to continue
(Method not currently in use)
Args:
value (str): The value of the parameter
name (str): The name of the parameter
Raises:
KeyError: If key/value is not specified in config.json
"""
# Currently not required
if value is None:
raise KeyError("Missing required configuration key: '%s'" % name)
class Config:
"""
The configuration of the Flask app, can be used to update the app object
"""
def __init__(self):
self.port = None
self.host = None
self.cors_origins = None
def from_json(self, data):
"""
Pulls information from config.json. Should be updated as json is modified.
Args:
data (str): config.json
Updates:
self.port (int): port to run Flask app on
self.host (str): ip to run Flask app on
"""
data = json.loads(data)
self.port = _json_read_key(data, "port")
self.host = _json_read_key(data, "host")
self.cors_origins = _json_read_key(data, "cors_origins")
# Set port and host to default if not specified
if self.port is None:
self.port = 5000
if self.host is None:
self.host = "127.0.0.1"
def get_dict(self):
"""
Returns the parameters for configuration of the app object
Returns:
configs (dict): Contains all parameters in config.json
"""
# Require that CORS_DOMAINS has been provided and it can be iterated
_require_key(self.cors_origins, "CORS_DOMAINS")
if not hasattr(self.cors_origins, "__iter__"):
raise TypeError
return {
"PORT": self.port,
"HOST": self.host,
"CORS_ORIGINS": self.cors_origins
}