Coverage for src/pytest_vulture/conf/reader.py: 0.00%

1"""The full configuration manager."""

2

3 import sys

4 from configparser import ConfigParser

5 from pathlib import Path

6

7

8 if sys.version_info >= (3, 11): # pragma: no cover

9 import tomllib

10 else: # pragma: no cover

11 import tomli as tomllib

12

13 from pytest_vulture.conf.package import PackageConfiguration

14 from pytest_vulture.conf.vulture import VultureConfiguration

15

16

17 class IniReader:

18 """The full configuration parser

19 Examples::

20 >>> config_file = Path("/tmp/test.ini")

21 >>> config_file.write_text("[package]\\nsetup_path = custom.py\\n[vulture]\\nignore-names = test")

22 62

23 >>> package_config = IniReader(Path("/tmp/test.ini"))

24 >>> package_config.read_ini()

25 >>> package_config.package_configuration.setup_path

26 PosixPath('custom.py')

27 >>> package_config.vulture_configuration.ignore_names

28 ['test']

29 """

30

31 _is_pyproject: bool

32

33 _vulture_configuration: VultureConfiguration

34 _package_configuration: PackageConfiguration

35 _path_ini: Path

36

37 def __init__(self, path_ini: Path = Path("vulture.ini"), *, is_pyproject: bool = False) -> None:

38 self._path_ini = path_ini

39 self._vulture_configuration = VultureConfiguration(is_pyproject=is_pyproject)

40 self._package_configuration = PackageConfiguration(is_pyproject=is_pyproject)

41 self._is_pyproject = is_pyproject

42

43 @property

44 def package_configuration(self) -> PackageConfiguration:

45 return self._package_configuration

46

47 @property

48 def vulture_configuration(self) -> VultureConfiguration:

49 return self._vulture_configuration

50

51 def read_ini(self) -> None:

52 """Read and parse the ini file."""

53 if self._path_ini.is_file():

54 if self._is_pyproject:

55 with self._path_ini.open("rb") as file:

56 pyproject_data = tomllib.load(file)

57 self._vulture_configuration.read_tomli(pyproject_data)

58 self._package_configuration.setup_path = self._path_ini

59 self._package_configuration.read_tomli(pyproject_data)

60

61 else:

62 config = ConfigParser()

63 config.read(self._path_ini)

64 self._vulture_configuration.read_ini(config)

65 self._package_configuration.read_ini(config)