From 91024efdba491f513517757be7fd6e492c28dbdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Brand=C3=A3o?= <555migalves555@gmail.com> Date: Wed, 19 Jul 2023 12:17:38 +0100 Subject: [PATCH] updated pr to new deepsearch ver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miguel Brandão <555migalves555@gmail.com> --- deepsearch/cli.py | 38 +++++++++------------- deepsearch/core/cli/profile.py | 7 ++++ deepsearch/core/client/settings.py | 5 +++ deepsearch/core/client/settings_manager.py | 2 ++ deepsearch/cps/cli/main.py | 1 + 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/deepsearch/cli.py b/deepsearch/cli.py index 9954dee6..8bdd3974 100644 --- a/deepsearch/cli.py +++ b/deepsearch/cli.py @@ -2,36 +2,25 @@ from pathlib import Path from typing import List -from deepsearch import DeepSearchConfig -from deepsearch.artifacts.cli.main import app as artifacts_app -from deepsearch.core.cli.main import app -from deepsearch.core.cli.plugins import get_cli_groups -from deepsearch.core.util.config_paths import config_file_path -from deepsearch.cps.cli.main import app as cps_app -from deepsearch.documents.cli.main import app as documents_app -from deepsearch.query.cli.main import app as query_app - -# TODO review if unwanted information is being logged +from deepsearch.core.client.settings_manager import settings_mgr def setup_logger(): # Setting up root logger - config_file = config_file_path() - - if not config_file.exists(): - raise RuntimeError( - f"Config file {config_file} does not exist. Please configure your default authentication with `$ deepsearch login`" - ) - config = DeepSearchConfig.parse_file(config_file) + active_profile_settings = settings_mgr.get_profile_settings( + settings_mgr.get_active_profile() + ) - p = Path(config.log_configuration.target_file) + p = Path(active_profile_settings.log_target_file) if not p.parent.is_dir(): p.parent.mkdir(parents=True) + p = p.joinpath("deepsearch.log") + handlers: List[logging.Handler] = [ logging.FileHandler(p), ] - if config.log_configuration.write_to_console: + if active_profile_settings.log_to_console: handlers.append(logging.StreamHandler()) formatter = logging.Formatter( "%(asctime)s %(name)s — %(levelname)s — %(module)s:%(funcName)s:%(lineno)d — %(message)s" @@ -46,18 +35,21 @@ def setup_logger(): logger = setup_logger() +from deepsearch.artifacts.cli.main import app as artifacts_app +from deepsearch.core.cli.main import app +from deepsearch.core.cli.plugins import get_cli_groups +from deepsearch.cps.cli.main import app as cps_app +from deepsearch.documents.cli.main import app as documents_app +from deepsearch.query.cli.main import app as query_app + app.add_typer(cps_app, name="cps", help="Interact with DeepSearch CPS component") -logger.info("Cps module initialized") app.add_typer(query_app, name="query", help="Interact with DeepSearch Query component") -logger.info("Query module initialized") app.add_typer( documents_app, name="documents", help="Interact with DeepSearch Document Conversion component", ) -logger.info("Documents module initialized") app.add_typer(artifacts_app, name="artifacts", help="Manage artifacts") -logger.info("Artifacts module initialized") for group in get_cli_groups(): app.add_typer(group) diff --git a/deepsearch/core/cli/profile.py b/deepsearch/core/cli/profile.py index 71b81ded..56fafb44 100644 --- a/deepsearch/core/cli/profile.py +++ b/deepsearch/core/cli/profile.py @@ -1,5 +1,6 @@ from typing import Optional +import platformdirs import typer from rich.console import Console from rich.table import Table @@ -29,6 +30,10 @@ def add_profile( username: str = typer.Option(prompt=True), api_key: str = typer.Option(prompt=True, hide_input=True), verify_ssl: bool = typer.Option(default=True), + log_directory: str = typer.Option( + prompt=True, default=platformdirs.user_log_dir("DeepSearch", "IBM") + ), + log_to_console: bool = typer.Option(default=False), profile_name: str = typer.Option( default="", help="If not set, the active profile will be updated or, if no profile available, a new profile with a predetermined name will be created.", @@ -44,6 +49,8 @@ def add_profile( username=username, api_key=api_key, verify_ssl=verify_ssl, + log_target_file=log_directory, + log_to_console=log_to_console, ) settings_mgr.save_settings( diff --git a/deepsearch/core/client/settings.py b/deepsearch/core/client/settings.py index ee09283f..9561938f 100644 --- a/deepsearch/core/client/settings.py +++ b/deepsearch/core/client/settings.py @@ -4,6 +4,7 @@ from pathlib import Path from typing import Dict, Optional, Union +import platformdirs from pydantic import BaseSettings, SecretStr @@ -38,6 +39,8 @@ class ProfileSettings(DumpableSettings): username: str api_key: SecretStr verify_ssl: bool = True + log_target_file: str + log_to_console: bool class Config: env_prefix = "DEEPSEARCH_" @@ -49,6 +52,8 @@ def from_cli_prompt(cls) -> ProfileSettings: username=input("Username: "), api_key=getpass("API key: "), verify_ssl=input("SSL verification [y/n]: "), + log_directory=input("Log directory: "), + log_to_console=input("log_to_console [y/n]: "), ) diff --git a/deepsearch/core/client/settings_manager.py b/deepsearch/core/client/settings_manager.py index 0cce3a07..791fdee1 100644 --- a/deepsearch/core/client/settings_manager.py +++ b/deepsearch/core/client/settings_manager.py @@ -86,6 +86,8 @@ def _migrate_legacy_config(self) -> None: username=legacy_cfg.auth.username, api_key=legacy_cfg.auth.api_key, verify_ssl=legacy_cfg.verify_ssl, + log_directory=platformdirs.user_log_dir("DeepSearch", "IBM"), + log_to_console=False, ) self.save_settings( profile_settgs=new_cfg, diff --git a/deepsearch/cps/cli/main.py b/deepsearch/cps/cli/main.py index d557f9bc..ed22b74f 100644 --- a/deepsearch/cps/cli/main.py +++ b/deepsearch/cps/cli/main.py @@ -1,6 +1,7 @@ import logging logger = logging.getLogger("root.cps") +print(logger.handlers) import typer