Skip to content

Commit

Permalink
Merge pull request #880 from MolSSI/qcp_env
Browse files Browse the repository at this point in the history
Enable creating portal client from env vars
  • Loading branch information
bennybp authored Jan 14, 2025
2 parents 4f552af + bb03249 commit 260aca0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
1 change: 0 additions & 1 deletion qcportal/qcportal/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ def __init__(
*,
cache_dir: Optional[str] = None,
cache_max_size: int = 0,
memory_cache_key: Optional[str] = None,
) -> None:
"""
Parameters
Expand Down
37 changes: 37 additions & 0 deletions qcportal/qcportal/client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 260aca0

Please sign in to comment.