annotate piecrust/appconfig.py @ 1188:a7c43131d871

bake: Fix file write flushing problem with Python 3.8+ Writing the cache files fails in Python 3.8 because it looks like flushing behaviour has changed. We need to explicitly flush. And even then, in very rare occurrences, it looks like it can still run into racing conditions, so we do a very hacky and ugly "retry" loop when fetching cached data :(
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 15 Jun 2021 22:36:23 -0700
parents 10520472cc73
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import re
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
1168
10520472cc73 routing: Fix breakages with routing on some versions of Python.
Ludovic Chabant <ludovic@chabant.com>
parents: 1150
diff changeset
3 import sys
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import copy
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import json
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 import urllib
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 import logging
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 import hashlib
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 import collections
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 import yaml
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
11 from piecrust import APP_VERSION, CACHE_VERSION, DEFAULT_DATE_FORMAT
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
12 from piecrust.appconfigdefaults import (
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
13 default_configuration,
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
14 default_theme_content_model_base,
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
15 default_content_model_base,
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
16 get_default_content_model, get_default_content_model_for_blog)
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 from piecrust.cache import NullCache
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 from piecrust.configuration import (
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
19 Configuration, ConfigurationError, ConfigurationLoader,
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
20 try_get_dict_values, set_dict_value,
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
21 merge_dicts, visit_dict)
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 from piecrust.sources.base import REALM_USER, REALM_THEME
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 logger = logging.getLogger(__name__)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
28 class InvalidConfigurationPathError(Exception):
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
29 pass
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
30
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
31
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 class VariantNotFoundError(Exception):
666
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
33 def __init__(self, variant_name, message=None):
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 super(VariantNotFoundError, self).__init__(
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
35 message or ("No such configuration variant: %s" %
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
36 variant_name))
666
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
37
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
38
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 class PieCrustConfiguration(Configuration):
681
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
40 def __init__(self, *, path=None, theme_path=None, values=None,
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
41 cache=None, validate=True, theme_config=False):
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
42 if theme_config and theme_path:
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
43 raise Exception("Can't be a theme site config and still have a "
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
44 "theme applied.")
663
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 585
diff changeset
45 super(PieCrustConfiguration, self).__init__()
681
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
46 self._path = path
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
47 self._theme_path = theme_path
666
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
48 self._cache = cache or NullCache()
948
6c445771a8dc internal: Fix caching issues with config variants.
Ludovic Chabant <ludovic@chabant.com>
parents: 887
diff changeset
49 self._cache_hash_mod = ''
681
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
50 self._custom_paths = []
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
51 self._post_fixups = []
663
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 585
diff changeset
52 self.theme_config = theme_config
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 585
diff changeset
53 # Set the values after we set the rest, since our validation needs
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 585
diff changeset
54 # our attributes.
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
55 if values is not None:
663
3ceeca7bb71c themes: Add support for a `--theme` argument to `chef`.
Ludovic Chabant <ludovic@chabant.com>
parents: 585
diff changeset
56 self.setAll(values, validate=validate)
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57
681
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
58 def addPath(self, p):
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
59 if not p:
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
60 raise InvalidConfigurationPathError()
666
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
61 self._ensureNotLoaded()
681
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
62 self._custom_paths.append(p)
666
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
63
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
64 def addVariant(self, variant_path, raise_if_not_found=True):
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
65 self._ensureNotLoaded()
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
66 if os.path.isfile(variant_path):
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
67 self.addPath(variant_path)
681
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
68 elif raise_if_not_found:
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
69 logger.error(
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
70 "Configuration variants should now be `.yml` files "
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
71 "located in the `configs/` directory of your website.")
681
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
72 raise VariantNotFoundError(variant_path)
666
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
73
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
74 def addVariantValue(self, path, value):
681
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
75 def _fixup(config):
666
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
76 set_dict_value(config, path, value)
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
77
681
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
78 self._post_fixups.append(_fixup)
948
6c445771a8dc internal: Fix caching issues with config variants.
Ludovic Chabant <ludovic@chabant.com>
parents: 887
diff changeset
79 self._cache_hash_mod += '&val[%s=%s]' % (path, repr(value))
666
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
80
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
81 def setAll(self, values, validate=False):
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
82 # Override base class implementation
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
83 values = self._processConfigs({}, values)
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
84 if validate:
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
85 values = self._validateAll(values)
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
86 self._values = values
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
87
666
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
88 def _ensureNotLoaded(self):
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
89 if self._values is not None:
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
90 raise Exception("The configurations has been loaded.")
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 def _load(self):
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
93 # Figure out where to load this configuration from.
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
94 paths = []
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
95 if self._theme_path:
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
96 paths.append(self._theme_path)
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
97 if self._path:
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
98 paths.append(self._path)
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
99 paths += self._custom_paths
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
100
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
101 # Build the cache-key.
686
1a6c4c2683fd internal: Fix incorrect check for cache times.
Ludovic Chabant <ludovic@chabant.com>
parents: 683
diff changeset
102 path_times = [os.path.getmtime(p) for p in paths]
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 cache_key_hash = hashlib.md5(
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
104 ("version=%s&cache=%d" % (
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
105 APP_VERSION, CACHE_VERSION)).encode('utf8'))
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
106 for p in paths:
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 cache_key_hash.update(("&path=%s" % p).encode('utf8'))
948
6c445771a8dc internal: Fix caching issues with config variants.
Ludovic Chabant <ludovic@chabant.com>
parents: 887
diff changeset
108 if self._cache_hash_mod:
6c445771a8dc internal: Fix caching issues with config variants.
Ludovic Chabant <ludovic@chabant.com>
parents: 887
diff changeset
109 cache_key_hash.update(self._cache_hash_mod.encode('utf8'))
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 cache_key = cache_key_hash.hexdigest()
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
112 # Check the cache for a valid version.
839
b8f089092281 bug: Fix crashes for commands run outside of a website.
Ludovic Chabant <ludovic@chabant.com>
parents: 805
diff changeset
113 if path_times and self._cache.isValid('config.json', path_times):
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 logger.debug("Loading configuration from cache...")
666
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
115 config_text = self._cache.read('config.json')
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116 self._values = json.loads(
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
117 config_text,
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
118 object_pairs_hook=collections.OrderedDict)
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 actual_cache_key = self._values.get('__cache_key')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 if actual_cache_key == cache_key:
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
122 # The cached version has the same key! Awesome!
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123 self._values['__cache_valid'] = True
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 return
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125 logger.debug("Outdated cache key '%s' (expected '%s')." % (
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
126 actual_cache_key, cache_key))
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
128 # Nope, load from the paths.
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
129 try:
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
130 # Theme values.
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
131 theme_values = None
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
132 if self._theme_path:
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
133 logger.debug("Loading theme layer from: %s" % self._theme_path)
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
134 theme_values = self._loadFrom(self._theme_path)
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
135
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
136 # Site and variant values.
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
137 site_paths = []
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
138 if self._path:
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
139 site_paths.append(self._path)
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
140 site_paths += self._custom_paths
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
141
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
142 site_values = {}
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
143 for path in site_paths:
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
144 logger.debug("Loading config layer from: %s" % path)
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
145 cur_values = self._loadFrom(path)
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
146 merge_dicts(site_values, cur_values)
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
147
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
148 # Do it!
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
149 values = self._processConfigs(theme_values, site_values)
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
150 self._values = self._validateAll(values)
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
151 except Exception as ex:
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 686
diff changeset
152 logger.exception(ex)
681
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
153 raise Exception(
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
154 "Error loading configuration from: %s" %
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
155 ', '.join(paths)) from ex
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
156
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
157 logger.debug("Caching configuration...")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
158 self._values['__cache_key'] = cache_key
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
159 config_text = json.dumps(self._values)
666
81d9c3a3a0b5 internal: Get rid of the whole "sub cache" business.
Ludovic Chabant <ludovic@chabant.com>
parents: 663
diff changeset
160 self._cache.write('config.json', config_text)
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
161
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
162 self._values['__cache_valid'] = False
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
163
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
164 def _loadFrom(self, path):
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
165 logger.debug("Loading configuration from: %s" % path)
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
166 with open(path, 'r', encoding='utf-8') as fp:
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
167 values = yaml.load(
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
168 fp.read(),
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
169 Loader=ConfigurationLoader)
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
170 if values is None:
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
171 values = {}
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
172 return values
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
173
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
174 def _processConfigs(self, theme_values, site_values):
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
175 # Start with the default configuration.
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
176 values = copy.deepcopy(default_configuration)
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
177
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
178 # If we have a theme, apply the theme on that. So stuff like routes
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
179 # will now look like:
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
180 # [custom theme] + [default theme] + [default]
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
181 if theme_values is not None:
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
182 self._processThemeLayer(theme_values, values)
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
183
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
184 # Make all sources belong to the "theme" realm at this point.
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
185 srcc = values['site'].get('sources')
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
186 if srcc:
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
187 for sn, sc in srcc.items():
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
188 sc['realm'] = REALM_THEME
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
189
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
190 # Now we apply the site stuff. We want to end up with:
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
191 # [custom site] + [default site] + [custom theme] + [default theme] +
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
192 # [default]
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
193 if site_values is not None:
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
194 self._processSiteLayer(site_values, values)
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
195
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
196 # Set the theme site flag.
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
197 if self.theme_config:
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
198 values['site']['theme_site'] = True
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
199
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
200 # Run final fixups
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
201 if self._post_fixups:
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
202 logger.debug("Applying %d configuration fixups." %
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
203 len(self._post_fixups))
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
204 for f in self._post_fixups:
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
205 f(values)
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
206
683
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
207 return values
ec384174b8b2 internal: More work/fixes on how default/theme/user configs are merged.
Ludovic Chabant <ludovic@chabant.com>
parents: 681
diff changeset
208
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
209 def _processThemeLayer(self, theme_values, values):
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
210 # Generate the default theme model.
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
211 gen_default_theme_model = bool(try_get_dict_values(
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
212 (theme_values, 'site/use_default_theme_content'),
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
213 default=True))
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
214 if gen_default_theme_model:
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
215 logger.debug("Generating default theme content model...")
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
216 cc = copy.deepcopy(default_theme_content_model_base)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
217 merge_dicts(values, cc)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
218
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
219 # Merge the theme config into the result config.
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
220 merge_dicts(values, theme_values)
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
221
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
222 def _processSiteLayer(self, site_values, values):
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
223 # Default site content.
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
224 gen_default_site_model = bool(try_get_dict_values(
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
225 (site_values, 'site/use_default_content'),
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
226 (values, 'site/use_default_content'),
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
227 default=True))
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
228 if gen_default_site_model:
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
229 logger.debug("Generating default content model...")
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
230 cc = copy.deepcopy(default_content_model_base)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
231 merge_dicts(values, cc)
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
232
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
233 dcm = get_default_content_model(site_values, values)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
234 merge_dicts(values, dcm)
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
235
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
236 blogsc = try_get_dict_values(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
237 (site_values, 'site/blogs'),
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
238 (values, 'site/blogs'))
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
239 if blogsc is None:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
240 blogsc = ['posts']
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
241 set_dict_value(site_values, 'site/blogs', blogsc)
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
242
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
243 is_only_blog = (len(blogsc) == 1)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
244 for blog_name in reversed(blogsc):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
245 blog_cfg = get_default_content_model_for_blog(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
246 blog_name, is_only_blog, site_values, values,
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
247 theme_site=self.theme_config)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
248 merge_dicts(values, blog_cfg)
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
249
1150
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
250 for route in dcm['site']['routes']:
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
251 values
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
252
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
253 # Merge the site config into the result config.
1150
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
254 _merge_route_configs(values, site_values)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
255 merge_dicts(values, site_values)
805
fd694f1297c7 config: Cleanup config loading code. Add support for a `local.yml` config.
Ludovic Chabant <ludovic@chabant.com>
parents: 792
diff changeset
256
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
257 def _validateAll(self, values):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
258 if values is None:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
259 values = {}
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
260
681
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
261 # Add a section for our cached information, and start visiting
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
262 # the configuration tree, calling validation functions as we
894d286b348f internal: Refactor config loading some more.
Ludovic Chabant <ludovic@chabant.com>
parents: 675
diff changeset
263 # find them.
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
264 cachec = collections.OrderedDict()
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
265 values['__cache'] = cachec
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
266 cache_writer = _ConfigCacheWriter(cachec)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
267 globs = globals()
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
268
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
269 def _visitor(path, val, parent_val, parent_key):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
270 callback_name = '_validate_' + path.replace('/', '_')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
271 callback = globs.get(callback_name)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
272 if callback:
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
273 try:
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
274 val2 = callback(val, values, cache_writer)
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
275 except Exception as ex:
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 686
diff changeset
276 logger.exception(ex)
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
277 raise Exception("Error raised in validator '%s'." %
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
278 callback_name) from ex
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
279 if val2 is None:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
280 raise Exception("Validator '%s' isn't returning a "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
281 "coerced value." % callback_name)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
282 parent_val[parent_key] = val2
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
283
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
284 visit_dict(values, _visitor)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
285
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
286 return values
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
287
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
288
1150
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
289 def _merge_route_configs(values, from_default):
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
290 actual_routes = values.get('site', {}).get('routes', [])
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
291 default_routes = from_default.get('site', {}).get('routes', [])
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
292 for dr in list(default_routes): # copy because we'll trim it as we go.
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
293 ar = next((i for i in actual_routes
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
294 if i.get('source') == dr['source']),
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
295 None)
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
296 if ar is not None:
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
297 merge_dicts(ar, dr)
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
298 default_routes.remove(dr)
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
299
97b1b46cc156 config: Allow tweaking the configuration of default routes.
Ludovic Chabant <ludovic@chabant.com>
parents: 1098
diff changeset
300
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
301 class _ConfigCacheWriter(object):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
302 def __init__(self, cache_dict):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
303 self._cache_dict = cache_dict
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
304
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
305 def write(self, name, val):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
306 logger.debug("Caching configuration item '%s' = %s" % (name, val))
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
307 self._cache_dict[name] = val
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
308
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
309
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
310 # Configuration value validators.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
311 #
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
312 # Make sure we have basic site stuff.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
313 def _validate_site(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
314 sources = v.get('sources')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
315 if not sources:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
316 raise ConfigurationError("No sources were defined.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
317 routes = v.get('routes')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
318 if not routes:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
319 raise ConfigurationError("No routes were defined.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
320 taxonomies = v.get('taxonomies')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
321 if taxonomies is None:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
322 v['taxonomies'] = {}
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
323 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
324
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 686
diff changeset
325
752
7dbddfed8129 config: Fix how we parse the root URL to allow for absolute and user URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 743
diff changeset
326 # Make sure the site root ends with a slash.
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
327 def _validate_site_root(v, values, cache):
752
7dbddfed8129 config: Fix how we parse the root URL to allow for absolute and user URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 743
diff changeset
328 url_bits = urllib.parse.urlparse(v)
7dbddfed8129 config: Fix how we parse the root URL to allow for absolute and user URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 743
diff changeset
329 if url_bits.params or url_bits.query or url_bits.fragment:
7dbddfed8129 config: Fix how we parse the root URL to allow for absolute and user URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 743
diff changeset
330 raise ConfigurationError("Root URL is invalid: %s" % v)
7dbddfed8129 config: Fix how we parse the root URL to allow for absolute and user URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 743
diff changeset
331
7dbddfed8129 config: Fix how we parse the root URL to allow for absolute and user URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 743
diff changeset
332 path = url_bits.path.rstrip('/') + '/'
7dbddfed8129 config: Fix how we parse the root URL to allow for absolute and user URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 743
diff changeset
333 if '%' not in path:
7dbddfed8129 config: Fix how we parse the root URL to allow for absolute and user URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 743
diff changeset
334 path = urllib.parse.quote(path)
7dbddfed8129 config: Fix how we parse the root URL to allow for absolute and user URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 743
diff changeset
335
7dbddfed8129 config: Fix how we parse the root URL to allow for absolute and user URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 743
diff changeset
336 root_url = urllib.parse.urlunparse((
7dbddfed8129 config: Fix how we parse the root URL to allow for absolute and user URLs.
Ludovic Chabant <ludovic@chabant.com>
parents: 743
diff changeset
337 url_bits.scheme, url_bits.netloc, path, '', '', ''))
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
338 return root_url
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
339
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
340
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
341 # Cache auto-format regexes, check that `.html` is in there.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
342 def _validate_site_auto_formats(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
343 if not isinstance(v, dict):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
344 raise ConfigurationError("The 'site/auto_formats' setting must be "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
345 "a dictionary.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
346
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
347 v.setdefault('html', values['site']['default_format'])
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
348 auto_formats_re = r"\.(%s)$" % (
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
349 '|'.join(
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
350 [re.escape(i) for i in list(v.keys())]))
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
351 cache.write('auto_formats_re', auto_formats_re)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
352 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
353
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
354
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
355 # Check that the default auto-format is known.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
356 def _validate_site_default_auto_format(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
357 if v not in values['site']['auto_formats']:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
358 raise ConfigurationError(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
359 "Default auto-format '%s' is not declared." % v)
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
360 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
361
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
362
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
363 # Cache pagination suffix regex and format.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
364 def _validate_site_pagination_suffix(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
365 if len(v) == 0 or v[0] != '/':
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
366 raise ConfigurationError("The 'site/pagination_suffix' setting "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
367 "must start with a slash.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
368 if '%num%' not in v:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
369 raise ConfigurationError("The 'site/pagination_suffix' setting "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
370 "must contain the '%num%' placeholder.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
371
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
372 pgn_suffix_fmt = v.replace('%num%', '%(num)d')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
373 cache.write('pagination_suffix_format', pgn_suffix_fmt)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
374
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
375 pgn_suffix_re = re.escape(v)
1168
10520472cc73 routing: Fix breakages with routing on some versions of Python.
Ludovic Chabant <ludovic@chabant.com>
parents: 1150
diff changeset
376 escaped_token = "%num%" if sys.hexversion >= 0x3070000 else "\\%num\\%"
10520472cc73 routing: Fix breakages with routing on some versions of Python.
Ludovic Chabant <ludovic@chabant.com>
parents: 1150
diff changeset
377 pgn_suffix_re = (pgn_suffix_re.replace(escaped_token, "(?P<num>\\d+)") +
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
378 '$')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
379 cache.write('pagination_suffix_re', pgn_suffix_re)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
380 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
381
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
382
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
383 # Make sure theme sources is a list.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
384 def _validate_site_theme_sources(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
385 if not isinstance(v, list):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
386 v = [v]
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
387 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
388
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
389
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
390 def _validate_site_sources(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
391 # Basic checks.
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
392 if not v:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
393 raise ConfigurationError("There are no sources defined.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
394 if not isinstance(v, dict):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
395 raise ConfigurationError("The 'site/sources' setting must be a "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
396 "dictionary.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
397
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
398 # Sources have the `default` scanner by default, duh. Also, a bunch
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
399 # of other default values for other configuration stuff.
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
400 reserved_endpoints = set(['piecrust', 'site', 'page', 'route',
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
401 'assets', 'pagination', 'siblings',
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
402 'family'])
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
403 for sn, sc in v.items():
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
404 if not isinstance(sc, dict):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
405 raise ConfigurationError("All sources in 'site/sources' must "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
406 "be dictionaries.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
407 sc.setdefault('type', 'default')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
408 sc.setdefault('fs_endpoint', sn)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
409 sc.setdefault('ignore_missing_dir', False)
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
410 sc.setdefault('data_endpoint', None)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
411 sc.setdefault('data_type', None)
979
45ad976712ec tests: Big push to get the tests to pass again.
Ludovic Chabant <ludovic@chabant.com>
parents: 948
diff changeset
412 sc.setdefault('default_layout', 'default')
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
413 sc.setdefault('item_name', sn)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
414 sc.setdefault('items_per_page', 5)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
415 sc.setdefault('date_format', DEFAULT_DATE_FORMAT)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
416 sc.setdefault('realm', REALM_USER)
854
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
417 sc.setdefault('pipeline', None)
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
418
585
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
419 # Validate endpoints.
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
420 endpoint = sc['data_endpoint']
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
421 if endpoint in reserved_endpoints:
25df894f9ab9 internal: Some fixes to the new app configuration.
Ludovic Chabant <ludovic@chabant.com>
parents: 584
diff changeset
422 raise ConfigurationError(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
423 "Source '%s' is using a reserved endpoint name: %s" %
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
424 (sn, endpoint))
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 686
diff changeset
425
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
426 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
427
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
428
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
429 def _validate_site_routes(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
430 if not v:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
431 raise ConfigurationError("There are no routes defined.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
432 if not isinstance(v, list):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
433 raise ConfigurationError("The 'site/routes' setting must be a "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
434 "list.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
435
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
436 # Check routes are referencing correct sources, have default
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
437 # values, etc.
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
438 used_sources = set()
1098
2323f0788170 config: Report error if a non-asset source has no URL route.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
439 source_configs = values['site']['sources']
2323f0788170 config: Report error if a non-asset source has no URL route.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
440 existing_sources = set(source_configs.keys())
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
441 for rc in v:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
442 if not isinstance(rc, dict):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
443 raise ConfigurationError("All routes in 'site/routes' must be "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
444 "dictionaries.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
445 rc_url = rc.get('url')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
446 if not rc_url:
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
447 raise ConfigurationError("All routes in 'site/routes' must "
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
448 "have an 'url'.")
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
449 if rc_url[0] != '/':
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
450 raise ConfigurationError("Route URLs must start with '/'.")
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 686
diff changeset
451
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 686
diff changeset
452 r_source = rc.get('source')
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
453 if r_source is None:
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
454 raise ConfigurationError("Routes must specify a source.")
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
455 if r_source not in existing_sources:
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
456 raise ConfigurationError("Route is referencing unknown "
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 686
diff changeset
457 "source: %s" % r_source)
853
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
458 if r_source in used_sources:
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
459 raise ConfigurationError("Source '%s' already has a route." %
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
460 r_source)
f070a4fc033c core: Continue PieCrust3 refactor, simplify pages.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
461 used_sources.add(r_source)
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 686
diff changeset
462
854
08e02c2a2a1a core: Keep refactoring, this time to prepare for generator sources.
Ludovic Chabant <ludovic@chabant.com>
parents: 853
diff changeset
463 rc.setdefault('pass', 1)
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
464 rc.setdefault('page_suffix', '/%num%')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
465
1098
2323f0788170 config: Report error if a non-asset source has no URL route.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
466 # Raise errors about non-asset sources that have no URL routes.
2323f0788170 config: Report error if a non-asset source has no URL route.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
467 sources_with_no_route = list(filter(
2323f0788170 config: Report error if a non-asset source has no URL route.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
468 lambda s: source_configs[s].get('pipeline') != 'asset',
2323f0788170 config: Report error if a non-asset source has no URL route.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
469 existing_sources.difference(used_sources)))
2323f0788170 config: Report error if a non-asset source has no URL route.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
470 if sources_with_no_route:
2323f0788170 config: Report error if a non-asset source has no URL route.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
471 raise ConfigurationError(
2323f0788170 config: Report error if a non-asset source has no URL route.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
472 "The following sources have no routes: %s" %
2323f0788170 config: Report error if a non-asset source has no URL route.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
473 ', '.join(sources_with_no_route))
2323f0788170 config: Report error if a non-asset source has no URL route.
Ludovic Chabant <ludovic@chabant.com>
parents: 979
diff changeset
474
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
475 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
476
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
477
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
478 def _validate_site_taxonomies(v, values, cache):
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 686
diff changeset
479 if not isinstance(v, dict):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 686
diff changeset
480 raise ConfigurationError(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
481 "The 'site/taxonomies' setting must be a mapping.")
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
482 for tn, tc in v.items():
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
483 tc.setdefault('multiple', False)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
484 tc.setdefault('term', tn)
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
485 tc.setdefault('page', '_%s.%%ext%%' % tc['term'])
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 686
diff changeset
486 return v
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
487
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents: 686
diff changeset
488
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
489 def _validate_site_plugins(v, values, cache):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
490 if isinstance(v, str):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
491 v = v.split(',')
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
492 elif not isinstance(v, list):
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
493 raise ConfigurationError(
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
494 "The 'site/plugins' setting must be an array, or a "
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 839
diff changeset
495 "comma-separated list.")
584
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
496 return v
9ccc933ac2c7 internal: Refactor the app configuration class.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
497