From 96016650c1c447062186fd787d63fb5752a9bfc5 Mon Sep 17 00:00:00 2001 From: dettlaff Date: Wed, 27 Nov 2024 16:52:39 +0400 Subject: [PATCH] feat: mv temporary_env_var to utils --- selfprivacy_api/utils/__init__.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/selfprivacy_api/utils/__init__.py b/selfprivacy_api/utils/__init__.py index cdba2e2..482845b 100644 --- a/selfprivacy_api/utils/__init__.py +++ b/selfprivacy_api/utils/__init__.py @@ -8,6 +8,7 @@ import subprocess import portalocker from typing import Optional import glob +from contextlib import contextmanager from traceback import format_tb as format_traceback @@ -267,3 +268,16 @@ def read_account_uri() -> str: with open(account_file[0], "r") as file: account_info = json.load(file) return account_info["registration"]["uri"] + + +@contextmanager +def temporary_env_var(key, value): + """ + A context manager for temporarily setting an environment variable + with automatic cleanup after exiting the block, even in case of an error. + """ + os.environ[key] = value + try: + yield + finally: + del os.environ[key]