forked from openedx/edx-platform
-
Notifications
You must be signed in to change notification settings - Fork 14
/
pylint_django_settings.py
59 lines (47 loc) · 2.2 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
53
54
55
56
57
58
59
from pylint.interfaces import IAstroidChecker
from pylint.checkers import BaseChecker
from pylint_django.checkers import ForeignKeyStringsChecker
from pylint_plugin_utils import get_checker
class ArgumentCompatibilityError(Exception):
pass
class SetDjangoSettingsChecker(BaseChecker):
"""
This isn't a checker, but setting django settings module when pylint command is ran.
This is to avoid 'django-not-configured' pylint warning
"""
__implements__ = IAstroidChecker
name = 'set-django-settings'
msgs = {'R0991': ('bogus', 'bogus', 'bogus')}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def open(self):
name_checker = get_checker(self.linter, ForeignKeyStringsChecker)
# pylint command should not run with modules from both cms and (lms, common) at once
cms_module = False
lms_module = False
common_module = False
arguments = self.linter.cmdline_parser.parse_args()[1]
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 can't be paired with cms while running pylint"
)
elif cms_module:
# If a module from cms is present in pylint command arguments
# and ony other module from (openedx, pavelib) is present
# than test setting of cms is used
name_checker.config.django_settings_module = 'cms.envs.test'
else:
# If any module form (lms, common, openedx, pavelib) is present in
# pylint command arguments than test setting of lms is used
name_checker.config.django_settings_module = 'lms.envs.test'
def register(linter):
linter.register_checker(SetDjangoSettingsChecker(linter))