This repository was archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathwrapper.py
More file actions
56 lines (42 loc) · 1.78 KB
/
wrapper.py
File metadata and controls
56 lines (42 loc) · 1.78 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
# -*- coding: utf-8 -*-
import importlib
import json
import os
from . import defaults
class SettingsWrapper(object):
def __init__(self):
self.defaults = {}
self.settings_modules = []
self._load_from_module(defaults, self.defaults)
if os.environ.get('THUMBNAILS_SETTINGS_MODULE'):
self.settings_modules.append(
importlib.import_module(os.environ.get('THUMBNAILS_SETTINGS_MODULE'))
)
if os.environ.get('DJANGO_SETTINGS_MODULE'):
try:
from django.conf import settings as settings_django # noqa skip:isort
from . import defaults_django
self.settings_modules.append(defaults_django)
self.settings_modules.append(settings_django)
except ImportError:
pass
if not os.path.exists(self.THUMBNAIL_PATH):
os.makedirs(os.path.dirname(self.THUMBNAIL_PATH), exist_ok=True)
os.makedirs(self.THUMBNAIL_PATH, exist_ok=True)
def __getattr__(self, key):
value = self.defaults.get(key, 'unknown setting')
for settings_module in self.settings_modules:
if hasattr(settings_module, key):
value = getattr(settings_module, key)
if 'overridden_settings' in os.environ:
settings = json.loads(os.environ['overridden_settings'])
if key in settings:
value = settings[key]
if value == 'unknown setting':
raise AttributeError('No setting for "{}".'.format(key))
return value
def _load_from_module(self, _module, target):
for setting in dir(_module):
if not setting.startswith('_'):
target[setting] = getattr(_module, setting)
settings = SettingsWrapper()