-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use custom Config class to support env and keys.cfg
- Loading branch information
Showing
4 changed files
with
51 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,5 @@ | |
"SWEEnv", | ||
"get_data_path_name", | ||
"PACKAGE_DIR", | ||
"CONFIG_DIR", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import logging | ||
from typing import Any | ||
import config as config_file | ||
import os | ||
|
||
|
||
logger = logging.getLogger("config") | ||
|
||
|
||
class Config: | ||
def __init__(self, ): | ||
"""This wrapper class is used to load keys from environment variables or keys.cfg file. | ||
Whenever both are presents, the environment variable is used. | ||
""" | ||
# Defer import to avoid circular import | ||
from sweagent import PACKAGE_DIR | ||
self._keys_cfg = None | ||
keys_cfg_path = PACKAGE_DIR / "keys.cfg" | ||
if keys_cfg_path.exists(): | ||
try: | ||
self._keys_cfg = config_file.Config(PACKAGE_DIR / "keys.cfg") | ||
except Exception as e: | ||
raise RuntimeError(f"Error loading keys.cfg. Please check the file.") from e | ||
else: | ||
logger.error(f"keys.cfg not found in {PACKAGE_DIR}") | ||
|
||
def get(self, key, default=None) -> Any: | ||
if key in os.environ: | ||
return os.environ[key] | ||
if self._keys_cfg is not None and key in self._keys_cfg: | ||
return self._keys_cfg[key] | ||
return default | ||
|
||
def __getitem__(self, key: str) -> Any: | ||
if key in os.environ: | ||
return os.environ[key] | ||
if self._keys_cfg is not None and key in self._keys_cfg: | ||
return self._keys_cfg[key] | ||
raise KeyError(f"Key {key} not found in environment or keys.cfg") | ||
|
||
def __contains__(self, key: str) -> bool: | ||
return key in os.environ or (self._keys_cfg is not None and key in self._keys_cfg) |