Coverage for C:\Repos\ekr-pylint\pylint\config\find_default_config_files.py: 55%

62 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 configparser 

8import os 

9import sys 

10import warnings 

11from collections.abc import Iterator 

12from pathlib import Path 

13 

14if sys.version_info >= (3, 11): 

15 import tomllib 

16else: 

17 import tomli as tomllib 

18 

19RC_NAMES = (Path("pylintrc"), Path(".pylintrc")) 

20CONFIG_NAMES = RC_NAMES + (Path("pyproject.toml"), Path("setup.cfg")) 

21 

22 

23def _toml_has_config(path: Path | str) -> bool: 

24 with open(path, mode="rb") as toml_handle: 

25 try: 

26 content = tomllib.load(toml_handle) 

27 except tomllib.TOMLDecodeError as error: 

28 print(f"Failed to load '{path}': {error}") 

29 return False 

30 return "pylint" in content.get("tool", []) 

31 

32 

33def _cfg_has_config(path: Path | str) -> bool: 

34 parser = configparser.ConfigParser() 

35 try: 

36 parser.read(path, encoding="utf-8") 

37 except configparser.Error: 

38 return False 

39 return any(section.startswith("pylint.") for section in parser.sections()) 

40 

41 

42def find_default_config_files() -> Iterator[Path]: 

43 """Find all possible config files.""" 

44 for config_name in CONFIG_NAMES: 

45 if config_name.is_file(): 

46 if config_name.suffix == ".toml" and not _toml_has_config(config_name): 

47 continue 

48 if config_name.suffix == ".cfg" and not _cfg_has_config(config_name): 

49 continue 

50 

51 yield config_name.resolve() 

52 

53 if Path("__init__.py").is_file(): 

54 curdir = Path(os.getcwd()).resolve() 

55 while (curdir / "__init__.py").is_file(): 

56 curdir = curdir.parent 

57 for rc_name in RC_NAMES: 

58 rc_path = curdir / rc_name 

59 if rc_path.is_file(): 

60 yield rc_path.resolve() 

61 

62 if "PYLINTRC" in os.environ and Path(os.environ["PYLINTRC"]).exists(): 

63 if Path(os.environ["PYLINTRC"]).is_file(): 

64 yield Path(os.environ["PYLINTRC"]).resolve() 

65 else: 

66 user_home = Path.home() 

67 if str(user_home) not in ("~", "/root"): 

68 home_rc = user_home / ".pylintrc" 

69 if home_rc.is_file(): 

70 yield home_rc.resolve() 

71 home_rc = user_home / ".config" / "pylintrc" 

72 if home_rc.is_file(): 

73 yield home_rc.resolve() 

74 

75 if os.path.isfile("/etc/pylintrc"): 

76 yield Path("/etc/pylintrc").resolve() 

77 

78 

79def find_pylintrc() -> str | None: 

80 """Search the pylint rc file and return its path if it finds it, else return None.""" 

81 # TODO: 3.0: Remove deprecated function 

82 warnings.warn( 

83 "find_pylintrc and the PYLINTRC constant have been deprecated. " 

84 "Use find_default_config_files if you want access to pylint's configuration file " 

85 "finding logic.", 

86 DeprecationWarning, 

87 ) 

88 for config_file in find_default_config_files(): 

89 if str(config_file).endswith("pylintrc"): 

90 return str(config_file) 

91 return None