(key: str, value: str)
| 22 | return os.getenv(key, default) |
| 23 | |
| 24 | def save_dotenv_value(key: str, value: str): |
| 25 | if value is None: |
| 26 | value = "" |
| 27 | dotenv_path = get_dotenv_file_path() |
| 28 | if not os.path.isfile(dotenv_path): |
| 29 | with open(dotenv_path, "w") as f: |
| 30 | f.write("") |
| 31 | with open(dotenv_path, "r+") as f: |
| 32 | lines = f.readlines() |
| 33 | found = False |
| 34 | for i, line in enumerate(lines): |
| 35 | if re.match(rf"^\s*{key}\s*=", line): |
| 36 | lines[i] = f"{key}={value}\n" |
| 37 | found = True |
| 38 | if not found: |
| 39 | lines.append(f"\n{key}={value}\n") |
| 40 | f.seek(0) |
| 41 | f.writelines(lines) |
| 42 | f.truncate() |
| 43 | load_dotenv() |
no test coverage detected