From 342321485e6cd3eec36744eb644649de3a12c6ec Mon Sep 17 00:00:00 2001 From: Leonardo Schwarz Date: Mon, 5 Aug 2024 10:10:42 +0200 Subject: [PATCH] use the harmonized logging setup --- bfabric/cli_formatting.py | 12 ++++++++++++ ...fabric_list_not_available_proteomics_workunits.py | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/bfabric/cli_formatting.py b/bfabric/cli_formatting.py index b8acc7f..f986f07 100644 --- a/bfabric/cli_formatting.py +++ b/bfabric/cli_formatting.py @@ -1,3 +1,6 @@ +import sys + +from loguru import logger from rich.highlighter import RegexHighlighter from rich.theme import Theme @@ -10,3 +13,12 @@ class HostnameHighlighter(RegexHighlighter): DEFAULT_THEME = Theme({"bfabric.hostname": "bold red"}) + + +def setup_script_logging(debug: bool = False) -> None: + """Sets up the logging for the command line scripts.""" + logger.remove() + if not debug: + logger.add(sys.stderr, filter="bfabric", level="INFO", format="{level} {message}") + else: + logger.add(sys.stderr, filter="bfabric", level="DEBUG") diff --git a/bfabric/scripts/bfabric_list_not_available_proteomics_workunits.py b/bfabric/scripts/bfabric_list_not_available_proteomics_workunits.py index cdbaaad..a833f5a 100755 --- a/bfabric/scripts/bfabric_list_not_available_proteomics_workunits.py +++ b/bfabric/scripts/bfabric_list_not_available_proteomics_workunits.py @@ -16,10 +16,12 @@ from datetime import datetime, timedelta from typing import Any +from loguru import logger from rich.console import Console from rich.table import Column, Table from bfabric import Bfabric, BfabricClientConfig +from bfabric.cli_formatting import setup_script_logging def render_output(workunits_by_status: dict[str, list[dict[str, Any]]], config: BfabricClientConfig) -> None: @@ -60,6 +62,12 @@ def render_output(workunits_by_status: dict[str, list[dict[str, Any]]], config: def list_not_available_proteomics_workunits(date_cutoff: datetime) -> None: """Lists proteomics work units that are not available on bfabric.""" client = Bfabric.from_config() + console = Console() + with console.capture() as capture: + console.print( + f"listing not available proteomics work units created after {date_cutoff}", style="bright_yellow", end="" + ) + logger.info(capture.get()) workunits_by_status = {} for status in ["Pending", "Processing", "Failed"]: @@ -73,6 +81,7 @@ def list_not_available_proteomics_workunits(date_cutoff: datetime) -> None: def main() -> None: """Parses the command line arguments and calls `list_not_available_proteomics_workunits`.""" + setup_script_logging() parser = ArgumentParser(description="Lists proteomics work units that are not available on bfabric.") parser.add_argument("--max-age", type=int, help="Max age of work units in days", default=14) args = parser.parse_args()