Skip to content

Commit e490616

Browse files
authored
Figure.timestamp: Refactor to use the new alias system (#4012)
1 parent 5d1a771 commit e490616

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

pygmt/src/timestamp.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
from packaging.version import Version
99
from pygmt._typing import AnchorCode
10+
from pygmt.alias import Alias, AliasSystem
1011
from pygmt.clib import Session, __gmt_version__
11-
from pygmt.helpers import build_arg_list, kwargs_to_strings
12+
from pygmt.helpers import build_arg_list
1213

1314
__doctest_skip__ = ["timestamp"]
1415

1516

16-
@kwargs_to_strings(offset="sequence")
1717
def timestamp(
1818
self,
1919
text: str | None = None,
@@ -78,39 +78,42 @@ def timestamp(
7878
"""
7979
self._activate_figure()
8080

81-
# Build the options passed to the "plot" module
82-
kwdict: dict = {"T": True, "U": ""}
83-
if label is not None:
84-
kwdict["U"] += f"{label}"
85-
kwdict["U"] += f"+j{justify}"
81+
if text is not None and len(str(text)) > 64:
82+
msg = (
83+
"Parameter 'text' expects a string no longer than 64 characters. "
84+
"The given text string will be truncated to 64 characters."
85+
)
86+
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
87+
text = str(text)[:64]
8688

87-
# TODO(GMT>=6.5.0): Remove the patch for upstream bug fixed in GMT 6.5.0.
88-
if Version(__gmt_version__) < Version("6.5.0") and "/" not in str(offset):
89-
# Giving a single offset doesn't work in GMT < 6.5.0.
90-
# See https://github.com/GenericMappingTools/gmt/issues/7107.
91-
offset = f"{offset}/{offset}"
92-
kwdict["U"] += f"+o{offset}"
89+
# TODO(GMT>=6.5.0): Remove the patch for upstream "offset" bug fixed in GMT 6.5.0.
90+
# TODO(GMT>=6.5.0): Remove the workaround for the '+t' modifier added in GMT 6.5.0.
91+
# Related issues:
92+
# - https://github.com/GenericMappingTools/gmt/issues/7107
93+
# - https://github.com/GenericMappingTools/gmt/pull/7127
94+
if Version(__gmt_version__) < Version("6.5.0"):
95+
if "/" not in str(offset): # Giving a single offset doesn't work in GMT<6.5.0
96+
offset = f"{offset}/{offset}"
97+
if text is not None:
98+
# Workaround for GMT<6.5.0 by overriding the 'timefmt' parameter and
99+
# unsetting 'text'.
100+
timefmt = str(text)
101+
text = None
93102

94-
# The +t modifier was added in GMT 6.5.0.
95-
# See https://github.com/GenericMappingTools/gmt/pull/7127.
96-
if text is not None:
97-
if len(str(text)) > 64:
98-
msg = (
99-
"Argument of 'text' must be no longer than 64 characters. "
100-
"The given text string will be truncated to 64 characters."
101-
)
102-
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
103-
# TODO(GMT>=6.5.0): Remove the workaround for the new '+t' modifier.
104-
if Version(__gmt_version__) < Version("6.5.0"):
105-
# Workaround for GMT<6.5.0 by overriding the 'timefmt' parameter
106-
timefmt = text[:64]
107-
else:
108-
kwdict["U"] += f"+t{text}"
103+
aliasdict = AliasSystem(
104+
U=[
105+
Alias(label, name="label"),
106+
Alias(justify, name="justify", prefix="+j"),
107+
Alias(offset, name="offset", prefix="+o", sep="/", size=2),
108+
Alias(text, name="text", prefix="+t"),
109+
]
110+
)
111+
aliasdict["T"] = "" # Add '-T' to the "plot" module.
109112

110113
with Session() as lib:
111114
lib.call_module(
112115
module="plot",
113116
args=build_arg_list(
114-
kwdict, confdict={"FONT_LOGO": font, "FORMAT_TIME_STAMP": timefmt}
117+
aliasdict, confdict={"FONT_LOGO": font, "FORMAT_TIME_STAMP": timefmt}
115118
),
116119
)

0 commit comments

Comments
 (0)