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:
Peter Sabaini 2020-05-09 18:15:16 +02:00 committed by Justin Mayer
commit 1c50a18d0a
3 changed files with 64 additions and 3 deletions

View file

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