You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

diffsettings.py 3.3KB

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