From 348da291b365a6c1aa4074d950a4f0f1b151c11c Mon Sep 17 00:00:00 2001 From: Robert Smallshire Date: Tue, 5 Mar 2024 11:56:28 +0100 Subject: [PATCH] Adds a missing file --- src/venv_management/pyenv_config.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/venv_management/pyenv_config.py diff --git a/src/venv_management/pyenv_config.py b/src/venv_management/pyenv_config.py new file mode 100644 index 0000000..05267d5 --- /dev/null +++ b/src/venv_management/pyenv_config.py @@ -0,0 +1,29 @@ +import configparser +from pathlib import Path + + +def pyvenv_config(env_dirpath: Path, key: str) -> str | None: + """Read a value from a pyvenv config file. + + Args: + env_dirpath: Path to the directory containing the pyvenv.cfg file. + key: The key from which to lookup the associated value. + + Returns: + The value from the pyvenv.cfg file, or None if the key does not exist + in the pyvenv.cfg file or if the pyenv.cfg config file does not exist. + """ + pyenv_cfg_path = env_dirpath / "pyvenv.cfg" + value = None + if pyenv_cfg_path.is_file(): + config_text = pyenv_cfg_path.read_text() + config = configparser.ConfigParser() + config.read_string("[root]\n" + config_text) + + try: + config_executable = config["root"][key] + except KeyError: + pass + else: + value = config_executable.strip() + return value