Skip to content

Commit 9202cf1

Browse files
authored
Merge pull request #5813 from pypa/more-secho-cleanup
Remove click.{s,}echo
2 parents f34449c + f63469a commit 9202cf1

File tree

1 file changed

+52
-83
lines changed

1 file changed

+52
-83
lines changed

pipenv/cli/command.py

Lines changed: 52 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,24 @@
2121
upgrade_options,
2222
verbose_option,
2323
)
24+
from pipenv.utils import console, err
2425
from pipenv.utils.environment import load_dot_env
2526
from pipenv.utils.processes import subprocess_run
26-
from pipenv.vendor import click
2727
from pipenv.vendor.click import (
2828
Choice,
2929
argument,
30-
echo,
3130
edit,
3231
group,
3332
option,
3433
pass_context,
35-
secho,
36-
style,
3734
version_option,
3835
)
3936

37+
with console.capture() as capture:
38+
console.print("[bold]pipenv[/bold]")
39+
40+
prog_name = capture.get()
41+
4042
subcommand_context = CONTEXT_SETTINGS.copy()
4143
subcommand_context.update({"ignore_unknown_options": True, "allow_extra_args": True})
4244
subcommand_context_no_interspersion = subcommand_context.copy()
@@ -61,7 +63,7 @@
6163
help="Output diagnostic information for use in GitHub issues.",
6264
)
6365
@general_options
64-
@version_option(prog_name=style("pipenv", bold=True), version=__version__)
66+
@version_option(prog_name=prog_name, version=__version__)
6567
@pass_state
6668
@pass_context
6769
def cli(
@@ -79,7 +81,6 @@ def cli(
7981
site_packages=None,
8082
**kwargs,
8183
):
82-
from pipenv.patched.pip._vendor import rich
8384
from pipenv.utils.shell import system_which
8485

8586
load_dot_env(state.project, quiet=state.quiet)
@@ -90,10 +91,9 @@ def cli(
9091
from pipenv.utils.virtualenv import cleanup_virtualenv, do_where, warn_in_virtualenv
9192

9293
if "PIPENV_COLORBLIND" in os.environ:
93-
echo(
94+
err.print(
9495
"PIPENV_COLORBLIND is deprecated, use NO_COLOR"
9596
" per https://no-color.org/ instead",
96-
err=True,
9797
)
9898

9999
if man:
@@ -102,25 +102,20 @@ def cli(
102102
os.execle(system_which("man"), "man", path, os.environ)
103103
return 0
104104
else:
105-
secho(
106-
"man does not appear to be available on your system.",
107-
fg="yellow",
108-
bold=True,
109-
err=True,
105+
err.print(
106+
"man does not appear to be available on your system.", style="bold yellow"
110107
)
111108
return 1
112109
if envs:
113-
echo("The following environment variables can be set, to do various things:\n")
110+
console.print(
111+
"The following environment variables can be set, to do various things:\n"
112+
)
114113
for key in state.project.__dict__:
115114
if key.startswith("PIPENV"):
116-
echo(f" - {style(key, bold=True)}")
117-
echo(
118-
"\nYou can learn more at:\n {}".format(
119-
style(
120-
"https://pipenv.pypa.io/en/latest/advanced/#configuration-with-environment-variables",
121-
fg="green",
122-
)
123-
)
115+
console.print(f" - {key}", style="bold")
116+
console.print(
117+
"\nYou can learn more at:\n "
118+
"[green]https://pipenv.pypa.io/en/latest/advanced/#configuration-with-environment-variables[/green]",
124119
)
125120
return 0
126121
warn_in_virtualenv(state.project)
@@ -146,57 +141,39 @@ def cli(
146141
elif venv:
147142
# There is no virtualenv yet.
148143
if not state.project.virtualenv_exists:
149-
echo(
150-
"{}({}){}".format(
151-
style(
152-
"No virtualenv has been created for this project", fg="red"
153-
),
154-
style(state.project.project_directory, bold=True),
155-
style(" yet!", fg="red"),
156-
),
157-
err=True,
144+
err.print(
145+
"[red]No virtualenv has been created for this project[/red]"
146+
f"[bold]{state.project.project_directory}[/bold]"
147+
" [red]yet![/red]"
158148
)
159149
ctx.abort()
160150
else:
161-
echo(state.project.virtualenv_location)
151+
print(state.project.virtualenv_location)
162152
return 0
163153
# --rm was passed...
164154
elif rm:
165155
# Abort if --system (or running in a virtualenv).
166156
if state.project.s.PIPENV_USE_SYSTEM or environments.is_in_virtualenv():
167-
echo(
168-
style(
169-
"You are attempting to remove a virtualenv that "
170-
"Pipenv did not create. Aborting.",
171-
fg="red",
172-
)
157+
console.print(
158+
"You are attempting to remove a virtualenv that "
159+
"Pipenv did not create. Aborting.",
160+
style="red",
173161
)
174162
ctx.abort()
175163
if state.project.virtualenv_exists:
176164
loc = state.project.virtualenv_location
177-
echo(
178-
style(
179-
"{} ({})...".format(
180-
style("Removing virtualenv", bold=True),
181-
style(loc, fg="green"),
182-
)
183-
)
165+
console.print(
166+
f"[bold]Removing virtualenv[/bold] ([green]{loc}[green])..."
184167
)
185168

186-
console = rich.console.Console()
187-
# TODO: add state.project.s to spinner status
188169
with console.status("Running..."):
189170
# Remove the virtualenv.
190171
cleanup_virtualenv(state.project, bare=True)
191172
return 0
192173
else:
193-
echo(
194-
style(
195-
"No virtualenv has been created for this project yet!",
196-
fg="red",
197-
bold=True,
198-
),
199-
err=True,
174+
err.print(
175+
"No virtualenv has been created for this project yet!",
176+
style="red bold",
200177
)
201178
ctx.abort()
202179
# --python was passed...
@@ -212,7 +189,7 @@ def cli(
212189
# Check this again before exiting for empty ``pipenv`` command.
213190
elif ctx.invoked_subcommand is None:
214191
# Display help to user, if no commands were passed.
215-
echo(format_help(ctx.get_help()))
192+
console.print(format_help(ctx.get_help()))
216193

217194

218195
@cli.command(
@@ -225,7 +202,8 @@ def cli(
225202
@install_options
226203
@pass_state
227204
def install(state, **kwargs):
228-
"""Installs provided packages and adds them to Pipfile, or (if no packages are given), installs all packages from Pipfile."""
205+
"""Installs provided packages and adds them to Pipfile,
206+
or (if no packages are given), installs all packages from Pipfile."""
229207
from pipenv.routines.install import do_install
230208

231209
do_install(
@@ -403,13 +381,10 @@ def shell(
403381
# If PIPENV_ACTIVE is set, VIRTUAL_ENV should always be set too.
404382
venv_name = os.environ.get("VIRTUAL_ENV", "UNKNOWN_VIRTUAL_ENVIRONMENT")
405383
if not anyway:
406-
echo(
407-
"{} {} {}\nNew shell not activated to avoid nested environments.".format(
408-
style("Shell for"),
409-
style(venv_name, fg="green", bold=True),
410-
style("already activated.", bold=True),
411-
),
412-
err=True,
384+
err.print(
385+
f"Shell for [green bold]{venv_name}[/green bold] "
386+
"[bold]already activated[/bold].\n"
387+
"New shell not activated to avoid nested environments."
413388
)
414389
sys.exit(1)
415390
# Load .env file.
@@ -639,13 +614,13 @@ def run_open(state, module, *args, **kwargs):
639614
]
640615
)
641616
if c.returncode:
642-
echo(style("Module not found!", fg="red"))
617+
console.print("Module not found!", style="red")
643618
sys.exit(1)
644619
if "__init__.py" in c.stdout:
645620
p = os.path.dirname(c.stdout.strip().rstrip("cdo"))
646621
else:
647622
p = c.stdout.strip().rstrip("cdo")
648-
echo(style(f"Opening {p!r} in your EDITOR.", bold=True))
623+
console.print(f"Opening {p!r} in your EDITOR.", style="bold")
649624
inline_activate_virtual_environment(state.project)
650625
edit(filename=p)
651626
return 0
@@ -713,7 +688,7 @@ def clean(state, dry_run=False, bare=False, user=False):
713688
def scripts(state):
714689
"""Lists scripts in current environment config."""
715690
if not state.project.pipfile_exists:
716-
echo("No Pipfile present at project home.", err=True)
691+
err.print("No Pipfile present at project home.")
717692
sys.exit(1)
718693
scripts = state.project.parsed_pipfile.get("scripts", {})
719694
first_column_width = max(len(word) for word in ["Command"] + list(scripts))
@@ -724,7 +699,7 @@ def scripts(state):
724699
"{0:<{width}} {1}".format(name, script, width=first_column_width)
725700
for name, script in scripts.items()
726701
)
727-
echo("\n".join(line for line in lines))
702+
console.print("\n".join(line for line in lines))
728703

729704

730705
@cli.command(
@@ -735,17 +710,14 @@ def scripts(state):
735710
def verify(state):
736711
"""Verify the hash in Pipfile.lock is up-to-date."""
737712
if not state.project.pipfile_exists:
738-
echo("No Pipfile present at project home.", err=True)
713+
err.print("No Pipfile present at project home.")
739714
sys.exit(1)
740715
if state.project.get_lockfile_hash() != state.project.calculate_pipfile_hash():
741-
echo(
742-
"Pipfile.lock is out-of-date. Run {} to update.".format(
743-
style("$ pipenv lock", fg="yellow", bold=True)
744-
),
745-
err=True,
716+
err.print(
717+
"Pipfile.lock is out-of-date. Run [yellow bold]$ pipenv lock[/yellow bold] to update."
746718
)
747719
sys.exit(1)
748-
echo(style("Pipfile.lock is up-to-date.", fg="green"))
720+
console.print("Pipfile.lock is up-to-date.", style="green")
749721
sys.exit(0)
750722

751723

@@ -787,17 +759,14 @@ def requirements(
787759

788760
def do_py(project, ctx=None, system=False):
789761
if not project.virtualenv_exists:
790-
click.echo(
791-
"{}({}){}".format(
792-
click.style("No virtualenv has been created for this project ", fg="red"),
793-
click.style(project.project_directory, fg="yellow", bold=True),
794-
click.style(" yet!", fg="red"),
795-
),
796-
err=True,
762+
err.print(
763+
"[red]No virtualenv has been created for this project[/red] "
764+
f"[yellow bold]{project.project_directory}[/yellow bold] "
765+
"[red] yet![/red]"
797766
)
798767
ctx.abort()
799768

800769
try:
801-
click.echo(project._which("python", allow_global=system))
770+
console.print(project._which("python", allow_global=system))
802771
except AttributeError:
803-
click.echo(click.style("No project found!", fg="red"))
772+
console.print("No project found!", style="red")

0 commit comments

Comments
 (0)