From 3c8a8188fb99fe6ba83b6de700bcab9a7636cd23 Mon Sep 17 00:00:00 2001 From: Chris McCarthy Date: Fri, 9 Jun 2023 22:23:45 +0100 Subject: [PATCH] #951 - Can not view and change the log level from the cli. - Fixed write transaction issue in transactions_to_file.py --- src/primaite/__init__.py | 23 ++++++++----- src/primaite/cli.py | 34 +++++++++++++++++++ .../setup/_package_data/primaite_config.yaml | 2 +- .../transactions/transactions_to_file.py | 9 +++-- 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/primaite/__init__.py b/src/primaite/__init__.py index c000e422..1ea110c9 100644 --- a/src/primaite/__init__.py +++ b/src/primaite/__init__.py @@ -14,6 +14,7 @@ from platformdirs import PlatformDirs _PLATFORM_DIRS: Final[PlatformDirs] = PlatformDirs(appname="primaite") """An instance of `PlatformDirs` set with appname='primaite'.""" + def _get_primaite_config(): config_path = _PLATFORM_DIRS.user_config_path / "primaite_config.yaml" if not config_path.exists(): @@ -24,16 +25,20 @@ def _get_primaite_config(): ) with open(config_path, "r") as file: primaite_config = yaml.safe_load(file) + log_level_map = { + "NOTSET": logging.NOTSET, + "DEBUG": logging.DEBUG, + "INFO": logging.INFO, + "WARN": logging.WARN, + "ERROR": logging.ERROR, + "CRITICAL": logging.CRITICAL + } + primaite_config["log_level"] = log_level_map[primaite_config["log_level"]] return primaite_config _PRIMAITE_CONFIG = _get_primaite_config() -# PrimAITE config items -_LOG_LEVEL: int = _PRIMAITE_CONFIG["log_level"] -_LOGGER_FORMAT: str = _PRIMAITE_CONFIG["logger_format"] - - _USER_DIRS: Final[Path] = Path.home() / "primaite" """The users home space for PrimAITE which is located at: ~/primaite.""" @@ -86,10 +91,10 @@ _FILE_HANDLER: Final[RotatingFileHandler] = RotatingFileHandler( backupCount=9, # Max 100MB of logs encoding="utf8", ) -_STREAM_HANDLER.setLevel(_LOG_LEVEL) -_FILE_HANDLER.setLevel(_LOG_LEVEL) +_STREAM_HANDLER.setLevel(_PRIMAITE_CONFIG["log_level"]) +_FILE_HANDLER.setLevel(_PRIMAITE_CONFIG["log_level"]) -_LOG_FORMAT_STR: Final[str] = _LOGGER_FORMAT +_LOG_FORMAT_STR: Final[str] = _PRIMAITE_CONFIG["logger_format"] _STREAM_HANDLER.setFormatter(logging.Formatter(_LOG_FORMAT_STR)) _FILE_HANDLER.setFormatter(logging.Formatter(_LOG_FORMAT_STR)) @@ -108,7 +113,7 @@ def getLogger(name: str) -> Logger: logging config. """ logger = logging.getLogger(name) - logger.setLevel(_LOG_LEVEL) + logger.setLevel(_PRIMAITE_CONFIG["log_level"]) return logger diff --git a/src/primaite/cli.py b/src/primaite/cli.py index 1abf625c..19746d01 100644 --- a/src/primaite/cli.py +++ b/src/primaite/cli.py @@ -1,11 +1,15 @@ # Crown Copyright (C) Dstl 2022. DEFCON 703. Shared in confidence. """Provides a CLI using Typer as an entry point.""" +import logging import os import shutil from pathlib import Path +from enum import Enum +from typing import Optional import pkg_resources import typer +import yaml from platformdirs import PlatformDirs from typing_extensions import Annotated @@ -49,6 +53,36 @@ def logs(last_n: Annotated[int, typer.Option("-n")]): print(re.sub(r"\n*", "", line)) +_LogLevel = Enum("LogLevel", {k: k for k in logging._levelToName.values()}) # noqa + + +@app.command() +def log_level(level: Annotated[Optional[_LogLevel], typer.Argument()] = None): + """ + View or set the PrimAITE Log Level. + + To View, simply call: primaite log-level + + To set, call: primaite log-level + + For example, to set the to debug, call: primaite log-level DEBUG + """ + app_dirs = PlatformDirs(appname="primaite") + app_dirs.user_config_path.mkdir(exist_ok=True, parents=True) + user_config_path = app_dirs.user_config_path / "primaite_config.yaml" + if user_config_path.exists(): + with open(user_config_path, "r") as file: + primaite_config = yaml.safe_load(file) + + if level: + primaite_config["log_level"] = level.value + with open(user_config_path, "w") as file: + yaml.dump(primaite_config, file) + else: + level = primaite_config["log_level"] + print(f"PrimAITE Log Level: {level}") + + @app.command() def notebooks(): """Start Jupyter Lab in the users PrimAITE notebooks directory.""" diff --git a/src/primaite/setup/_package_data/primaite_config.yaml b/src/primaite/setup/_package_data/primaite_config.yaml index e5c9667e..690544fb 100644 --- a/src/primaite/setup/_package_data/primaite_config.yaml +++ b/src/primaite/setup/_package_data/primaite_config.yaml @@ -1,5 +1,5 @@ # The main PrimAITE application config file # Logging -log_level: 'INFO' +log_level: INFO logger_format: '%(asctime)s::%(levelname)s::%(name)s::%(lineno)s::%(message)s' diff --git a/src/primaite/transactions/transactions_to_file.py b/src/primaite/transactions/transactions_to_file.py index da2468cf..11e68af8 100644 --- a/src/primaite/transactions/transactions_to_file.py +++ b/src/primaite/transactions/transactions_to_file.py @@ -16,11 +16,10 @@ def turn_action_space_to_array(_action_space): Args: _action_space: The action space. """ - return_array = [] - for x in range(len(_action_space)): - return_array.append(str(_action_space[x])) - - return return_array + if isinstance(_action_space, list): + return [str(i) for i in _action_space] + else: + return [str(_action_space)] def turn_obs_space_to_array(_obs_space, _obs_assets, _obs_features):