From 2f4194a2d6a5e3ec26d0bddeeeac15cd87d06f89 Mon Sep 17 00:00:00 2001 From: Phil Ewels Date: Thu, 5 Nov 2020 12:16:58 +0100 Subject: [PATCH 1/4] Logging - force colours if certain env vars are set. --- nf_core/__main__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/nf_core/__main__.py b/nf_core/__main__.py index 6eebbe8815..a1d7f726ff 100755 --- a/nf_core/__main__.py +++ b/nf_core/__main__.py @@ -28,13 +28,18 @@ # Submodules should all traverse back to this log = logging.getLogger() +# Should we force coloured output from Rich? +rich_force_colors = None +if os.getenv("GITHUB_ACTIONS") or os.getenv("FORCE_COLOR") or os.getenv("PY_COLORS"): + rich_force_colors = True + def run_nf_core(): # Set up the rich traceback rich.traceback.install(width=200, word_wrap=True) # Print nf-core header to STDERR - stderr = rich.console.Console(file=sys.stderr) + stderr = rich.console.Console(file=sys.stderr, force_terminal=rich_force_colors) stderr.print("\n[green]{},--.[grey39]/[green],-.".format(" " * 42), highlight=False) stderr.print("[blue] ___ __ __ __ ___ [green]/,-._.--~\\", highlight=False) stderr.print("[blue] |\ | |__ __ / ` / \ |__) |__ [yellow] } {", highlight=False) @@ -114,7 +119,7 @@ def nf_core_cli(verbose, log_file): log.addHandler( rich.logging.RichHandler( level=logging.DEBUG if verbose else logging.INFO, - console=rich.console.Console(file=sys.stderr), + console=rich.console.Console(file=sys.stderr, force_terminal=rich_force_colors), show_time=False, markup=True, ) From 29ce8b36c3af82524d5d6a941c7c5167bf4ce739 Mon Sep 17 00:00:00 2001 From: Phil Ewels Date: Thu, 5 Nov 2020 12:23:54 +0100 Subject: [PATCH 2/4] Add to utils function, apply to lint and launch console functions --- nf_core/__main__.py | 9 ++------- nf_core/launch.py | 2 +- nf_core/lint.py | 2 +- nf_core/utils.py | 9 +++++++++ 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/nf_core/__main__.py b/nf_core/__main__.py index a1d7f726ff..ec3c1aa2a6 100755 --- a/nf_core/__main__.py +++ b/nf_core/__main__.py @@ -28,18 +28,13 @@ # Submodules should all traverse back to this log = logging.getLogger() -# Should we force coloured output from Rich? -rich_force_colors = None -if os.getenv("GITHUB_ACTIONS") or os.getenv("FORCE_COLOR") or os.getenv("PY_COLORS"): - rich_force_colors = True - def run_nf_core(): # Set up the rich traceback rich.traceback.install(width=200, word_wrap=True) # Print nf-core header to STDERR - stderr = rich.console.Console(file=sys.stderr, force_terminal=rich_force_colors) + stderr = rich.console.Console(file=sys.stderr, force_terminal=nf_core.utils.rich_force_colors()) stderr.print("\n[green]{},--.[grey39]/[green],-.".format(" " * 42), highlight=False) stderr.print("[blue] ___ __ __ __ ___ [green]/,-._.--~\\", highlight=False) stderr.print("[blue] |\ | |__ __ / ` / \ |__) |__ [yellow] } {", highlight=False) @@ -119,7 +114,7 @@ def nf_core_cli(verbose, log_file): log.addHandler( rich.logging.RichHandler( level=logging.DEBUG if verbose else logging.INFO, - console=rich.console.Console(file=sys.stderr, force_terminal=rich_force_colors), + console=rich.console.Console(file=sys.stderr, force_terminal=nf_core.utils.rich_force_colors()), show_time=False, markup=True, ) diff --git a/nf_core/launch.py b/nf_core/launch.py index 488d19c224..162cf5062c 100644 --- a/nf_core/launch.py +++ b/nf_core/launch.py @@ -640,7 +640,7 @@ def validate_pattern(val): def print_param_header(self, param_id, param_obj): if "description" not in param_obj and "help_text" not in param_obj: return - console = Console() + console = Console(nf_core.utils.rich_force_colors()) console.print("\n") console.print(param_obj.get("title", param_id), style="bold") if "description" in param_obj: diff --git a/nf_core/lint.py b/nf_core/lint.py index 9b62da3209..1fb0aeac69 100755 --- a/nf_core/lint.py +++ b/nf_core/lint.py @@ -1307,7 +1307,7 @@ def check_schema_params(self): def print_results(self, show_passed=False): log.debug("Printing final results") - console = Console() + console = Console(force_terminal=nf_core.utils.rich_force_colors()) # Helper function to format test links nicely def format_result(test_results, table): diff --git a/nf_core/utils.py b/nf_core/utils.py index c0eb461e33..f09c4bd3cb 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -43,6 +43,15 @@ def check_if_outdated(current_version=None, remote_version=None, source_url="htt return (is_outdated, current_version, remote_version) +def rich_force_colors(): + """ + Check if any environment variables are set to force Rich to use coloured output + """ + if os.getenv("GITHUB_ACTIONS") or os.getenv("FORCE_COLOR") or os.getenv("PY_COLORS"): + return True + return None + + def fetch_wf_config(wf_path): """Uses Nextflow to retrieve the the configuration variables from a Nextflow workflow. From d2ee7b69fba6b4d47874bed83a9adb17da644762 Mon Sep 17 00:00:00 2001 From: Phil Ewels Date: Thu, 5 Nov 2020 12:26:04 +0100 Subject: [PATCH 3/4] Add tests for new utils function --- tests/test_utils.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index 9c73919ad3..f4874506b3 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -4,6 +4,7 @@ import nf_core.utils +import os import unittest @@ -39,3 +40,15 @@ def test_check_if_outdated_5(self): remote_version = "1.11" is_outdated, current, remote = nf_core.utils.check_if_outdated(current_version, remote_version) assert is_outdated + + def test_rich_force_colours_false(self): + os.environ.pop("GITHUB_ACTIONS", None) + os.environ.pop("FORCE_COLOR", None) + os.environ.pop("PY_COLORS", None) + assert nf_core.utils.rich_force_colors() is None + + def test_rich_force_colours_true(self): + os.environ["GITHUB_ACTIONS"] = True + os.environ.pop("FORCE_COLOR", None) + os.environ.pop("PY_COLORS", None) + assert nf_core.utils.rich_force_colors() is True From e9d7c86cf18ebbec0918b751a1f87beffe282e1c Mon Sep 17 00:00:00 2001 From: Phil Ewels Date: Thu, 5 Nov 2020 12:33:25 +0100 Subject: [PATCH 4/4] Fix new utils test --- tests/test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index f4874506b3..b533abb7a1 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -48,7 +48,7 @@ def test_rich_force_colours_false(self): assert nf_core.utils.rich_force_colors() is None def test_rich_force_colours_true(self): - os.environ["GITHUB_ACTIONS"] = True + os.environ["GITHUB_ACTIONS"] = "1" os.environ.pop("FORCE_COLOR", None) os.environ.pop("PY_COLORS", None) assert nf_core.utils.rich_force_colors() is True