-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
139 lines (121 loc) · 4.35 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
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
138
139
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Importing libraries
import os
from functools import reduce
import yaml
'''
Based on https://github.com/hmanuel1/covid
'''
class DotDict(dict):
""" Map dictionary to use `dot` notation
This is copy from stackoverflow.com at:
https://stackoverflow.com/questions/39463936
This is a read-only view of the dictionary
If a key is missing, it returns None instead of KeyError
"""
def __getattr__(self, key):
try:
value = self[key]
except KeyError:
return None
if isinstance(value, dict):
return DotDict(value)
return value
def __getitem__(self, key):
if isinstance(key, str) and '.' in key:
key = key.split('.')
if isinstance(key, (list, tuple)):
return reduce(lambda d, kk: d[kk], key, self)
return super().__getitem__(key)
def get(self, key, default=None):
if isinstance(key, str) and '.' in key:
try:
return self[key]
except KeyError:
return default
return super().get(key, default=default)
def cwd():
"""Return current working directory if running in a server,
jupiter or python.
Returns:
String -- path to current working directory
"""
try:
__file__
except NameError:
cur_working_dir = os.getcwd()
else:
cur_working_dir = os.path.dirname(__file__)
return cur_working_dir
def load_config():
"""Load application configuration from config.yaml
Returns:
dictionary -- dictionary with configuration values
"""
config_file = open(os.path.join(cwd(), "config.yaml"))
config = yaml.load(config_file, Loader=yaml.FullLoader)
return config
# Application configuration using dot notation
# Read-only view of configuration parameters defined
# in config.yaml expected in this directory.
CONFIG = DotDict(load_config())
FASTAPI_PATH = CONFIG.app.fastapi.path
if CONFIG.environment == 'local':
# when running locally, this listening port
# number is set to a constant in the config.yaml file.
# normally, it is set to 8000.
FASTAPI_PORT = CONFIG.proxy.fastapi.local.port
FASTAPI_ADDR = CONFIG.proxy.fastapi.local.address
FASTAPI_URL = f"http://{FASTAPI_ADDR}:{FASTAPI_PORT}"
elif CONFIG.environment == 'heroku':
# when running at heroku,
# heroku assigns a listening port number dynamically,
# at starts up. Then, it passes this value in
# the environment variable PORT to the application.
# Only one public facing listening port is available
# per heroku app.
FASTAPI_PORT = os.environ.get('PORT')
FASTAPI_DN = CONFIG.proxy.fastapi.heroku.domain
FASTAPI_ADDR = CONFIG.proxy.fastapi.heroku.address
FASTAPI_URL = f"https://{FASTAPI_DN}"
def set_bokeh_port(port):
""" Set bokeh port number
When running locally, it sets internal bokeh port
number in .env file expected in this directory.
When running at heroku, it sets the environment
variable BOKEH_PORT.
This value is set only once at startup by bkapp.py
and used solely for communication between flask app
and bokeh app.
Arguments:
port {int} -- bokeh port number
"""
if CONFIG.environment == 'local':
with open(os.path.join(cwd(), ".env"), 'w') as env_file:
env_file.write(str(port))
elif CONFIG.environment == 'heroku':
os.environ['BOKEH_PORT'] = str(port)
def get_bokeh_port():
"""Get bokeh server port
When running locally, it returns internal bokeh
port number stored in the .env file.
When running at heroku, it returns port number
store in the environment variable BOKEH_PORT
This value is set only once at startup by bkapp.py
and used solely for communication between flask app
and bokeh app.
Returns:
str -- bokeh port number
"""
if CONFIG.environment == 'local':
with open(os.path.join(cwd(), ".env"), 'r') as env_file:
port = env_file.read()
elif CONFIG.environment == 'heroku':
port = os.environ.get('BOKEH_PORT')
return port
BOKEH_PATH = CONFIG.app.bokeh.path
BOKEH_ADDR = CONFIG.proxy.bokeh.local.address
BOKEH_WS_PATH = CONFIG.proxy.bokeh.local.path
BOKEH_URL = f"http://{BOKEH_ADDR}:{get_bokeh_port()}"
BOKEH_URI = f"ws://{BOKEH_ADDR}:{get_bokeh_port()}{BOKEH_WS_PATH}"