Skip to content

Commit

Permalink
feat: Change the style of logging status to console (#2384)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Nov 16, 2023
1 parent 31bb450 commit cef67e9
Show file tree
Hide file tree
Showing 24 changed files with 67 additions and 142 deletions.
29 changes: 9 additions & 20 deletions src/pdm/cli/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,18 @@ def check_lockfile(project: Project, raise_not_exist: bool = True) -> str | None
if not project.lockfile.exists():
if raise_not_exist:
raise ProjectError("Lockfile does not exist, nothing to install")
project.core.ui.echo("Lockfile does not exist", style="warning", err=True)
project.core.ui.warn("Lockfile does not exist")
return "all"
compat = project.lockfile.compatibility()
if compat == Compatibility.NONE:
project.core.ui.echo("Lockfile is not compatible with PDM", style="warning", err=True)
project.core.ui.warn("Lockfile is not compatible with PDM")
return "reuse"
elif compat == Compatibility.BACKWARD:
project.core.ui.echo("Lockfile is generated on an older version of PDM", style="warning", err=True)
project.core.ui.warn("Lockfile is generated on an older version of PDM")
elif compat == Compatibility.FORWARD:
project.core.ui.echo("Lockfile is generated on a newer version of PDM", style="warning", err=True)
project.core.ui.warn("Lockfile is generated on a newer version of PDM")
elif not project.is_lockfile_hash_match():
project.core.ui.echo(
"Lockfile hash doesn't match pyproject.toml, packages may be outdated",
style="warning",
err=True,
)
project.core.ui.warn("Lockfile hash doesn't match pyproject.toml, packages may be outdated")
return "reuse"
return None

Expand Down Expand Up @@ -253,15 +249,8 @@ def print_pep582_command(project: Project, shell: str = "AUTO") -> None:
try:
set_env_in_reg("PYTHONPATH", pep582_path)
except PermissionError:
ui.echo(
"Permission denied, please run the terminal as administrator.",
style="error",
err=True,
)
ui.echo(
"The environment variable has been saved, please restart the session to take effect.",
style="success",
)
ui.error("Permission denied, please run the terminal as administrator.")
ui.info("The environment variable has been saved, please restart the session to take effect.")
return
lib_path = pep582_path.replace("'", "\\'")
if shell == "AUTO":
Expand Down Expand Up @@ -360,15 +349,15 @@ def check_update(project: Project) -> None: # pragma: no cover
install_command = f"{sys.executable} -m {install_command}"

message = [
f"\nPDM [primary]{this_version}[/]",
f"PDM [primary]{this_version}[/]",
f" is installed, while [primary]{latest_version}[/]",
" is available.\n",
f"Please run [req]`{install_command}`[/]",
" to upgrade.\n",
f"Run [req]`{disable_command}`[/]",
" to disable the check.",
]
project.core.ui.echo("".join(message), err=True, style="info")
project.core.ui.info("".join(message))


# Moved functions
Expand Down
8 changes: 2 additions & 6 deletions src/pdm/cli/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def do_add(
lock_groups = ["default"] if project.lockfile.empty() else project.lockfile.groups
if lock_groups is not None and group not in lock_groups:
if project.enable_write_lockfile:
project.core.ui.echo(f"Adding group [success]{group}[/] to lockfile", err=True, style="info")
project.core.ui.info(f"Adding group [success]{group}[/] to lockfile")
lock_groups.append(group)
if (
group == "default"
Expand All @@ -129,11 +129,7 @@ def do_add(
raise PdmUsageError("Cannot add editables to the default or optional dependency group")
for r in [parse_requirement(line, True) for line in editables] + [parse_requirement(line) for line in packages]:
if project.name and normalize_name(project.name) == r.key and not r.extras:
project.core.ui.echo(
f"Package [req]{project.name}[/] is the project itself.",
err=True,
style="warning",
)
project.core.ui.warn(f"Package [req]{project.name}[/] is the project itself.")
continue
if r.is_file_or_url:
r.relocate(project.backend) # type: ignore[attr-defined]
Expand Down
18 changes: 6 additions & 12 deletions src/pdm/cli/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ def _get_config(self, project: Project, options: argparse.Namespace) -> None:
from findpython import ALL_PROVIDERS

if options.key in project.project_config.deprecated: # pragma: no cover
project.core.ui.echo(
f"DEPRECATED: the config has been renamed to {project.project_config.deprecated[options.key]}",
style="warning",
err=True,
project.core.ui.warn(
f"[warning]DEPRECATED:[/] the config has been renamed to {project.project_config.deprecated[options.key]}",
)
options.key = project.project_config.deprecated[options.key]
try:
Expand All @@ -96,10 +94,8 @@ def _get_config(self, project: Project, options: argparse.Namespace) -> None:
def _set_config(self, project: Project, options: argparse.Namespace) -> None:
config = project.project_config if options.local else project.global_config
if options.key in config.deprecated: # pragma: no cover
project.core.ui.echo(
f"DEPRECATED: the config has been renamed to {config.deprecated[options.key]}",
style="warning",
err=True,
project.core.ui.warn(
f"[warning]DEPRECATED:[/] the config has been renamed to {config.deprecated[options.key]}",
)
config[options.key] = options.value

Expand Down Expand Up @@ -176,9 +172,7 @@ def _list_config(self, project: Project, options: argparse.Namespace) -> None:
def _delete_config(self, project: Project, options: argparse.Namespace) -> None:
config = project.project_config if options.local else project.global_config
if options.key in config.deprecated: # pragma: no cover
project.core.ui.echo(
f"DEPRECATED: the config has been renamed to {config.deprecated[options.key]}",
style="warning",
err=True,
project.core.ui.warn(
f"[warning]DEPRECATED:[/] the config has been renamed to {config.deprecated[options.key]}",
)
del config[options.key]
4 changes: 1 addition & 3 deletions src/pdm/cli/commands/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
else:
if not project.lockfile.exists():
raise PdmUsageError("No lockfile found, please run `pdm lock` first.")
project.core.ui.echo(
project.core.ui.warn(
"The exported requirements file is no longer cross-platform. "
"Using it on other platforms may cause unexpected result.",
style="warning",
err=True,
)
candidates = resolve_candidates_from_lockfile(project, requirements.values())
# Remove candidates with [extras] because the bare candidates are already
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/cli/commands/fix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def check_problems(project: Project, strict: bool = True) -> None:
if not problems:
return
breaking = False
project.core.ui.echo("[warning]WARNING:[/] The following problems are found in your project:", err=True)
project.core.ui.warn("The following problems are found in your project:")
for name, fixer in problems:
project.core.ui.echo(f" [b]{name}[/]: {fixer.get_message()}", err=True)
if fixer.breaking:
Expand Down
15 changes: 5 additions & 10 deletions src/pdm/cli/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ def _init_cookiecutter(self, project: Project, options: argparse.Namespace) -> N
if not options.template:
raise PdmUsageError("template argument is required when --cookiecutter is passed")
if options.project_path:
project.core.ui.echo(
project.core.ui.warn(
"Cookiecutter generator does not respect --project option. "
"It will always create a project dir under the current directory",
err=True,
style="warning",
)
try:
cookiecutter.main([options.template, *options.generator_args], standalone_mode=False)
Expand Down Expand Up @@ -247,16 +245,13 @@ def set_python(self, project: Project, python: str | None, hooks: HookManager) -
path = project._create_virtualenv()
python_info = PythonInfo.from_path(get_venv_python(path))
except Exception as e: # pragma: no cover
project.core.ui.echo(
f"Error occurred when creating virtualenv: {e}\nPlease fix it and create later.",
style="error",
err=True,
project.core.ui.error(
f"Error occurred when creating virtualenv: {e}\nPlease fix it and create later."
)
if python_info.get_venv() is None:
project.core.ui.echo(
project.core.ui.info(
"You are using the PEP 582 mode, no virtualenv is created.\n"
"For more info, please visit https://peps.python.org/pep-0582/",
style="success",
"For more info, please visit https://peps.python.org/pep-0582/"
)
project.python = python_info

Expand Down
4 changes: 1 addition & 3 deletions src/pdm/cli/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,9 @@ def _group_of(name: str) -> set[str]:
try:
ui.echo(text_body, highlight=True)
except UnicodeEncodeError:
ui.echo(
ui.error(
"Markdown output contains non-ASCII characters. "
"Setting env var PYTHONIOENCODING to 'utf8' may fix this.",
err=True,
style="error",
)
ui.echo(text_body.encode().decode("ascii", errors="ignore"), highlight=True)
ui.echo("**Problem decoding file as UTF-8. Some characters may be omit.**")
Expand Down
6 changes: 1 addition & 5 deletions src/pdm/cli/commands/publish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,7 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
logger.debug("Response from %s:\n%s %s", resp.url, resp.status_code, resp.reason)

if options.skip_existing and self._skip_upload(resp):
project.core.ui.echo(
f"Skipping {package.base_filename} because it appears to already exist",
style="warning",
err=True,
)
project.core.ui.warn(f"Skipping {package.base_filename} because it appears to already exist")
continue
self._check_response(resp)
uploaded.append(package)
Expand Down
4 changes: 1 addition & 3 deletions src/pdm/cli/commands/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ def do_remove(
if not dry_run:
project.pyproject.write()
if lock_groups and group not in lock_groups:
project.core.ui.echo(
f"Group [success]{group}[/] isn't in lockfile, skipping lock.", style="warning", err=True
)
project.core.ui.warn(f"Group [success]{group}[/] isn't in lockfile, skipping lock.")
return
do_lock(project, "reuse", dry_run=dry_run, hooks=hooks, groups=lock_groups)
if sync:
Expand Down
6 changes: 1 addition & 5 deletions src/pdm/cli/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,7 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
if options.site_packages:
runner.global_options.update({"site_packages": options.site_packages})
if not options.script:
project.core.ui.echo(
"No command is given, default to the Python REPL.",
style="warning",
err=True,
)
project.core.ui.warn("No command is given, default to the Python REPL.")
options.script = "python"
hooks.try_emit("pre_run", script=options.script, args=options.args)
exit_code = runner.run(options.script, options.args)
Expand Down
6 changes: 1 addition & 5 deletions src/pdm/cli/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
with project.environment.get_finder() as finder:
best_match = finder.find_best_match(package, allow_prereleases=True)
if not best_match.applicable:
project.core.ui.echo(
f"No match found for the package {package!r}",
err=True,
style="warning",
)
project.core.ui.warn(f"No match found for the package {package!r}")
return
latest = Candidate.from_installation_candidate(best_match.best, parse_requirement(package))
latest_stable = next(filter(filter_stable, best_match.applicable), None)
Expand Down
10 changes: 2 additions & 8 deletions src/pdm/cli/commands/use.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,11 @@ def version_matcher(py_version: PythonInfo) -> bool:
path = use_cache.get(python)
cached_python = PythonInfo.from_path(path)
if not cached_python.valid:
project.core.ui.echo(
project.core.ui.error(
f"The last selection is corrupted. {path!r}",
style="error",
err=True,
)
elif version_matcher(cached_python):
project.core.ui.echo(
"Using the last selection, add '-i' to ignore it.",
style="warning",
err=True,
)
project.core.ui.info("Using the last selection, add '-i' to ignore it.")
return cached_python

found_interpreters = list(dict.fromkeys(project.find_interpreters(python)))
Expand Down
10 changes: 3 additions & 7 deletions src/pdm/cli/commands/venv/activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,15 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
# Use what is saved in .pdm-python
interpreter = project._saved_python
if not interpreter:
project.core.ui.echo(
"The project doesn't have a saved python.path. Run [success]pdm use[/] to pick one.",
style="warning",
err=True,
project.core.ui.warn(
"The project doesn't have a saved python.path. Run [success]pdm use[/] to pick one."
)
raise SystemExit(1)
venv_like = VirtualEnv.from_interpreter(Path(interpreter))
if venv_like is None:
project.core.ui.echo(
project.core.ui.warn(
f"Can't activate a non-venv Python [success]{interpreter}[/], "
"you can specify one with [success]pdm venv activate <env_name>[/]",
style="warning",
err=True,
)
raise SystemExit(1)
venv = venv_like
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/cli/commands/venv/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
return

if not options.force:
project.core.ui.echo("The following Virtualenvs will be purged:", style="error")
project.core.ui.echo("The following Virtualenvs will be purged:", style="warning")
for i, venv in enumerate(all_central_venvs):
project.core.ui.echo(f"{i}. [success]{venv[0]}[/]")

Expand Down
4 changes: 1 addition & 3 deletions src/pdm/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,7 @@ def use_venv(project: Project, name: str) -> None:
from pdm.environments import PythonEnvironment

venv = get_venv_with_name(project, cast(str, name))
project.core.ui.echo(
f"In virtual environment: [success]{venv.root}[/]", err=True, style="info", verbosity=termui.Verbosity.DETAIL
)
project.core.ui.info(f"In virtual environment: [success]{venv.root}[/]", verbosity=termui.Verbosity.DETAIL)
project.environment = PythonEnvironment(project, python=str(venv.interpreter))


Expand Down
11 changes: 2 additions & 9 deletions src/pdm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,7 @@ def main(
err=True,
)
if should_show_tb:
self.ui.echo(
"Add '-v' to see the detailed traceback",
style="warning",
err=True,
verbosity=termui.Verbosity.NORMAL,
)
self.ui.warn("Add '-v' to see the detailed traceback", verbosity=termui.Verbosity.NORMAL)
sys.exit(1)
else:
if project.config["check_update"] and not is_in_zipapp():
Expand Down Expand Up @@ -283,10 +278,8 @@ def my_plugin(core: pdm.core.Core) -> None:
try:
plugin.load()(self)
except Exception as e:
self.ui.echo(
self.ui.error(
f"Failed to load plugin {plugin.name}={plugin.value}: {e}",
style="error",
err=True,
)


Expand Down
2 changes: 1 addition & 1 deletion src/pdm/formats/flit.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def warn_against_dynamic_version_or_docstring(self, source: Path, version: str,
"They are probably imported from other files which is not supported by PDM."
" You may need to supply their values in pyproject.toml manually."
)
self._ui.echo(message, err=True, style="warning")
self._ui.warn(message)

@convert_from("metadata")
def name(self, metadata: dict[str, Any]) -> str:
Expand Down
6 changes: 2 additions & 4 deletions src/pdm/models/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,10 @@ def _prompt_for_password(self, netloc: str) -> tuple[str | None, str | None, boo

def _should_save_password_to_keyring(self) -> bool:
if get_keyring_provider() is None:
self.ui.echo(
self.ui.info(
"The provided credentials will not be saved into the keyring.\n"
"You can enable this by installing keyring:\n"
" [success]pdm self add keyring[/]",
err=True,
style="info",
" [success]pdm self add keyring[/]"
)
return super()._should_save_password_to_keyring()

Expand Down
4 changes: 1 addition & 3 deletions src/pdm/models/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,10 @@ def search(self, query: str) -> SearchResult:
session = finder.session
resp = session.get(search_url, params={"q": query})
if resp.status_code == 404:
self.environment.project.core.ui.echo(
self.environment.project.core.ui.warn(
f"{pypi_simple!r} doesn't support '/search' endpoint, fallback "
f"to {self.DEFAULT_INDEX_URL!r} now.\n"
"This may take longer depending on your network condition.",
err=True,
style="warning",
)
resp = session.get(f"{self.DEFAULT_INDEX_URL}/search", params={"q": query})
parser = SearchResultParser()
Expand Down
10 changes: 2 additions & 8 deletions src/pdm/project/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,7 @@ def __setitem__(self, key: str, value: Any) -> None:

value = config.coerce(value)
if key in self.env_map:
ui.echo(
f"WARNING: the config is shadowed by env var '{config.env_var}', the value set won't take effect.",
style="warning",
)
ui.warn(f"the config is shadowed by env var '{config.env_var}', the value set won't take effect.")
self._file_data[config_key] = value
if config.replace:
self._file_data.pop(config.replace, None)
Expand Down Expand Up @@ -375,10 +372,7 @@ def __delitem__(self, key: str) -> None:

env_var = config.env_var
if env_var is not None and env_var in os.environ:
ui.echo(
f"WARNING: the config is shadowed by env var '{env_var}', set value won't take effect.",
style="warning",
)
ui.warn(f"The config is shadowed by env var '{env_var}', set value won't take effect.")
self._save_config()

def get_repository_config(self, name_or_url: str, prefix: str) -> RepositoryConfig | None:
Expand Down
Loading

0 comments on commit cef67e9

Please sign in to comment.