Skip to content
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

Figure.timestamp: Let the 'text' parameter work for GMT<=6.4.0 and raise a warning for long text #2425

Merged
merged 2 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 11 additions & 7 deletions pygmt/src/timestamp.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""
timestamp - Plot the GMT timestamp logo.
"""
import warnings

from packaging.version import Version
from pygmt import __gmt_version__
from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import build_arg_string, is_nonstr_iter

__doctest_skip__ = ["timestamp"]
Expand All @@ -28,7 +29,6 @@ def timestamp(
If ``None``, the current UNIX timestamp is shown in the GMT timestamp
logo. Set this parameter to replace the UNIX timestamp with a
custom text string instead. The text must be less than 64 characters.
*Requires GMT>=6.5.0*.
label : None or str
The text string shown after the GMT timestamp logo.
justification : str
Expand Down Expand Up @@ -85,13 +85,17 @@ def timestamp(
# The +t modifier was added in GMT 6.5.0.
# See https://github.com/GenericMappingTools/gmt/pull/7127.
if text is not None:
if Version(__gmt_version__) < Version("6.5.0"):
raise GMTInvalidInput("The parameter 'text' requires GMT>=6.5.0.")
if len(str(text)) > 64:
raise GMTInvalidInput(
"The parameter 'text' must be less than 64 characters."
msg = (
"Argument of 'text' must be no longer than 64 characters. "
"The text string will be truncated to 64 characters."
seisman marked this conversation as resolved.
Show resolved Hide resolved
)
kwdict["U"] += f"+t{text}"
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
if Version(__gmt_version__) <= Version("6.4.0"):
# workaround for GMT<=6.4.0 by overriding the 'timefmt' parameter
timefmt = text[:64]
else:
kwdict["U"] += f"+t{text}"

with Session() as lib:
lib.call_module(
Expand Down
4 changes: 4 additions & 0 deletions pygmt/tests/baseline/test_timestamp_text_truncated.png.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
outs:
- md5: 14b0d80e2b31ee0c7fb61cf0b9f4f73f
size: 3491
path: test_timestamp_text_truncated.png
23 changes: 16 additions & 7 deletions pygmt/tests/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
Tests for Figure.timestamp.
"""
import pytest
from packaging.version import Version
from pygmt import Figure, __gmt_version__, config
from pygmt import Figure, config


@pytest.fixture(scope="module", name="faketime")
Expand Down Expand Up @@ -72,19 +71,29 @@ def test_timestamp_font(faketime):
return fig


@pytest.mark.skipif(
Version(__gmt_version__) < Version("6.5.0"),
reason="The 'text' parameter requires GMT>=6.5.0",
)
@pytest.mark.mpl_image_compare(filename="test_timestamp.png")
def test_timestamp_text(faketime):
"""
Test if the "text" parameter works.
"""
fig = Figure()
fig.timestamp(text=faketime)
return fig


@pytest.mark.mpl_image_compare
def test_timestamp_text_truncated():
"""
Passing a text string longer than 64 characters raises a warning and the
string will be truncated.

Requires GMT>=6.5.0.
"""
fig = Figure()
fig.timestamp(text=faketime)
with pytest.warns(expected_warning=RuntimeWarning) as record:
# a string with 70 characters will be truncated to 64 characters
fig.timestamp(text="0123456789" * 7)
assert len(record) == 1 # check that only one warning was raised
return fig


Expand Down