| 1 |
from django.core.management.base import BaseCommand |
| 2 |
|
| 3 |
|
| 4 |
def module_to_dict(module, omittable=lambda k: k.startswith("_") or not k.isupper()): |
| 5 |
"""Convert a module namespace to a Python dictionary.""" |
| 6 |
return {k: repr(getattr(module, k)) for k in dir(module) if not omittable(k)} |
| 7 |
|
| 8 |
|
| 9 |
class Command(BaseCommand): |
| 10 |
help = """Displays differences between the current settings.py and Django's |
| 11 |
default settings.""" |
| 12 |
|
| 13 |
requires_system_checks = [] |
| 14 |
|
| 15 |
def add_arguments(self, parser): |
| 16 |
parser.add_argument( |
| 17 |
"--all", |
| 18 |
action="store_true", |
| 19 |
help=( |
| 20 |
'Display all settings, regardless of their value. In "hash" ' |
| 21 |
'mode, default values are prefixed by "###".' |
| 22 |
), |
| 23 |
) |
| 24 |
parser.add_argument( |
| 25 |
"--default", |
| 26 |
metavar="MODULE", |
| 27 |
help=( |
| 28 |
"The settings module to compare the current settings against. Leave " |
| 29 |
"empty to compare against Django's default settings." |
| 30 |
), |
| 31 |
) |
| 32 |
parser.add_argument( |
| 33 |
"--output", |
| 34 |
default="hash", |
| 35 |
choices=("hash", "unified"), |
| 36 |
help=( |
| 37 |
"Selects the output format. 'hash' mode displays each changed " |
| 38 |
"setting, with the settings that don't appear in the defaults " |
| 39 |
"followed by ###. 'unified' mode prefixes the default setting " |
| 40 |
"with a minus sign, followed by the changed setting prefixed " |
| 41 |
"with a plus sign." |
| 42 |
), |
| 43 |
) |
| 44 |
|
| 45 |
def handle(self, **options): |
| 46 |
from django.conf import Settings, global_settings, settings |
| 47 |
|
| 48 |
# Because settings are imported lazily, we need to explicitly load them. |
| 49 |
if not settings.configured: |
| 50 |
settings._setup() |
| 51 |
|
| 52 |
user_settings = module_to_dict(settings._wrapped) |
| 53 |
default = options["default"] |
| 54 |
default_settings = module_to_dict( |
| 55 |
Settings(default) if default else global_settings |
| 56 |
) |
| 57 |
output_func = { |
| 58 |
"hash": self.output_hash, |
| 59 |
"unified": self.output_unified, |
| 60 |
}[options["output"]] |
| 61 |
return "\n".join(output_func(user_settings, default_settings, **options)) |
| 62 |
|
| 63 |
def output_hash(self, user_settings, default_settings, **options): |
| 64 |
# Inspired by Postfix's "postconf -n". |
| 65 |
output = [] |
| 66 |
for key in sorted(user_settings): |
| 67 |
if key not in default_settings: |
| 68 |
output.append("%s = %s ###" % (key, user_settings[key])) |
| 69 |
elif user_settings[key] != default_settings[key]: |
| 70 |
output.append("%s = %s" % (key, user_settings[key])) |
| 71 |
elif options["all"]: |
| 72 |
output.append("### %s = %s" % (key, user_settings[key])) |
| 73 |
return output |
| 74 |
|
| 75 |
def output_unified(self, user_settings, default_settings, **options): |
| 76 |
output = [] |
| 77 |
for key in sorted(user_settings): |
| 78 |
if key not in default_settings: |
| 79 |
output.append( |
| 80 |
self.style.SUCCESS("+ %s = %s" % (key, user_settings[key])) |
| 81 |
) |
| 82 |
elif user_settings[key] != default_settings[key]: |
| 83 |
output.append( |
| 84 |
self.style.ERROR("- %s = %s" % (key, default_settings[key])) |
| 85 |
) |
| 86 |
output.append( |
| 87 |
self.style.SUCCESS("+ %s = %s" % (key, user_settings[key])) |
| 88 |
) |
| 89 |
elif options["all"]: |
| 90 |
output.append(" %s = %s" % (key, user_settings[key])) |
| 91 |
return output |