conf.py
3.41 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
import os
import sys
import importlib
import warnings
from django.core.exceptions import ImproperlyConfigured
class InaccessibleSettings(ImproperlyConfigured):
"""Settings.py is Inaccessible.
Check if the file exists and if you have read permissions."""
class DatabaseUndefined(ImproperlyConfigured):
"""Default database is not set.
When DEBUG is set to True a local sqlite database can be used for
developement porposes but otherwise the `default` database must
be set."""
def _load_py_file(py_path, path):
original_path = sys.path
sys.path = [path]
try:
py_settings = importlib.import_module(py_path)
except IOError:
msg = ('Could not open settings file {}. Please '
'check if the file exists and if user '
'has read rights.').format(py_path)
raise InaccessibleSettings(msg)
except SyntaxError as excpt:
msg = ('Syntax Error: {}'.format(excpt))
raise InaccessibleSettings(msg)
finally:
sys.path = original_path
py_setting = {var: getattr(py_settings, var) for var in dir(py_settings)
if not var.startswith('__')}
return py_setting
def load_py_settings():
settings_dir = '/etc/colab/settings.d'
settings_file = os.getenv('COLAB_SETTINGS', '/etc/colab/settings.py')
settings_module = settings_file.split('.')[-2].split('/')[-1]
py_path = "/".join(settings_file.split('/')[:-1])
if not os.path.exists(py_path):
msg = "The py file {} does not exist".format(py_path)
raise InaccessibleSettings(msg)
py_settings = _load_py_file(settings_module, py_path)
# Try to read settings from settings.d
if os.path.exists(settings_dir):
for file_name in os.listdir(settings_dir):
if file_name.endswith('.py'):
file_module = file_name.split('.')[0]
py_settings_d = _load_py_file(file_module, settings_dir)
py_settings.update(py_settings_d)
return py_settings
def load_colab_apps():
plugins_dir = os.getenv('COLAB_PLUGINS', '/etc/colab/plugins.d/')
COLAB_APPS = {}
# Try to read settings from plugins.d
if os.path.exists(plugins_dir):
for file_name in os.listdir(plugins_dir):
if file_name.endswith('.py'):
file_module = file_name.split('.')[0]
py_settings_d = _load_py_file(file_module, plugins_dir)
fields = ['verbose_name', 'upstream', 'urls',
'menu_urls', 'middlewares', 'dependencies',
'context_processors', 'private_token']
app_name = py_settings_d.get('name')
if not app_name:
warnings.warn("Plugin missing name variable")
continue
COLAB_APPS[app_name] = {}
COLAB_APPS[app_name]['menu_title'] = \
py_settings_d.get('menu_title')
for key in fields:
value = py_settings_d.get(key)
if value:
COLAB_APPS[app_name][key] = value
return {'COLAB_APPS': COLAB_APPS}
def validate_database(database_dict, default_db, debug):
db_name = database_dict.get('default', {}).get('NAME')
if not debug and db_name == default_db:
msg = ('Since DEBUG is set to False DATABASE must be set on '
'Colab settings')
raise DatabaseUndefined(msg)