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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
"""Functions used in setting up configurations."""
import os # replace os.path with pathlib.Path
import sys
import logging
from flask import current_app
logger = logging.getLogger(__name__)
def override_from_envvars(app):
"""
Override `app` configuration values with those in the enviroment variables with the same names.
"""
configs = dict((key, value.strip()) for key,value in
((key, os.environ.get(key)) for key in app.config.keys())
if value is not None and value != "")
app.config.update(**configs)
return app
def get_setting(app, setting_id, guess=None):
"""Resolve a setting from the `app`."""
setting = app.config.get(setting_id, guess or "")
if setting is None or setting == "":
raise Exception(
f"{setting_id} setting unknown or faulty "
"(update default_settings.py?).")
return setting
def get_setting_bool(app, setting_id):
v = get_setting(app, setting_id)
if v not in [0, False, 'False', 'FALSE', None]:
return True
return False
def get_setting_int(app, setting_id):
val = get_setting(app, setting_id)
if isinstance(val, str):
return int(val)
if val is None:
return 0
return val
def valid_bin(path):
if os.path.islink(path) or valid_file(path):
return path
return None
def valid_file(path):
if os.path.isfile(path):
return path
return None
def valid_path(path):
if os.path.isdir(path):
return path
return None
def flat_file_exists(app, subdir):
base = get_setting(app, "GENENETWORK_FILES")
return valid_path(base + "/" + subdir)
def flat_files(app, subdir=None):
base = get_setting(app, "GENENETWORK_FILES")
if subdir:
return assert_dir(base + "/" + subdir)
return assert_dir(base)
def assert_bin(fn):
if not valid_bin(fn):
raise Exception("ERROR: can not find binary " + fn)
return fn
def assert_dir(the_dir):
if not valid_path(the_dir):
raise FileNotFoundError(f"ERROR: can not find directory '{the_dir}'")
return the_dir
def assert_writable_dir(path):
try:
fn = path + "/test.txt"
fh = open(fn, 'w')
fh.write("I am writing this text to the file\n")
fh.close()
os.remove(fn)
except IOError:
raise Exception(f"Unable to write test.txt to directory {path}")
return path
def assert_file(fn):
if not valid_file(fn):
raise FileNotFoundError(f"Unable to find file '{fn}'")
return fn
def mk_dir(path):
if not valid_path(path):
os.makedirs(path)
return assert_dir(path)
def locate(app, name, subdir=None):
"""
Locate a static flat file in the GENENETWORK_FILES environment.
This function throws an error when the file is not found.
"""
base = get_setting(app, "GENENETWORK_FILES")
if subdir:
base = base + "/" + subdir
if valid_path(base):
lookfor = base + "/" + name
if valid_file(lookfor):
return lookfor
else:
raise Exception("Can not locate " + lookfor)
if subdir:
sys.stderr.write(subdir)
raise Exception("Can not locate " + name + " in " + base)
def locate_ignore_error(app, name, subdir=None):
"""
Locate a static flat file in the GENENETWORK_FILES environment.
This function does not throw an error when the file is not found
but returns None.
"""
base = get_setting(app, "GENENETWORK_FILES")
if subdir:
base = base + "/" + subdir
if valid_path(base):
lookfor = base + "/" + name
if valid_file(lookfor):
return lookfor
return None
def tempdir(app):
"""Retrieve the configured temporary directory or `/tmp`."""
return valid_path(get_setting(app, "TMPDIR", "/tmp"))
def show_settings(app):
"""Print out the application configurations."""
BLUE = '\033[94m'
GREEN = '\033[92m'
BOLD = '\033[1m'
ENDC = '\033[0m'
app = app or current_app
LOG_LEVEL = app.config.get("LOG_LEVEL")
print(("Set global log level to " + BLUE + LOG_LEVEL + ENDC),
file=sys.stderr)
log_level = getattr(logging, LOG_LEVEL.upper())
logging.basicConfig(level=log_level)
logger.info(BLUE + "Mr. Mojo Risin 2" + ENDC)
keylist = list(app.config.keys())
print("runserver.py: ****** Webserver configuration - k,v pairs from app.config ******",
file=sys.stderr)
keylist.sort()
for k in keylist:
try:
print(("%s: %s%s%s%s" % (k, BLUE, BOLD, get_setting(app, k), ENDC)),
file=sys.stderr)
except:
print(("%s: %s%s%s%s" % (k, GREEN, BOLD, app.config[k], ENDC)),
file=sys.stderr)
|