Move --print-settings logic out of main()

This commit is contained in:
Justin Mayer 2018-10-26 12:00:40 +02:00
commit ab9b21c03d

View file

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