Commit 8d3adc821b48111092392efeb9ae41cfdc5f5f0a
1 parent
2fedfd73
Exists in
master
and in
5 other branches
Import Colab configurations from py files.
YAML Settings file is deprecated now. Signed-off-by: Alexandre Barbosa <alexandreab@live.com> Signed-off-by: Gustavo Jaruga <darksshades@gmail.com>
Showing
2 changed files
with
69 additions
and
4 deletions
Show diff stats
colab/settings.py
... | ... | @@ -280,8 +280,12 @@ CONVERSEJS_SHOW_ONLY_ONLINE_USERS = True |
280 | 280 | TASTYPIE_DEFAULT_FORMATS = ['json', ] |
281 | 281 | |
282 | 282 | from .utils.conf import load_yaml_settings |
283 | +from .utils.conf import load_py_settings | |
284 | + | |
283 | 285 | locals().update(load_yaml_settings()) |
284 | 286 | |
287 | +locals().update(load_py_settings()) | |
288 | + | |
285 | 289 | if locals().get('RAVEN_DSN', False): |
286 | 290 | RAVEN_CONFIG = { |
287 | 291 | 'dsn': RAVEN_DSN + '?timeout=30', # noqa | ... | ... |
colab/utils/conf.py
1 | 1 | |
2 | 2 | import os |
3 | +import sys | |
4 | + | |
5 | +import warnings | |
6 | + | |
3 | 7 | import yaml |
4 | 8 | |
5 | 9 | import yamlordereddictloader |
6 | 10 | |
7 | 11 | from django.core.exceptions import ImproperlyConfigured |
8 | 12 | |
13 | +import importlib | |
14 | + | |
15 | + | |
16 | +USING_YAML_SETTINGS = False | |
17 | + | |
9 | 18 | |
10 | 19 | class InaccessibleYAMLSettings(ImproperlyConfigured): |
11 | 20 | """Settings YAML is Inaccessible. |
... | ... | @@ -31,9 +40,12 @@ def load_yaml_settings(): |
31 | 40 | settings_dir = '/etc/colab/settings.d' |
32 | 41 | yaml_path = os.getenv('COLAB_SETTINGS', '/etc/colab/settings.yaml') |
33 | 42 | |
34 | - if not os.path.exists(yaml_path): | |
35 | - msg = "The yaml file {} does not exist".format(yaml_path) | |
36 | - raise InaccessibleYAMLSettings(msg) | |
43 | + if os.path.exists(yaml_path): | |
44 | + global USING_YAML_SETTINGS | |
45 | + USING_YAML_SETTINGS = True | |
46 | + warnings.warn("YAML Settings file is deprecated. Use Py file instead.") | |
47 | + else: | |
48 | + return {} | |
37 | 49 | |
38 | 50 | yaml_settings = _load_yaml_file(yaml_path) |
39 | 51 | |
... | ... | @@ -47,4 +59,53 @@ def load_yaml_settings(): |
47 | 59 | |
48 | 60 | return yaml_settings or {} |
49 | 61 | |
50 | -yaml_settings = load_yaml_settings() | |
62 | + | |
63 | +class InaccessiblePySettings(ImproperlyConfigured): | |
64 | + """Settings.py is Inaccessible. | |
65 | + | |
66 | + Check if the file exists and if you have read permissions.""" | |
67 | + | |
68 | + | |
69 | +def _load_py_file(py_path): | |
70 | + try: | |
71 | + py_settings = importlib.import_module(py_path) | |
72 | + | |
73 | + except: | |
74 | + msg = ('Could not open settings file {}. Please ' | |
75 | + 'check if the file exists and if user ' | |
76 | + 'has read rights.').format(py_path) | |
77 | + raise InaccessiblePySettings(msg) | |
78 | + | |
79 | + return py_settings.__dict__ | |
80 | + | |
81 | + | |
82 | +def load_py_settings(): | |
83 | + settings_dir = '/etc/colab/settings.d' | |
84 | + settings_module = 'settings' | |
85 | + py_path = os.getenv('COLAB_SETTINGS', | |
86 | + "/etc/colab/{}.py".format(settings_module)) | |
87 | + | |
88 | + global USING_YAML_SETTINGS | |
89 | + if not os.path.exists(py_path) and not USING_YAML_SETTINGS: | |
90 | + msg = "The py file {} does not exist".format(py_path) | |
91 | + raise InaccessiblePySettings(msg) | |
92 | + elif USING_YAML_SETTINGS: | |
93 | + return {} | |
94 | + | |
95 | + sys.path.insert(0, '/etc/colab/') | |
96 | + sys.path.insert(0, settings_dir) | |
97 | + | |
98 | + py_settings = _load_py_file(settings_module) | |
99 | + | |
100 | + # Try to read settings from settings.d | |
101 | + if os.path.exists(settings_dir): | |
102 | + for file_name in os.listdir(settings_dir): | |
103 | + if file_name.endswith('.py'): | |
104 | + file_module = file_name.split('.')[0] | |
105 | + py_settings_d = _load_py_file(file_module) | |
106 | + py_settings.update(py_settings_d) | |
107 | + | |
108 | + sys.path.remove('/etc/colab/') | |
109 | + sys.path.remove(settings_dir) | |
110 | + | |
111 | + return py_settings or {} | ... | ... |