diff --git a/smartassist/src/utils.py b/smartassist/src/utils.py index b7aa187..af86f7f 100644 --- a/smartassist/src/utils.py +++ b/smartassist/src/utils.py @@ -5,7 +5,7 @@ import logging import json import requests from typing import Optional -from enums import LogLevel +from enums import LogLevel, LOG_LEVEL_MAPPING class GlobalState: """ @@ -43,58 +43,42 @@ class GlobalState: return cls._instance - # def configure_logging(self, level: Optional[LogLevel] = None) -> None: - # """ - # Configure the logging system for this project. - - # Args: - # level (LogLevel): The log level to use. If None, uses the default log level set in `self.log_level`. - - # Notes: - # This method sets up logging for the project and logs a message at the debug level indicating the effective log level. - # """ - def configure_logging(self, level: Optional[str] = None) -> None: + def configure_logging(self, level: Optional[LogLevel] = None) -> None: """ Configure the logging system for this project. - + Args: - level (str): The log level to use. Can be one of the standard Python log levels (e.g., 'DEBUG', 'INFO', 'WARNING', etc.). If None, uses the default log level set in `self.log_level`. - + level (LogLevel): The log level to use. If None, uses the default log level set in `self.log_level`. + Notes: This method sets up logging for the project and logs a message at the debug level indicating the effective log level. """ - if level is None: + if level == None: level = self.log_level - # numeric_level = getattr(logging, level.upper()) # Convert string to numeric level - numeric_level = getattr(logging, level.upper()) # Convert string to numeric level + if isinstance(level, LogLevel): + logging.info(f"Trying to set up logging with level {level}") + numeric_level = LOG_LEVEL_MAPPING[level] + if numeric_level is None: + raise ValueError("Invalid log level") self.logger.setLevel(numeric_level) self.logger.debug(f"utils.py -- configure_logging(): effective log level is {level} which is {self.logger.getEffectiveLevel()}") - # def set_log_level(self, level: LogLevel) -> None: - # """ - # Set the log level for this project. - # Args: - # level (LogLevel): The new log level to use. Can be one of the evels defined in enum.py (e.g., DEBUG, INFO, WARNING, CRITICAL etc.). - - # Notes: - # This method updates the `self.log_level` attribute and calls `configure_logging()` to apply the change. - # """ - def set_log_level(self, level: str = 'INFO') -> None: + def set_log_level(self, level: LogLevel) -> None: """ Set the log level for this project. Args: - level (str): The new log level to use. Can be one of the standard Python log levels (e.g., 'DEBUG', 'INFO', 'WARNING', etc.). + level (LogLevel): The new log level to use. Can be one of the evels defined in enum.py (e.g., DEBUG, INFO, WARNING, CRITICAL etc.). Notes: This method updates the `self.log_level` attribute and calls `configure_logging()` to apply the change. - """ + """ self.log_level = level self.configure_logging() - def get_log_level(self) -> str: + def get_log_level(self) -> LogLevel: """ Get the current log level. @@ -232,6 +216,18 @@ class GlobalState: """ return [ep for ep in self.endpoints if key in ep] + def get_endpoints_with_key_values(self, key: str, value: any) -> list[dict]: + """ + Returns a list of endpoint dictionaries that contain the specified key-value pair. + + Args: + key (str): The key to search for in the endpoint dictionaries. + value (Any): The value to search for in the endpoint dictionaries. + + Returns: + list[dict]: A list of endpoint dictionaries containing the specified key. + """ + return [ep for ep in self.endpoints if key in ep and value == ep[key]] def fetch_models(self) -> None: """