Skip to content

Commit 1b500da

Browse files
committed
Figure.timestamp: Refactor to use the new alias system
1 parent 082b962 commit 1b500da

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

Diff for: pygmt/src/timestamp.py

+32-33
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,18 @@
22
timestamp - Plot the GMT timestamp logo.
33
"""
44

5-
from __future__ import annotations
6-
75
import warnings
8-
from typing import TYPE_CHECKING
6+
from collections.abc import Sequence
97

108
from packaging.version import Version
9+
from pygmt.alias import Alias, AliasSystem
1110
from pygmt.clib import Session, __gmt_version__
12-
from pygmt.helpers import build_arg_list, kwargs_to_strings
13-
14-
if TYPE_CHECKING:
15-
from collections.abc import Sequence
16-
11+
from pygmt.helpers import build_arg_list
1712

1813
__doctest_skip__ = ["timestamp"]
1914

2015

21-
@kwargs_to_strings(offset="sequence")
16+
# ruff: noqa: ARG001
2217
def timestamp(
2318
self,
2419
text: str | None = None,
@@ -81,35 +76,39 @@ def timestamp(
8176
>>> fig.timestamp(label="Powered by PyGMT")
8277
>>> fig.show()
8378
"""
84-
self._preprocess()
79+
alias = AliasSystem(
80+
U=[
81+
Alias("label"),
82+
Alias("justify", prefix="+j"),
83+
Alias("offset", prefix="+o", separator="/"),
84+
Alias("text", prefix="+t"),
85+
]
86+
)
8587

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

92-
if Version(__gmt_version__) <= Version("6.4.0") and "/" not in str(offset):
93-
# Giving a single offset doesn't work in GMT <= 6.4.0.
90+
# Workarounds for bugs/missing features for GMT<=6.4.0
91+
if Version(__gmt_version__) <= Version("6.4.0"):
92+
# Giving a single offset doesn't work.
9493
# See https://github.com/GenericMappingTools/gmt/issues/7107.
95-
offset = f"{offset}/{offset}"
96-
kwdict["U"] += f"+o{offset}"
97-
98-
# The +t modifier was added in GMT 6.5.0.
99-
# See https://github.com/GenericMappingTools/gmt/pull/7127.
100-
if text is not None:
101-
if len(str(text)) > 64:
102-
msg = (
103-
"Argument of 'text' must be no longer than 64 characters. "
104-
"The given text string will be truncated to 64 characters."
105-
)
106-
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
107-
if Version(__gmt_version__) <= Version("6.4.0"):
108-
# workaround for GMT<=6.4.0 by overriding the 'timefmt' parameter
94+
if "/" not in str(offset):
95+
offset = (offset, offset)
96+
# The +t modifier was added in GMT 6.5.0.
97+
# See https://github.com/GenericMappingTools/gmt/pull/7127.
98+
if text is not None:
99+
# Overriding the 'timefmt' parameter and set 'text' to None
109100
timefmt = text[:64]
110-
else:
111-
kwdict["U"] += f"+t{text}"
101+
text = None
102+
103+
if text is not None and len(text) > 64:
104+
msg = (
105+
"Argument of 'text' must be no longer than 64 characters. "
106+
"The given text string will be truncated to 64 characters."
107+
)
108+
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
109+
text = text[:64]
112110

111+
kwdict: dict = {"T": True, "U": True} | alias.kwdict
113112
with Session() as lib:
114113
lib.call_module(
115114
module="plot",

0 commit comments

Comments
 (0)