conf.py
4.18 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
138
139
140
141
142
143
144
145
146
147
import os
import sys
import warnings
import yaml
import yamlordereddictloader
from django.core.exceptions import ImproperlyConfigured
import importlib
USING_YAML_SETTINGS = False
class InaccessibleYAMLSettings(ImproperlyConfigured):
"""Settings YAML is Inaccessible.
Check if the file exists and if you have read permissions."""
def _load_yaml_file(yaml_path):
try:
with open(yaml_path) as yaml_file:
yaml_settings = yaml.load(yaml_file.read(),
yamlordereddictloader.Loader)
except IOError:
msg = ('Could not open settings file {}. Please '
'check if the file exists and if user '
'has read rights.').format(yaml_path)
raise InaccessibleYAMLSettings(msg)
return yaml_settings
def load_yaml_settings():
settings_dir = '/etc/colab/settings.d'
yaml_path = os.getenv('COLAB_SETTINGS', '/etc/colab/settings.yaml')
if os.path.exists(yaml_path):
global USING_YAML_SETTINGS
USING_YAML_SETTINGS = True
warnings.warn("YAML Settings file is deprecated. Use Py file instead.")
else:
return {}
yaml_settings = _load_yaml_file(yaml_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('.yaml') or file_name.endswith('yml'):
file_path = os.path.join(settings_dir, file_name)
yaml_settings_d = _load_yaml_file(file_path)
yaml_settings.update(yaml_settings_d)
return yaml_settings or {}
class InaccessiblePySettings(ImproperlyConfigured):
"""Settings.py is Inaccessible.
Check if the file exists and if you have read permissions."""
def _load_py_file(py_path):
try:
py_settings = importlib.import_module(py_path)
except:
msg = ('Could not open settings file {}. Please '
'check if the file exists and if user '
'has read rights.').format(py_path)
raise InaccessiblePySettings(msg)
return py_settings
def load_py_settings():
settings_dir = '/etc/colab/settings.d'
settings_module = 'settings'
py_path = os.getenv('COLAB_SETTINGS',
"/etc/colab/{}.py".format(settings_module))
global USING_YAML_SETTINGS
if not os.path.exists(py_path) and not USING_YAML_SETTINGS:
msg = "The py file {} does not exist".format(py_path)
raise InaccessiblePySettings(msg)
elif USING_YAML_SETTINGS:
return {}
sys.path.insert(0, '/etc/colab/')
sys.path.insert(0, settings_dir)
py_settings = _load_py_file(settings_module).__dict__
# 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).__dict__
py_settings.update(py_settings_d)
sys.path.remove('/etc/colab/')
sys.path.remove(settings_dir)
return py_settings or {}
def load_colab_apps():
plugins_dir = '/etc/colab/plugins.d/'
global USING_YAML_SETTINGS
if USING_YAML_SETTINGS:
return {}
sys.path.insert(0, plugins_dir)
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)
fields = ['urls', 'menu', 'upstream', 'middlewares',
'dependencies', 'context_processors']
app_name = getattr(py_settings_d, 'name', None)
if not app_name:
warnings.warn("Plugin missing name variable")
continue
COLAB_APPS[app_name] = {}
for key in fields:
value = getattr(py_settings_d, key, None)
if value:
COLAB_APPS[app_name][key] = value
sys.path.remove(plugins_dir)
return {'COLAB_APPS': COLAB_APPS}