-
Notifications
You must be signed in to change notification settings - Fork 72
[Opt]Support passing parameters by overriding default values through environment variables #702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Potterluo
wants to merge
2
commits into
ModelEngine-Group:develop
Choose a base branch
from
Potterluo:bugfix_test_config
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,86 +1,195 @@ | ||
| import logging | ||
| import os | ||
| import re | ||
| import threading | ||
| from typing import Any, Dict | ||
| from typing import Any, Dict, Optional, Union | ||
|
|
||
| import yaml | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _parse_string_type(value: Any) -> Any: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try to use ast.literal_eval instead of building another wheel |
||
| """Convert string values to appropriate Python types (bool, int, float).""" | ||
| if not isinstance(value, str): | ||
| return value | ||
|
|
||
| stripped = value.strip() | ||
| if not stripped: | ||
| return stripped | ||
|
|
||
| lower_val = stripped.lower() | ||
|
|
||
| # Boolean conversion (support multiple formats) | ||
| if lower_val == "true": | ||
| return True | ||
| if lower_val == "false": | ||
| return False | ||
|
|
||
| # Numeric conversion | ||
| try: | ||
| if "." in stripped and stripped.count(".") == 1: | ||
| if not stripped.startswith(".") and not stripped.endswith("."): | ||
| return float(stripped) | ||
| return int(stripped) | ||
| except ValueError: | ||
| return stripped | ||
|
|
||
|
|
||
| class ConfigUtils: | ||
| """ | ||
| Singleton Configuration Utility | ||
| Provides methods to read and access YAML configuration files. | ||
| Thread-safe singleton configuration utility. | ||
|
|
||
| Features: | ||
| - YAML config with environment variable substitution ${VAR:-default} | ||
| - Automatic type conversion for string values | ||
| - Nested key access using dot notation | ||
| """ | ||
|
|
||
| _instance = None | ||
| _lock = threading.Lock() # Ensure thread-safe singleton creation | ||
| _instance: Optional["ConfigUtils"] = None | ||
| _lock = threading.Lock() | ||
| _init_lock = threading.Lock() | ||
|
|
||
| def __init__(self): | ||
| self._config = None | ||
| ENV_PATTERN = re.compile(r"^\$\{(\w+)(?::-([^}]*))?\}$") | ||
|
|
||
| def __new__(cls, config_file: str = None): | ||
| # Double-checked locking | ||
| def __new__(cls, config_file: Optional[str] = None, **kwargs): | ||
| """Double-checked locking for thread-safe singleton""" | ||
| if cls._instance is None: | ||
| with cls._lock: | ||
| if cls._instance is None: | ||
| instance = super().__new__(cls) | ||
| instance._init_config(config_file) | ||
| instance._config = None | ||
| instance.config_file = None | ||
| cls._instance = instance | ||
| instance._init_config(config_file) | ||
|
|
||
| return cls._instance | ||
|
|
||
| def _init_config(self, config_file: str = None): | ||
| """Initialize configuration file path and load config""" | ||
| def __init__(self): | ||
| self._config = None | ||
|
|
||
| @classmethod | ||
| def get_instance(cls, config_file: Optional[str] = None) -> "ConfigUtils": | ||
| """Get singleton instance""" | ||
| return cls(config_file) | ||
|
|
||
| def _init_config(self, config_file: Optional[str] = None): | ||
| """Initialize config file path""" | ||
| if config_file is None: | ||
| current_dir = os.path.dirname(os.path.abspath(__file__)) | ||
| config_file = os.path.join(current_dir, "..", "config.yaml") | ||
|
|
||
| self.config_file = os.path.abspath(config_file) | ||
| self._config = None # Lazy load | ||
| logger.info(f"Configuration file path set to: {self.config_file}") | ||
|
|
||
| def _substitute_env_vars(self, data: Any) -> Any: | ||
| """Recursively substitute environment variables""" | ||
| if isinstance(data, dict): | ||
| return {k: self._substitute_env_vars(v) for k, v in data.items()} | ||
| elif isinstance(data, list): | ||
| return [self._substitute_env_vars(item) for item in data] | ||
| elif isinstance(data, str): | ||
| return self._process_string_value(data) | ||
| return data | ||
|
|
||
| def _process_string_value(self, value: str) -> Any: | ||
| """Process string: check for env var pattern, then type conversion""" | ||
| match = self.ENV_PATTERN.fullmatch(value.strip()) | ||
|
|
||
| if match: | ||
| var_name = match.group(1) | ||
| default_val = match.group(2) | ||
|
|
||
| env_value = os.getenv(var_name) | ||
| if env_value is not None: | ||
| return _parse_string_type(env_value.strip()) | ||
| elif default_val is not None: | ||
| return _parse_string_type(default_val.strip()) | ||
| else: | ||
| logger.warning( | ||
| f"Env var '{var_name}' not found, no default. Keeping: {value}" | ||
| ) | ||
| return value | ||
| else: | ||
| return _parse_string_type(value) | ||
|
|
||
| def _load_config(self) -> Dict[str, Any]: | ||
| """Internal method to read configuration from file""" | ||
| """Load config from file""" | ||
| if not self.config_file: | ||
| logger.error("Config file path not initialized") | ||
| return {} | ||
|
|
||
| if not os.path.exists(self.config_file): | ||
| logger.warning(f"Configuration file not found: {self.config_file}") | ||
| return {} | ||
|
|
||
| try: | ||
| with open(self.config_file, "r", encoding="utf-8") as f: | ||
| return yaml.safe_load(f) or {} | ||
| except FileNotFoundError: | ||
| print(f"[WARN] Config file not found: {self.config_file}") | ||
| return {} | ||
| raw_config = yaml.safe_load(f) or {} | ||
| return self._substitute_env_vars(raw_config) | ||
| except yaml.YAMLError as e: | ||
| print(f"[ERROR] Failed to parse YAML config: {e}") | ||
| logger.error(f"YAML parsing error: {e}") | ||
| return {} | ||
| except Exception as e: | ||
| logger.error(f"Error loading config: {e}") | ||
| return {} | ||
|
|
||
| def read_config(self) -> Dict[str, Any]: | ||
| """Read configuration file (lazy load)""" | ||
| """Lazy load configuration""" | ||
| if self._config is None: | ||
| self._config = self._load_config() | ||
| with self._init_lock: | ||
| if self._config is None: | ||
| self._config = self._load_config() | ||
| return self._config | ||
|
|
||
| def reload_config(self): | ||
| """Force reload configuration file""" | ||
| self._config = self._load_config() | ||
| def reload_config(self) -> Dict[str, Any]: | ||
| """Force reload configuration""" | ||
| with self._init_lock: | ||
| self._config = self._load_config() | ||
| logger.info(f"Configuration reloaded successfully") | ||
| return self._config | ||
|
|
||
| def get_config(self, key: str, default: Any = None) -> Any: | ||
| """Get top-level configuration item""" | ||
| config = self.read_config() | ||
| return config.get(key, default) | ||
| """Get top-level config item""" | ||
| return self.read_config().get(key, default) | ||
|
|
||
| def get_nested_config(self, key_path: str, default: Any = None) -> Any: | ||
| """Get nested configuration, e.g., 'influxdb.host'""" | ||
| def get_nested_config( | ||
| self, key_path: str, default: Any = None, separator: str = "." | ||
| ) -> Any: | ||
| """Get nested config using dot notation (e.g., 'database.host')""" | ||
| config = self.read_config() | ||
| keys = key_path.split(".") | ||
| keys = key_path.split(separator) | ||
| value = config | ||
|
|
||
| try: | ||
| for k in keys: | ||
| value = value[k] | ||
| for key in keys: | ||
| if not isinstance(value, dict): | ||
| return default | ||
| value = value[key] | ||
| return value | ||
| except (KeyError, TypeError): | ||
| return default | ||
|
|
||
| def __getitem__(self, key: str) -> Any: | ||
| """Support config_utils['key']""" | ||
| return self.get_config(key) | ||
|
|
||
| def __contains__(self, key: str) -> bool: | ||
| """Support 'key' in config_utils""" | ||
| return key in self.read_config() | ||
|
|
||
|
|
||
| # Global instance | ||
| config_utils = ConfigUtils() | ||
|
|
||
| if __name__ == "__main__": | ||
| print("DataBase config:", config_utils.get_config("database")) | ||
| print("=== Configuration Test ===") | ||
| print(f"Config file path: {config_utils.config_file}") | ||
| print(f"LLM Connection: {config_utils.get_config('llm_connection')}") | ||
| print(f"EXTRA_INFO: {config_utils.get_nested_config('llm_connection.extra_info')}") | ||
| os.environ["LLM_EX_INFO"] = "prefix cache and gsa" | ||
| config_utils.reload_config() | ||
| print(f"EXTRA_INFO: {config_utils.get_nested_config('llm_connection.extra_info')}") | ||
| print( | ||
| "DataBase host:", config_utils.get_nested_config("database.host", "localhost") | ||
| f"Timeout: {config_utils.get_nested_config('llm_connection.timeout')} (type: {type(config_utils.get_nested_config('llm_connection.timeout'))})" | ||
| ) | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import logger from ucm module maybe