Skip to content

Commit 8c54b69

Browse files
authored
Warn on command execution using custom styled format (#652)
* Warn on command execution using custom styled format * Fix formatting for python 2.7, increment warning stacklevel
1 parent fc76215 commit 8c54b69

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

planet/api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ class ClientV1DeprecationWarning(FutureWarning):
4040
"For more details please see the discussion at "
4141
"https://github.com/planetlabs/planet-client-python/discussions.",
4242
ClientV1DeprecationWarning,
43+
stacklevel=2
4344
)

planet/scripts/cli.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
import click
1616
import logging
1717
import sys
18+
import textwrap
19+
import warnings
20+
21+
# Importing planet.api triggers a deprecation warning. In the planet
22+
# command and its sub-commands we will intercept and ignore the
23+
# warning and instead show users a detailed warning in the command
24+
# usage text, and a short warning on command execution.
25+
warnings.filterwarnings("ignore", module="planet.api")
1826

1927
from planet import api
2028
from planet.api.__version__ import __version__
@@ -64,7 +72,35 @@ def configure_logging(verbosity):
6472
' - Default https://api.planet.com/')
6573
@click.version_option(version=__version__, message='%(version)s')
6674
def cli(context, verbose, api_key, base_url, workers):
67-
'''Planet API Client'''
75+
"""Planet API Client
76+
77+
Attention!
78+
79+
You are using version 1 of the Planet CLI. A pre-release of version
80+
2 is available at https://pypi.org/project/planet/. Version 2 will
81+
not be backward compatible. Changes are planned for the arguments,
82+
options, and return values of subcommands. For more information
83+
about the new version, please join the discussion at
84+
https://github.com/planetlabs/planet-client- python/discussions.
85+
86+
"""
87+
warnmsg = click.style(
88+
"\n".join(
89+
textwrap.wrap(
90+
"Attention! You are using version 1 of the Planet CLI. "
91+
"Version 2 will not be backward compatible. "
92+
"Please see `planet --help` for details. "
93+
"This warning may be disabled by setting "
94+
"`PYTHONWARNINGS=ignore` in your environment.",
95+
width=80,
96+
)
97+
),
98+
fg="yellow",
99+
)
100+
prev_formatwarning = warnings.formatwarning
101+
warnings.formatwarning = lambda msg, *args, **kwargs: "{}\n".format(msg)
102+
warnings.warn(warnmsg, UserWarning)
103+
warnings.formatwarning = prev_formatwarning
68104

69105
configure_logging(verbose)
70106

0 commit comments

Comments
 (0)