Commit 7bde548fe11361c4159b2ccfbcf3513d4a490f81
1 parent
4622cb2a
Exists in
master
and in
39 other branches
Added helper to load yaml settings
Showing
1 changed file
with
32 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,32 @@ |
1 | + | |
2 | +import os | |
3 | +import yaml | |
4 | + | |
5 | +from django.core.exceptions import ImproperlyConfigured | |
6 | + | |
7 | + | |
8 | +class InaccessibleYAMLSettings(ImproperlyConfigured): | |
9 | + """Settings YAML is Inaccessible. | |
10 | + | |
11 | + Check if the file exists and if you have read permissions.""" | |
12 | + | |
13 | + | |
14 | +def load_yaml_settings(): | |
15 | + yaml_path = os.getenv('COLAB_SETTINGS', '/etc/colab.yaml') | |
16 | + | |
17 | + if not os.path.exists(yaml_path): | |
18 | + msg = "The yaml file {} does not exist".format(yaml_path) | |
19 | + raise InaccessibleYAMLSettings(msg) | |
20 | + | |
21 | + try: | |
22 | + with open(yaml_path) as yaml_file: | |
23 | + yaml_settings = yaml.load(yaml_file.read()) | |
24 | + except IOError: | |
25 | + msg = ('Could not open settings file {}. Please ' | |
26 | + 'check if the file exists and if user ' | |
27 | + 'has read rights.').format(yaml_path) | |
28 | + raise InaccessibleYAMLSettings(msg) | |
29 | + | |
30 | + return yaml_settings | |
31 | + | |
32 | +yaml_settings = load_yaml_settings() | ... | ... |