forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 6
/
pylint_django_settings.py
52 lines (40 loc) · 1.86 KB
/
pylint_django_settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from pylint_django.checkers import ForeignKeyStringsChecker
from pylint_plugin_utils import get_checker
class ArgumentCompatibilityError(Exception):
pass
def _get_django_settings_module(arguments):
"""
Determines the appropriate Django settings module based on the pylint command-line arguments.
It prevents the use of cms modules alongside lms or common modules.
:param arguments: List of command-line arguments passed to pylint
:return: A string representing the correct Django settings module ('cms.envs.test' or 'lms.envs.test')
:raises ArgumentCompatibilityError: If both cms and lms/common modules are present
"""
cms_module, lms_module, common_module = False, False, False
for arg in arguments:
if arg.startswith("cms"):
cms_module = True
elif arg.startswith("lms"):
lms_module = True
elif arg.startswith("common"):
common_module = True
if cms_module and (lms_module or common_module):
# when cms module is present in pylint command, it can't be parired with (lms, common)
# as common and lms gives error with cms test settings
raise ArgumentCompatibilityError(
"Modules from both common and lms cannot be paired with cms when running pylint"
)
# Return the appropriate Django settings module based on the arguments
return "cms.envs.test" if cms_module else "lms.envs.test"
def register(linter):
"""
Placeholder function to register the plugin with pylint.
"""
pass
def load_configuration(linter):
"""
Configures the Django settings module based on the command-line arguments passed to pylint.
"""
name_checker = get_checker(linter, ForeignKeyStringsChecker)
arguments = linter.cmdline_parser.parse_args()[1]
name_checker.config.django_settings_module = _get_django_settings_module(arguments)