From 9b31eda7f3a99d97681a5dfef7d3ee605274cf7a Mon Sep 17 00:00:00 2001 From: "(GalaxyMaster)" Date: Tue, 14 Apr 2020 21:35:38 +1000 Subject: [PATCH] Add support for themes to define their own defaults --- pelican/settings.py | 16 ++++++++++++++++ pelican/tests/test_settings.py | 14 ++++++++++++-- pelican/tests/themes/custom/pelicanconf.py | 3 +++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 pelican/tests/themes/custom/pelicanconf.py diff --git a/pelican/settings.py b/pelican/settings.py index c3b29ca5..11d6d9ef 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -524,6 +524,22 @@ def configure_settings(settings): raise Exception("Could not find the theme %s" % settings['THEME']) + theme_config = os.path.join(settings['THEME'], 'pelicanconf.py') + if os.path.isfile(theme_config): + preserved = {} + logger.debug('Theme provides a config `%s`', theme_config) + # preserve variables that are dynamically calculated in read_settings() + for p in ['PATH', 'OUTPUT_PATH', 'THEME', 'CACHE_PATH', 'PLUGIN_PATHS']: + if settings.get(p) is not None: + preserved[p] = settings.get(p) + settings.pop('THEME') # avoid recursion + settings = read_settings(theme_config, settings) + # restore them back to look like we did not calculate them again + for p in ['PATH', 'OUTPUT_PATH', 'THEME', 'CACHE_PATH', 'PLUGIN_PATHS']: + if preserved.get(p) is not None: + settings[p] = preserved.pop(p) + + # make paths selected for writing absolute if necessary settings['WRITE_SELECTED'] = [ os.path.abspath(path) for path in diff --git a/pelican/tests/test_settings.py b/pelican/tests/test_settings.py index d995527c..388dd7c1 100644 --- a/pelican/tests/test_settings.py +++ b/pelican/tests/test_settings.py @@ -6,14 +6,12 @@ import os from os.path import abspath, dirname, join from sys import platform - from pelican.settings import (DEFAULT_CONFIG, DEFAULT_THEME, _printf_s_to_format_field, configure_settings, handle_deprecated_settings, read_settings) from pelican.tests.support import unittest - class TestSettingsConfiguration(unittest.TestCase): """Provided a file, it should read it, replace the default values, append new values to the settings (if any), and apply basic settings @@ -66,6 +64,18 @@ class TestSettingsConfiguration(unittest.TestCase): new_settings = read_settings(default_conf) self.assertNotEqual(new_settings['SITEURL'], settings['SITEURL']) + def test_theme_settings_applied(self): + # Make sure that the results from loading theme's config do not + # affect the previously defined system configuration + self.PATH = abspath(dirname(__file__)) + default_conf = join(self.PATH, 'default_conf.py') + expected = copy.deepcopy(self.settings) + expected['THEME'] = os.path.join(self.PATH, 'themes/custom') + expected['CUSTOM_THEME_SETTING'] = 'test' + settings = read_settings(default_conf, { 'THEME': 'themes/custom' } ) + self.maxDiff = None + self.assertDictEqual(settings, expected) + def test_defaults_not_overwritten(self): # This assumes 'SITENAME': 'A Pelican Blog' settings = read_settings(None) diff --git a/pelican/tests/themes/custom/pelicanconf.py b/pelican/tests/themes/custom/pelicanconf.py new file mode 100644 index 00000000..7b76eb72 --- /dev/null +++ b/pelican/tests/themes/custom/pelicanconf.py @@ -0,0 +1,3 @@ +CUSTOM_THEME_SETTING = 'test' +PATH = 'a path' +AUTHOR = 'Test Suite'