Skip to content

Commit

Permalink
Figure.timestamp: Refactor to use the new alias system
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed May 10, 2024
1 parent ba53b9c commit 05dd80e
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions pygmt/src/timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@
timestamp - Plot the GMT timestamp logo.
"""

from __future__ import annotations

import warnings
from typing import TYPE_CHECKING
from collections.abc import Sequence

from packaging.version import Version
from pygmt.alias import Alias, AliasSystem
from pygmt.clib import Session, __gmt_version__
from pygmt.helpers import build_arg_list, kwargs_to_strings

if TYPE_CHECKING:
from collections.abc import Sequence

from pygmt.helpers import build_arg_list

__doctest_skip__ = ["timestamp"]


@kwargs_to_strings(offset="sequence")
# ruff: noqa: ARG001
def timestamp(
self,
text: str | None = None,
Expand Down Expand Up @@ -81,35 +76,39 @@ def timestamp(
>>> fig.timestamp(label="Powered by PyGMT")
>>> fig.show()
"""
self._preprocess()
alias = AliasSystem(
U=[
Alias("label"),
Alias("justify", prefix="+j"),
Alias("offset", prefix="+o", separator="/"),
Alias("text", prefix="+t"),
]
)

# Build the options passed to the "plot" module
kwdict: dict = {"T": True, "U": ""}
if label is not None:
kwdict["U"] += f"{label}"
kwdict["U"] += f"+j{justify}"
self._preprocess()

if Version(__gmt_version__) <= Version("6.4.0") and "/" not in str(offset):
# Giving a single offset doesn't work in GMT <= 6.4.0.
# Workarounds for bugs/missing features for GMT<=6.4.0
if Version(__gmt_version__) <= Version("6.4.0"):
# Giving a single offset doesn't work.
# See https://github.com/GenericMappingTools/gmt/issues/7107.
offset = f"{offset}/{offset}"
kwdict["U"] += f"+o{offset}"

# The +t modifier was added in GMT 6.5.0.
# See https://github.com/GenericMappingTools/gmt/pull/7127.
if text is not None:
if len(str(text)) > 64:
msg = (
"Argument of 'text' must be no longer than 64 characters. "
"The given text string will be truncated to 64 characters."
)
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
if "/" not in str(offset):
offset = (offset, offset)

Check warning on line 95 in pygmt/src/timestamp.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/timestamp.py#L94-L95

Added lines #L94 - L95 were not covered by tests
# The +t modifier was added in GMT 6.5.0.
# See https://github.com/GenericMappingTools/gmt/pull/7127.
if text is not None:

Check warning on line 98 in pygmt/src/timestamp.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/timestamp.py#L98

Added line #L98 was not covered by tests
# Overriding the 'timefmt' parameter and set 'text' to None
timefmt = text[:64]
else:
kwdict["U"] += f"+t{text}"
text = None

Check warning on line 101 in pygmt/src/timestamp.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/timestamp.py#L101

Added line #L101 was not covered by tests

if text is not None and len(text) > 64:
msg = (
"Argument of 'text' must be no longer than 64 characters. "
"The given text string will be truncated to 64 characters."
)
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
text = text[:64]

kwdict: dict = {"T": True, "U": True} | alias.kwdict
with Session() as lib:
lib.call_module(
module="plot",
Expand Down

0 comments on commit 05dd80e

Please sign in to comment.