Skip to content

Commit

Permalink
Use Rich.prompt Confirm.ask instead of click.confirm
Browse files Browse the repository at this point in the history
  • Loading branch information
ewels committed Jul 23, 2020
1 parent 38776e5 commit 1b61878
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 46 deletions.
29 changes: 10 additions & 19 deletions nf_core/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 == {}:
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
37 changes: 11 additions & 26 deletions nf_core/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"pyyaml",
"requests",
"requests_cache",
"rich",
"rich>=3.4.0",
"tabulate",
],
setup_requires=["twine>=1.11.0", "setuptools>=38.6."],
Expand Down

0 comments on commit 1b61878

Please sign in to comment.