Add support for themes to define their own defaults

This commit is contained in:
(GalaxyMaster) 2020-04-14 21:35:38 +10:00
commit 9b31eda7f3
3 changed files with 31 additions and 2 deletions

View file

@ -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

View file

@ -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)

View file

@ -0,0 +1,3 @@
CUSTOM_THEME_SETTING = 'test'
PATH = 'a path'
AUTHOR = 'Test Suite'