diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | pelican/settings.py | 32 |
2 files changed, 24 insertions, 10 deletions
@@ -9,3 +9,5 @@ build dist tags .tox +.coverage +htmlcov diff --git a/pelican/settings.py b/pelican/settings.py index e9888f94..645a9809 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +import imp +import inspect import os import locale import logging @@ -84,21 +86,31 @@ def read_settings(filename=None): return configured_settings -def get_settings_from_file(filename, default_settings=None): - """Load a Python file into a dictionary. +def get_settings_from_module(module=None, default_settings=_DEFAULT_CONFIG): """ - if default_settings == None: - default_settings = _DEFAULT_CONFIG + Load settings from a module, returning a dict. + + """ + context = default_settings.copy() - if filename: - tempdict = {} - execfile(filename, tempdict) - for key in tempdict: - if key.isupper(): - context[key] = tempdict[key] + if module is not None: + context.update( + (k, v) for k, v in inspect.getmembers(module) if k.isupper() + ) return context +def get_settings_from_file(filename, default_settings=_DEFAULT_CONFIG): + """ + Load settings from a file path, returning a dict. + + """ + + name = os.path.basename(filename).rpartition(".")[0] + module = imp.load_source(name, filename) + return get_settings_from_module(module, default_settings=default_settings) + + def configure_settings(settings, default_settings=None, filename=None): """Provide optimizations, error checking, and warnings for loaded settings""" if default_settings is None: |