From 1b6187822fbf5cbdc1ebffd25bf09e47ea95a533 Mon Sep 17 00:00:00 2001 From: Phil Ewels Date: Thu, 23 Jul 2020 08:49:14 +0200 Subject: [PATCH] Use Rich.prompt Confirm.ask instead of click.confirm See https://github.com/nf-core/tools/issues/682#issuecomment-662555028 --- nf_core/launch.py | 29 ++++++++++------------------- nf_core/schema.py | 37 +++++++++++-------------------------- setup.py | 2 +- 3 files changed, 22 insertions(+), 46 deletions(-) diff --git a/nf_core/launch.py b/nf_core/launch.py index a1c590aa2e..6864223933 100644 --- a/nf_core/launch.py +++ b/nf_core/launch.py @@ -4,8 +4,8 @@ from __future__ import print_function from rich.console import Console from rich.markdown import Markdown +from rich.prompt import Confirm -import click import copy import json import logging @@ -112,11 +112,7 @@ def launch_pipeline(self): # Check if the output file exists already if os.path.exists(self.params_out): log.warning("Parameter output file already exists! {}".format(os.path.relpath(self.params_out))) - if click.confirm( - click.style("Do you want to overwrite this file? ", fg="yellow") + click.style("[y/N]", fg="red"), - default=False, - show_default=False, - ): + if Confirm.ask("[yellow]Do you want to overwrite this file?"): os.remove(self.params_out) log.info("Deleted {}\n".format(self.params_out)) else: @@ -248,9 +244,9 @@ def merge_nxf_flag_schema(self): def prompt_web_gui(self): """ Ask whether to use the web-based or cli wizard to collect params """ - click.secho( - "\nWould you like to enter pipeline parameters using a web-based interface or a command-line wizard?\n", - fg="magenta", + log.info( + "[magenta]Would you like to enter pipeline parameters using a web-based interface or a command-line wizard?", + extra={"markup": True}, ) question = { "type": "list", @@ -407,7 +403,7 @@ def prompt_param(self, param_id, param_obj, is_required, answers): # If required and got an empty reponse, ask again while type(answer[param_id]) is str and answer[param_id].strip() == "" and is_required: - click.secho("Error - this property is required.", fg="red", err=True) + log.error("This property is required.") answer = PyInquirer.prompt([question]) # TODO: use raise_keyboard_interrupt=True when PyInquirer 1.0.3 is released if answer == {}: @@ -464,7 +460,7 @@ def prompt_group(self, param_id, param_obj): req_default = self.schema_obj.input_params.get(p_required, "") req_answer = answers.get(p_required, "") if req_default == "" and req_answer == "": - click.secho("Error - '{}' is required.".format(p_required), fg="red", err=True) + log.error("Error - [bold]'{}'[/] is required.".format(p_required), extra={"markup": True}) while_break = False else: child_param = answer[param_id] @@ -708,14 +704,9 @@ def build_command(self): def launch_workflow(self): """ Launch nextflow if required """ log.info( - "[bold underline]Nextflow command:{}[/]\n [magenta]{}\n\n".format(self.nextflow_cmd), - extra={"markup": True}, + "[bold underline]Nextflow command:[/]\n[magenta]{}\n\n".format(self.nextflow_cmd), extra={"markup": True}, ) - if click.confirm( - "Do you want to run this command now? " + click.style("[y/N]", fg="green"), - default=False, - show_default=False, - ): - log.info("Launching workflow!") + if Confirm.ask("Do you want to run this command now? "): + log.info("Launching workflow! :rocket:", extra={"markup": True}) subprocess.call(self.nextflow_cmd, shell=True) diff --git a/nf_core/schema.py b/nf_core/schema.py index 173bf8575e..81dbc38e80 100644 --- a/nf_core/schema.py +++ b/nf_core/schema.py @@ -2,8 +2,8 @@ """ Code to deal with pipeline JSON Schema """ from __future__ import print_function +from rich.prompt import Confirm -import click import copy import jinja2 import json @@ -244,7 +244,7 @@ def build_schema(self, pipeline_dir, no_prompts, web_only, url): # If running interactively, send to the web for customisation if not self.no_prompts: - if click.confirm(click.style("\nLaunch web builder for customisation and editing?", fg="magenta"), True): + if Confirm.ask(":rocket: Launch web builder for customisation and editing?"): try: self.launch_web_builder() except AssertionError as e: @@ -334,13 +334,6 @@ def remove_schema_notfound_configs(self): log.debug("Removing '{}' from JSON Schema".format(p_key)) params_removed.append(p_key) - if len(params_removed) > 0: - log.info( - "Removed {} params from existing JSON Schema that were not found with `nextflow config`:\n {}\n".format( - len(params_removed), ", ".join(params_removed) - ) - ) - return params_removed def prompt_remove_schema_notfound_config(self, p_key): @@ -350,13 +343,11 @@ def prompt_remove_schema_notfound_config(self, p_key): Returns True if it should be removed, False if not. """ if p_key not in self.pipeline_params.keys(): - p_key_nice = click.style("params.{}".format(p_key), fg="white", bold=True) - remove_it_nice = click.style("Remove it?", fg="yellow") - if ( - self.no_prompts - or self.schema_from_scratch - or click.confirm( - "Unrecognised '{}' found in schema but not pipeline. {}".format(p_key_nice, remove_it_nice), True + if self.no_prompts or self.schema_from_scratch: + return True + if Confirm.ask( + ":question: Unrecognised [white bold]'params.{}'[/] found in schema but not pipeline! [yellow]Remove it?".format( + p_key ) ): return True @@ -372,24 +363,18 @@ def add_schema_found_configs(self): if not p_key in self.schema["properties"].keys(): # Check if key is in group-level params if not any([p_key in param.get("properties", {}) for k, param in self.schema["properties"].items()]): - p_key_nice = click.style("params.{}".format(p_key), fg="white", bold=True) - add_it_nice = click.style("Add to JSON Schema?", fg="cyan") if ( self.no_prompts or self.schema_from_scratch - or click.confirm( - "Found '{}' in pipeline but not in schema. {}".format(p_key_nice, add_it_nice), True + or Confirm.ask( + ":sparkles: Found [white bold]'params.{}'[/] in pipeline but not in schema! [blue]Add to JSON Schema?".format( + p_key + ) ) ): self.schema["properties"][p_key] = self.build_schema_param(p_val) log.debug("Adding '{}' to JSON Schema".format(p_key)) params_added.append(p_key) - if len(params_added) > 0: - log.info( - "Added {} params to JSON Schema that were found with `nextflow config`:\n {}".format( - len(params_added), ", ".join(params_added) - ) - ) return params_added diff --git a/setup.py b/setup.py index 43de115ff1..4c3c4e9096 100644 --- a/setup.py +++ b/setup.py @@ -40,7 +40,7 @@ "pyyaml", "requests", "requests_cache", - "rich", + "rich>=3.4.0", "tabulate", ], setup_requires=["twine>=1.11.0", "setuptools>=38.6."],