Skip to content

Commit eb64d55

Browse files
Figure.psconvert: Refactor the dpi/gs_path/gs_option/prefix parameters using the new alias system (#4056)
Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com>
1 parent 2f336d4 commit eb64d55

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

pygmt/src/psconvert.py

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,25 @@
22
psconvert - Convert [E]PS file(s) to other formats using Ghostscript.
33
"""
44

5+
from collections.abc import Sequence
56
from pathlib import Path
67

8+
from pygmt.alias import Alias, AliasSystem
79
from pygmt.clib import Session
810
from pygmt.exceptions import GMTValueError
9-
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
11+
from pygmt.helpers import build_arg_list, fmt_docstring, use_alias
1012

1113

1214
@fmt_docstring
13-
@use_alias(
14-
A="crop",
15-
C="gs_option",
16-
E="dpi",
17-
F="prefix",
18-
G="gs_path",
19-
I="resize",
20-
N="bb_style",
21-
T="fmt",
22-
Q="anti_aliasing",
23-
V="verbose",
24-
)
25-
@kwargs_to_strings()
26-
def psconvert(self, **kwargs):
15+
@use_alias(A="crop", I="resize", N="bb_style", T="fmt", Q="anti_aliasing", V="verbose")
16+
def psconvert(
17+
self,
18+
prefix: str | None = None,
19+
dpi: int | None = None,
20+
gs_option: str | Sequence[str] | None = None,
21+
gs_path: str | None = None,
22+
**kwargs,
23+
):
2724
r"""
2825
Convert [E]PS file(s) to other formats using Ghostscript.
2926
@@ -37,6 +34,10 @@ def psconvert(self, **kwargs):
3734
Full GMT docs at :gmt-docs:`psconvert.html`.
3835
3936
{aliases}
37+
- C = gs_option
38+
- E = dpi
39+
- F = prefix
40+
- G = gs_path
4041
4142
Parameters
4243
----------
@@ -49,19 +50,20 @@ def psconvert(self, **kwargs):
4950
creating very small images where the difference of one pixel
5051
might matter. If ``verbose`` is used we also report the
5152
dimensions of the final illustration.
52-
gs_path : str
53+
gs_path
5354
Full path to the Ghostscript executable.
54-
gs_option : str
55-
Specify a single, custom option that will be passed on to
56-
Ghostscript as is.
57-
dpi : int
58-
Set raster resolution in dpi. Default is 720 for PDF, 300 for
59-
others.
60-
prefix : str
61-
Force the output file name. By default output names are constructed
62-
using the input names as base, which are appended with an
63-
appropriate extension. Use this option to provide a different name,
64-
but without extension. Extension is still determined automatically.
55+
gs_option
56+
Specify one or a list of custom options that will be passed on to Ghostscript
57+
as is.
58+
dpi
59+
Set raster resolution in dpi [Default is 720 for PDF, 300 for others]. **Note**:
60+
Ghostscript limits the final width and height pixel dimensions of a raster file
61+
to be less than or equal to 65536.
62+
prefix
63+
Force the output file name. By default output names are constructed using the
64+
input names as base, which are appended with an appropriate extension. Use this
65+
parameter to provide a different name, but without extension. Extension is still
66+
determined automatically.
6567
resize : str
6668
[**+m**\ *margins*][**+s**\ [**m**]\ *width*\
6769
[/\ *height*]][**+S**\ *scale*].
@@ -113,7 +115,6 @@ def psconvert(self, **kwargs):
113115
if kwargs.get("A") is None:
114116
kwargs["A"] = ""
115117

116-
prefix = kwargs.get("F")
117118
if prefix in {"", None, False, True}:
118119
raise GMTValueError(
119120
prefix,
@@ -122,10 +123,17 @@ def psconvert(self, **kwargs):
122123
)
123124

124125
# Check if the parent directory exists
125-
prefix_path = Path(prefix).parent
126+
prefix_path = Path(prefix).parent # type: ignore[arg-type]
126127
if not prefix_path.exists():
127-
msg = f"No such directory: '{prefix_path}', please create it first."
128+
msg = f"No such directory: {prefix_path!r}, please create it first."
128129
raise FileNotFoundError(msg)
129130

131+
aliasdict = AliasSystem(
132+
C=Alias(gs_option, name="gs_option"),
133+
E=Alias(dpi, name="dpi"),
134+
F=Alias(prefix, name="prefix"),
135+
G=Alias(gs_path, name="gs_path"),
136+
).merge(kwargs)
137+
130138
with Session() as lib:
131-
lib.call_module(module="psconvert", args=build_arg_list(kwargs))
139+
lib.call_module(module="psconvert", args=build_arg_list(aliasdict))

0 commit comments

Comments
 (0)