From bb032490ded34a410f3b93b34409038a9ff30d10 Mon Sep 17 00:00:00 2001 From: Benjamin Pritchard Date: Tue, 14 Jan 2025 14:50:48 -0500 Subject: [PATCH] Enable creating portal client from env vars --- qcportal/qcportal/client.py | 1 - qcportal/qcportal/client_base.py | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/qcportal/qcportal/client.py b/qcportal/qcportal/client.py index e1fe97cee..f41475aca 100644 --- a/qcportal/qcportal/client.py +++ b/qcportal/qcportal/client.py @@ -125,7 +125,6 @@ def __init__( *, cache_dir: Optional[str] = None, cache_max_size: int = 0, - memory_cache_key: Optional[str] = None, ) -> None: """ Parameters diff --git a/qcportal/qcportal/client_base.py b/qcportal/qcportal/client_base.py index c60725831..254e69562 100644 --- a/qcportal/qcportal/client_base.py +++ b/qcportal/qcportal/client_base.py @@ -235,6 +235,43 @@ def from_file(cls, server_name: Optional[str] = None, config_path: Optional[str] return cls(**data) + @classmethod + def from_env(cls): + """Creates a new client given information stored in environment variables + + The environment variables are: + + * QCPORTAL_ADDRESS (required) + * QCPORTAL_USERNAME (optional) + * QCPORTAL_PASSWORD (optional) + * QCPORTAL_VERIFY (optional, defaults to True) + * QCPORTAL_CACHE_DIR (optional) + """ + + address = os.environ.get("QCPORTAL_ADDRESS", None) + username = os.environ.get("QCPORTAL_USERNAME", None) + password = os.environ.get("QCPORTAL_PASSWORD", None) + verify = os.environ.get("QCPORTAL_VERIFY", True) + cache_dir = os.environ.get("QCPORTAL_CACHE_DIR", None) + + if address is None: + raise KeyError("Required environment variable 'QCPORTAL_ADDRESS' not found") + + data = {"address": address} + + if username is not None: + data["username"] = username + + if password is not None: + data["password"] = password + + if cache_dir is not None: + data["cache_dir"] = cache_dir + + data["verify"] = verify + + return cls(**data) + @property def encoding(self) -> str: return self._encoding