mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Override settings from the command line
Add a --setting-overrides KEY=VAL command line option to override default settings or those defined in settings files. This adds flexibility in running Pelican and helps reduce sprawl of settings files. Cast int and str setting overrides to their respective types. Support other setting types by treating them as JSON. Fall back to JSON when an override typecast errors. This should make it possible to set int values to None, resp. to JSON 'none'
This commit is contained in:
parent
f80bf187a9
commit
1c50a18d0a
3 changed files with 64 additions and 3 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import copy
|
||||
import importlib.util
|
||||
import inspect
|
||||
import json
|
||||
import locale
|
||||
import logging
|
||||
import os
|
||||
|
|
@ -658,3 +659,23 @@ def configure_settings(settings):
|
|||
continue # setting not specified, nothing to do
|
||||
|
||||
return settings
|
||||
|
||||
|
||||
def coerce_overrides(overrides):
|
||||
coerced = {}
|
||||
types_to_cast = {int, str}
|
||||
for k, v in overrides.items():
|
||||
if k not in overrides:
|
||||
logger.warning('Override for unknown setting %s, ignoring', k)
|
||||
continue
|
||||
setting_type = type(DEFAULT_CONFIG[k])
|
||||
if setting_type not in types_to_cast:
|
||||
coerced[k] = json.loads(v)
|
||||
else:
|
||||
try:
|
||||
coerced[k] = setting_type(v)
|
||||
except ValueError:
|
||||
logger.debug('ValueError for %s override with %s, try to '
|
||||
'load as json', k, v)
|
||||
coerced[k] = json.loads(v)
|
||||
return coerced
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue