ini.py 2.6 KB

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