|
1 | 1 | from __future__ import absolute_import
|
2 | 2 |
|
| 3 | +import os |
| 4 | + |
3 | 5 | import astroid
|
| 6 | +from configurations import importer |
4 | 7 | from pylint.checkers import BaseChecker
|
5 | 8 | from pylint.checkers.utils import check_messages
|
6 | 9 | from pylint.interfaces import IAstroidChecker
|
@@ -41,6 +44,15 @@ class ForeignKeyStringsChecker(BaseChecker):
|
41 | 44 | "help": "A module containing Django settings to be used while linting.",
|
42 | 45 | },
|
43 | 46 | ),
|
| 47 | + ( |
| 48 | + "django-configuration", |
| 49 | + { |
| 50 | + "default": None, |
| 51 | + "type": "string", |
| 52 | + "metavar": "<django configuration>", |
| 53 | + "help": "The configuration for Django to use while linting.", |
| 54 | + }, |
| 55 | + ), |
44 | 56 | )
|
45 | 57 |
|
46 | 58 | msgs = {
|
@@ -89,6 +101,32 @@ def open(self):
|
89 | 101 | try:
|
90 | 102 | import django # pylint: disable=import-outside-toplevel
|
91 | 103 |
|
| 104 | + if ( |
| 105 | + os.environ.get("DJANGO_SETTINGS_MODULE") is None |
| 106 | + or os.environ.get("DJANGO_CONFIGURATION") is None |
| 107 | + ): |
| 108 | + try: |
| 109 | + os.environ.setdefault( |
| 110 | + "DJANGO_SETTINGS_MODULE", |
| 111 | + os.environ.get("DJANGO_SETTINGS_MODULE") |
| 112 | + or self.config.django_settings_module, |
| 113 | + ) |
| 114 | + os.environ.setdefault( |
| 115 | + "DJANGO_CONFIGURATION", |
| 116 | + os.environ.get("DJANGO_CONFIGURATION") |
| 117 | + or self.config.django_configuration, |
| 118 | + ) |
| 119 | + except TypeError as ex: |
| 120 | + missing_module = "" |
| 121 | + if self.config.django_settings_module is None: |
| 122 | + missing_module = "DJANGO_SETTINGS_MODULE" |
| 123 | + else: |
| 124 | + missing_module = "DJANGO_CONFIGURATION" |
| 125 | + raise RuntimeError( |
| 126 | + f"{missing_module} required to initialize Django project settings" |
| 127 | + ) from ex |
| 128 | + importer.install() |
| 129 | + |
92 | 130 | django.setup()
|
93 | 131 | from django.apps import ( # noqa pylint: disable=import-outside-toplevel,unused-import
|
94 | 132 | apps,
|
|
0 commit comments