diff --git a/colab/settings.py b/colab/settings.py index c017b2c..53a0cd0 100644 --- a/colab/settings.py +++ b/colab/settings.py @@ -280,8 +280,12 @@ CONVERSEJS_SHOW_ONLY_ONLINE_USERS = True TASTYPIE_DEFAULT_FORMATS = ['json', ] from .utils.conf import load_yaml_settings +from .utils.conf import load_py_settings + locals().update(load_yaml_settings()) +locals().update(load_py_settings()) + if locals().get('RAVEN_DSN', False): RAVEN_CONFIG = { 'dsn': RAVEN_DSN + '?timeout=30', # noqa diff --git a/colab/utils/conf.py b/colab/utils/conf.py index b0dc80b..3226f85 100644 --- a/colab/utils/conf.py +++ b/colab/utils/conf.py @@ -1,11 +1,20 @@ 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. @@ -31,9 +40,12 @@ def load_yaml_settings(): settings_dir = '/etc/colab/settings.d' yaml_path = os.getenv('COLAB_SETTINGS', '/etc/colab/settings.yaml') - if not os.path.exists(yaml_path): - msg = "The yaml file {} does not exist".format(yaml_path) - raise InaccessibleYAMLSettings(msg) + 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) @@ -47,4 +59,53 @@ def load_yaml_settings(): return yaml_settings or {} -yaml_settings = load_yaml_settings() + +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.__dict__ + + +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) + + # 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) + py_settings.update(py_settings_d) + + sys.path.remove('/etc/colab/') + sys.path.remove(settings_dir) + + return py_settings or {} -- libgit2 0.21.2