Coverage for C:\Repos\ekr-pylint\pylint\config\config_initialization.py: 20%

49 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-24 10:21 -0500

1# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html 

2# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE 

3# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt 

4 

5from __future__ import annotations 

6 

7import sys 

8from pathlib import Path 

9from typing import TYPE_CHECKING 

10 

11from pylint import reporters 

12from pylint.config.config_file_parser import _ConfigurationFileParser 

13from pylint.config.exceptions import _UnrecognizedOptionError 

14from pylint.utils import utils 

15 

16if TYPE_CHECKING: 

17 from pylint.lint import PyLinter 

18 

19 

20def _config_initialization( 

21 linter: PyLinter, 

22 args_list: list[str], 

23 reporter: reporters.BaseReporter | reporters.MultiReporter | None = None, 

24 config_file: None | str | Path = None, 

25 verbose_mode: bool = False, 

26) -> list[str]: 

27 """Parse all available options, read config files and command line arguments and 

28 set options accordingly. 

29 """ 

30 config_file = Path(config_file) if config_file else None 

31 

32 # Set the current module to the configuration file 

33 # to allow raising messages on the configuration file. 

34 linter.set_current_module(str(config_file) if config_file else None) 

35 

36 # Read the configuration file 

37 config_file_parser = _ConfigurationFileParser(verbose_mode, linter) 

38 try: 

39 config_data, config_args = config_file_parser.parse_config_file( 

40 file_path=config_file 

41 ) 

42 except OSError as ex: 

43 print(ex, file=sys.stderr) 

44 sys.exit(32) 

45 

46 # Run init hook, if present, before loading plugins 

47 if "init-hook" in config_data: 

48 exec(utils._unquote(config_data["init-hook"])) # pylint: disable=exec-used 

49 

50 # Load plugins if specified in the config file 

51 if "load-plugins" in config_data: 

52 linter.load_plugin_modules(utils._splitstrip(config_data["load-plugins"])) 

53 

54 # First we parse any options from a configuration file 

55 try: 

56 linter._parse_configuration_file(config_args) 

57 except _UnrecognizedOptionError as exc: 

58 msg = ", ".join(exc.options) 

59 linter.add_message("unrecognized-option", line=0, args=msg) 

60 

61 # Then, if a custom reporter is provided as argument, it may be overridden 

62 # by file parameters, so we re-set it here. We do this before command line 

63 # parsing, so it's still overridable by command line options 

64 if reporter: 

65 linter.set_reporter(reporter) 

66 

67 # Set the current module to the command line 

68 # to allow raising messages on it 

69 linter.set_current_module("Command line") 

70 

71 # Now we parse any options from the command line, so they can override 

72 # the configuration file 

73 parsed_args_list = linter._parse_command_line_configuration(args_list) 

74 

75 # Check if there are any options that we do not recognize 

76 unrecognized_options: list[str] = [] 

77 for opt in parsed_args_list: 

78 if opt.startswith("--"): 

79 unrecognized_options.append(opt[2:]) 

80 elif opt.startswith("-"): 

81 unrecognized_options.append(opt[1:]) 

82 if unrecognized_options: 

83 msg = ", ".join(unrecognized_options) 

84 linter._arg_parser.error(f"Unrecognized option found: {msg}") 

85 

86 # Set the current module to configuration as we don't know where 

87 # the --load-plugins key is coming from 

88 linter.set_current_module("Command line or configuration file") 

89 

90 # We have loaded configuration from config file and command line. Now, we can 

91 # load plugin specific configuration. 

92 linter.load_plugin_configuration() 

93 

94 # parsed_args_list should now only be a list of files/directories to lint. 

95 # All other options have been removed from the list. 

96 if not parsed_args_list: 

97 print(linter.help()) 

98 sys.exit(32) 

99 

100 # Now that plugins are loaded, get list of all fail_on messages, and enable them 

101 linter.enable_fail_on_messages() 

102 

103 linter._parse_error_mode() 

104 

105 return parsed_args_list