Skip to content

Warn on command execution using custom styled format #652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions planet/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ class ClientV1DeprecationWarning(FutureWarning):
"For more details please see the discussion at "
"https://github.com/planetlabs/planet-client-python/discussions.",
ClientV1DeprecationWarning,
stacklevel=2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, the warning will point to the module that imports planet.api instead of to planet.api itself.

)
38 changes: 37 additions & 1 deletion planet/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
import click
import logging
import sys
import textwrap
import warnings

# Importing planet.api triggers a deprecation warning. In the planet
# command and its sub-commands we will intercept and ignore the
# warning and instead show users a detailed warning in the command
# usage text, and a short warning on command execution.
warnings.filterwarnings("ignore", module="planet.api")

from planet import api
from planet.api.__version__ import __version__
Expand Down Expand Up @@ -64,7 +72,35 @@ def configure_logging(verbosity):
' - Default https://api.planet.com/')
@click.version_option(version=__version__, message='%(version)s')
def cli(context, verbose, api_key, base_url, workers):
'''Planet API Client'''
"""Planet API Client

Attention!

You are using version 1 of the Planet CLI. A pre-release of version
2 is available at https://pypi.org/project/planet/. Version 2 will
not be backward compatible. Changes are planned for the arguments,
options, and return values of subcommands. For more information
about the new version, please join the discussion at
https://github.com/planetlabs/planet-client- python/discussions.

"""
warnmsg = click.style(
"\n".join(
textwrap.wrap(
"Attention! You are using version 1 of the Planet CLI. "
"Version 2 will not be backward compatible. "
"Please see `planet --help` for details. "
"This warning may be disabled by setting "
"`PYTHONWARNINGS=ignore` in your environment.",
width=80,
)
),
fg="yellow",
)
prev_formatwarning = warnings.formatwarning
warnings.formatwarning = lambda msg, *args, **kwargs: "{}\n".format(msg)
warnings.warn(warnmsg, UserWarning)
warnings.formatwarning = prev_formatwarning
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, the warning was sent to the terminal using click.echo, but this doesn't provide an easy way to disable the warning. Using Python's warning system, as above, lets us use PYTHONWARNINGS=ignore.


configure_logging(verbose)

Expand Down