From e81a284c399befcce5594fef19f8b01e5f510490 Mon Sep 17 00:00:00 2001 From: Justin Mayer Date: Tue, 23 Oct 2018 17:57:37 +0200 Subject: [PATCH 1/3] Add CLI argument to print current settings Fixes #2275 Refs #1762 --- docs/settings.rst | 13 +++++++++---- pelican/__init__.py | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/docs/settings.rst b/docs/settings.rst index e09a90dc..9c73dc04 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -21,6 +21,12 @@ Settings are configured in the form of a Python module (a file). There is an `_ available for reference. +To see a list of current settings in your environment, including both default +and any customized values, run the following command (append a specific +setting name as an argument to see the value for that one setting):: + + pelican --print-settings + All the setting identifiers must be set in all-caps, otherwise they will not be processed. Setting values that are numbers (5, 20, etc.), booleans (True, False, None, etc.), dictionaries, or tuples should *not* be enclosed in @@ -28,10 +34,9 @@ quotation marks. All other values (i.e., strings) *must* be enclosed in quotation marks. Unless otherwise specified, settings that refer to paths can be either absolute -or relative to the configuration file. - -The settings you define in the configuration file will be passed to the -templates, which allows you to use your settings to add site-wide content. +or relative to the configuration file. The settings you define in the +configuration file will be passed to the templates, which allows you to use your +settings to add site-wide content. Here is a list of settings for Pelican: diff --git a/pelican/__init__.py b/pelican/__init__.py index c00bc591..0339681b 100644 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -11,6 +11,7 @@ import re import sys import time import traceback +from pprint import pprint import six @@ -319,6 +320,12 @@ def parse_arguments(): help='Relaunch pelican each time a modification occurs' ' on the content files.') + parser.add_argument('--print-settings', dest='print_settings', + action='append', nargs='?', metavar='SETTING_NAME', + help='Print current configuration settings and exit. ' + 'Append a setting name as argument to see the value ' + 'for that specific setting only.') + parser.add_argument('--relative-urls', dest='relative_paths', action='store_true', help='Use relative urls in output, ' @@ -527,6 +534,21 @@ def main(): try: pelican, settings = get_instance(args) + + if args.print_settings: + # If no argument was given to --print-settings, print all settings + if args.print_settings[0] is None: + pprint(settings) + # An argument was given to --print-settings, so print that setting + else: + try: + pprint(settings[args.print_settings[0]]) + except KeyError: + print("{} is not a recognized setting.".format( + args.print_settings[0])) + return 1 + return 0 + readers = Readers(settings) reader_descs = sorted(set(['%s (%s)' % (type(r).__name__, From f130695bbf8572b38d314c8c30073fb383bb9790 Mon Sep 17 00:00:00 2001 From: Justin Mayer Date: Wed, 24 Oct 2018 11:49:03 +0200 Subject: [PATCH 2/3] Support multiple --print-settings arguments --- docs/settings.rst | 4 ++-- pelican/__init__.py | 26 ++++++++++++++------------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/docs/settings.rst b/docs/settings.rst index 9c73dc04..26f0a233 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -22,8 +22,8 @@ Settings are configured in the form of a Python module (a file). There is an available for reference. To see a list of current settings in your environment, including both default -and any customized values, run the following command (append a specific -setting name as an argument to see the value for that one setting):: +and any customized values, run the following command (append one or more +specific setting names as arguments to see values for those settings only):: pelican --print-settings diff --git a/pelican/__init__.py b/pelican/__init__.py index 0339681b..48dc6dff 100644 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -320,11 +320,11 @@ def parse_arguments(): help='Relaunch pelican each time a modification occurs' ' on the content files.') - parser.add_argument('--print-settings', dest='print_settings', - action='append', nargs='?', metavar='SETTING_NAME', + parser.add_argument('--print-settings', dest='print_settings', nargs='*', + action='store', metavar='SETTING_NAME(S)', help='Print current configuration settings and exit. ' - 'Append a setting name as argument to see the value ' - 'for that specific setting only.') + 'Append one or more setting name arguments to see the ' + 'values for specific settings only.') parser.add_argument('--relative-urls', dest='relative_paths', action='store_true', @@ -535,18 +535,20 @@ def main(): try: pelican, settings = get_instance(args) - if args.print_settings: + if args.print_settings != None: # If no argument was given to --print-settings, print all settings - if args.print_settings[0] is None: + if args.print_settings == []: pprint(settings) # An argument was given to --print-settings, so print that setting else: - try: - pprint(settings[args.print_settings[0]]) - except KeyError: - print("{} is not a recognized setting.".format( - args.print_settings[0])) - return 1 + for setting in args.print_settings: + try: + setting_value = settings[setting] + print("\n{}: ".format(setting)) + pprint(setting_value) + except KeyError: + print("\n{} is not a recognized setting.".format(setting)) + return 1 return 0 readers = Readers(settings) From ab9b21c03d3c4faccb30136127a05126ca50fb92 Mon Sep 17 00:00:00 2001 From: Justin Mayer Date: Fri, 26 Oct 2018 12:00:40 +0200 Subject: [PATCH 3/3] Move --print-settings logic out of main() --- pelican/__init__.py | 46 +++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/pelican/__init__.py b/pelican/__init__.py index 48dc6dff..83b2c169 100644 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -7,11 +7,11 @@ import locale import logging import multiprocessing import os +import pprint import re import sys import time import traceback -from pprint import pprint import six @@ -270,6 +270,32 @@ class Pelican(object): return writer(self.output_path, settings=self.settings) +class PrintSettings(argparse.Action): + def __call__(self, parser, namespace, values, option_string): + instance, settings = get_instance(namespace) + + if values: + # One or more arguments provided, so only print those settings + for setting in values: + if setting in settings: + # Only add newline between setting name and value if dict + if isinstance(settings[setting], dict): + setting_format = '\n{}:\n{}' + else: + setting_format = '\n{}: {}' + print(setting_format.format( + setting, + pprint.pformat(settings[setting]))) + else: + print('\n{} is not a recognized setting.'.format(setting)) + break + else: + # No argument was given to --print-settings, so print all settings + pprint.pprint(settings) + + parser.exit() + + def parse_arguments(): parser = argparse.ArgumentParser( description='A tool to generate a static blog, ' @@ -321,7 +347,7 @@ def parse_arguments(): ' on the content files.') parser.add_argument('--print-settings', dest='print_settings', nargs='*', - action='store', metavar='SETTING_NAME(S)', + action=PrintSettings, metavar='SETTING_NAME', help='Print current configuration settings and exit. ' 'Append one or more setting name arguments to see the ' 'values for specific settings only.') @@ -535,22 +561,6 @@ def main(): try: pelican, settings = get_instance(args) - if args.print_settings != None: - # If no argument was given to --print-settings, print all settings - if args.print_settings == []: - pprint(settings) - # An argument was given to --print-settings, so print that setting - else: - for setting in args.print_settings: - try: - setting_value = settings[setting] - print("\n{}: ".format(setting)) - pprint(setting_value) - except KeyError: - print("\n{} is not a recognized setting.".format(setting)) - return 1 - return 0 - readers = Readers(settings) reader_descs = sorted(set(['%s (%s)' % (type(r).__name__,