pyenv_cfg.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from __future__ import annotations
  2. import logging
  3. import os
  4. from collections import OrderedDict
  5. from typing import TYPE_CHECKING
  6. if TYPE_CHECKING:
  7. from pathlib import Path
  8. LOGGER = logging.getLogger(__name__)
  9. class PyEnvCfg:
  10. def __init__(self, content: OrderedDict[str, str], path: Path) -> None:
  11. self.content = content
  12. self.path = path
  13. @classmethod
  14. def from_folder(cls, folder: Path) -> PyEnvCfg:
  15. return cls.from_file(folder / "pyvenv.cfg")
  16. @classmethod
  17. def from_file(cls, path: Path) -> PyEnvCfg:
  18. content = cls._read_values(path) if path.exists() else OrderedDict()
  19. return PyEnvCfg(content, path)
  20. @staticmethod
  21. def _read_values(path: Path) -> OrderedDict[str, str]:
  22. content = OrderedDict()
  23. for line in path.read_text(encoding="utf-8").splitlines():
  24. equals_at = line.index("=")
  25. key = line[:equals_at].strip()
  26. value = line[equals_at + 1 :].strip()
  27. if len(value) > 1 and value[0] in {"'", '"'} and value[0] == value[-1]:
  28. value = value[1:-1]
  29. content[key] = value
  30. return content
  31. def write(self) -> None:
  32. LOGGER.debug("write %s", self.path)
  33. text = ""
  34. for key, value in self.content.items():
  35. # Use abspath to normalize relative paths but preserve symlinks (match venv behavior)
  36. # See issue #2770 - realpath resolves symlinks which breaks prefix symlinks
  37. if key == "prompt" and value:
  38. normalized_value = f'"{value}"'
  39. else:
  40. normalized_value = os.path.abspath(value) if value and os.path.exists(value) else value
  41. line = f"{key} = {normalized_value}"
  42. LOGGER.debug("\t%s", line)
  43. text += line
  44. text += "\n"
  45. self.path.write_text(text, encoding="utf-8")
  46. def refresh(self) -> OrderedDict[str, str]:
  47. self.content = self._read_values(self.path)
  48. return self.content
  49. def __setitem__(self, key: str, value: str) -> None:
  50. self.content[key] = value
  51. def __getitem__(self, key: str) -> str:
  52. return self.content[key]
  53. def __contains__(self, item: str) -> bool:
  54. return item in self.content
  55. def update(self, other: dict[str, str]) -> PyEnvCfg:
  56. self.content.update(other)
  57. return self
  58. def __repr__(self) -> str:
  59. return f"{self.__class__.__name__}(path={self.path})"
  60. __all__ = [
  61. "PyEnvCfg",
  62. ]