ini.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from __future__ import annotations
  2. import logging
  3. import os
  4. from configparser import ConfigParser
  5. from pathlib import Path
  6. from typing import TYPE_CHECKING, ClassVar
  7. from platformdirs import user_config_dir
  8. from .convert import convert
  9. if TYPE_CHECKING:
  10. from collections.abc import Mapping
  11. from typing import Any
  12. from .convert import TypeData
  13. LOGGER = logging.getLogger(__name__)
  14. class IniConfig:
  15. VIRTUALENV_CONFIG_FILE_ENV_VAR: ClassVar[str] = "VIRTUALENV_CONFIG_FILE"
  16. STATE: ClassVar[dict[bool | None, str]] = {None: "failed to parse", True: "active", False: "missing"}
  17. section = "virtualenv"
  18. def __init__(self, env: Mapping[str, str] | None = None) -> None:
  19. env = os.environ if env is None else env
  20. config_file = env.get(self.VIRTUALENV_CONFIG_FILE_ENV_VAR, None)
  21. self.is_env_var = config_file is not None
  22. if config_file is None:
  23. config_file = Path(user_config_dir(appname="virtualenv", appauthor="pypa")) / "virtualenv.ini"
  24. else:
  25. config_file = Path(config_file)
  26. self.config_file = config_file
  27. self._cache = {}
  28. exception = None
  29. self.has_config_file = None
  30. try:
  31. self.has_config_file = self.config_file.exists()
  32. except OSError as exc:
  33. exception = exc
  34. else:
  35. if self.has_config_file:
  36. self.config_file = self.config_file.resolve()
  37. self.config_parser = ConfigParser()
  38. try:
  39. self._load()
  40. self.has_virtualenv_section = self.config_parser.has_section(self.section)
  41. except Exception as exc: # noqa: BLE001
  42. exception = exc
  43. if exception is not None:
  44. LOGGER.error("failed to read config file %s because %r", config_file, exception)
  45. def _load(self) -> None:
  46. with self.config_file.open("rt", encoding="utf-8") as file_handler:
  47. return self.config_parser.read_file(file_handler)
  48. def get(self, key: str, as_type: TypeData) -> tuple[Any, str] | None:
  49. cache_key = key, as_type
  50. if cache_key in self._cache:
  51. return self._cache[cache_key]
  52. try:
  53. source = "file"
  54. raw_value = self.config_parser.get(self.section, key.lower())
  55. value = convert(raw_value, as_type, source)
  56. result = value, source
  57. except Exception: # noqa: BLE001
  58. result = None
  59. self._cache[cache_key] = result
  60. return result
  61. def __bool__(self) -> bool:
  62. return bool(self.has_config_file) and bool(self.has_virtualenv_section)
  63. @property
  64. def epilog(self) -> str:
  65. return (
  66. f"\nconfig file {self.config_file} {self.STATE[self.has_config_file]} "
  67. f"(change{'d' if self.is_env_var else ''} via env var {self.VIRTUALENV_CONFIG_FILE_ENV_VAR})"
  68. )