Skip to content

Commit fd286fb

Browse files
authored
Refactor all wrappers to pass an argument list to Session.call_module (#3132)
1 parent b15a38a commit fd286fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+155
-160
lines changed

pygmt/figure.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from pygmt.clib import Session
1919
from pygmt.exceptions import GMTError, GMTInvalidInput
2020
from pygmt.helpers import (
21-
build_arg_string,
21+
build_arg_list,
2222
fmt_docstring,
2323
kwargs_to_strings,
2424
launch_external_viewer,
@@ -108,7 +108,7 @@ def _activate_figure(self):
108108
# Passing format '-' tells pygmt.end to not produce any files.
109109
fmt = "-"
110110
with Session() as lib:
111-
lib.call_module(module="figure", args=f"{self._name} {fmt}")
111+
lib.call_module(module="figure", args=[self._name, fmt])
112112

113113
def _preprocess(self, **kwargs):
114114
"""
@@ -234,15 +234,12 @@ def psconvert(self, **kwargs):
234234
# Default cropping the figure to True
235235
if kwargs.get("A") is None:
236236
kwargs["A"] = ""
237-
# Manually handle prefix -F argument so spaces aren't converted to \040
238-
# by build_arg_string function. For more information, see
239-
# https://github.com/GenericMappingTools/pygmt/pull/1487
240-
prefix = kwargs.pop("F", None)
237+
238+
prefix = kwargs.get("F")
241239
if prefix in ["", None, False, True]:
242240
raise GMTInvalidInput(
243241
"The 'prefix' parameter must be specified with a valid value."
244242
)
245-
prefix_arg = f'-F"{prefix}"'
246243

247244
# check if the parent directory exists
248245
prefix_path = Path(prefix).parent
@@ -252,9 +249,7 @@ def psconvert(self, **kwargs):
252249
)
253250

254251
with Session() as lib:
255-
lib.call_module(
256-
module="psconvert", args=f"{prefix_arg} {build_arg_string(kwargs)}"
257-
)
252+
lib.call_module(module="psconvert", args=build_arg_list(kwargs))
258253

259254
def savefig( # noqa: PLR0912
260255
self,

pygmt/helpers/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ def data_kind(data=None, x=None, y=None, z=None, required_z=False, required_data
174174
'image'
175175
"""
176176
# determine the data kind
177-
if isinstance(data, str | pathlib.PurePath):
177+
if isinstance(data, str | pathlib.PurePath) or (
178+
isinstance(data, list | tuple)
179+
and all(isinstance(_file, str | pathlib.PurePath) for _file in data)
180+
):
181+
# One or more files
178182
kind = "file"
179183
elif isinstance(data, bool | int | float) or (data is None and not required_data):
180184
kind = "arg"

pygmt/session_management.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def begin():
2323

2424
prefix = "pygmt-session"
2525
with Session() as lib:
26-
lib.call_module(module="begin", args=prefix)
26+
lib.call_module(module="begin", args=[prefix])
2727
# pygmt relies on GMT modern mode with GMT_COMPATIBILITY at version 6
28-
lib.call_module(module="set", args="GMT_COMPATIBILITY 6")
28+
lib.call_module(module="set", args=["GMT_COMPATIBILITY=6"])
2929

3030

3131
def end():
@@ -38,4 +38,4 @@ def end():
3838
``pygmt.begin``), and bring the figures to the working directory.
3939
"""
4040
with Session() as lib:
41-
lib.call_module(module="end", args="")
41+
lib.call_module(module="end", args=[])

pygmt/src/basemap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from pygmt.clib import Session
6-
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
6+
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
77

88

99
@fmt_docstring
@@ -84,4 +84,4 @@ def basemap(self, **kwargs):
8484
"""
8585
kwargs = self._preprocess(**kwargs)
8686
with Session() as lib:
87-
lib.call_module(module="basemap", args=build_arg_string(kwargs))
87+
lib.call_module(module="basemap", args=build_arg_list(kwargs))

pygmt/src/binstats.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from pygmt.clib import Session
6-
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
6+
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
77

88

99
@fmt_docstring
@@ -109,6 +109,6 @@ def binstats(data, outgrid: str | None = None, **kwargs):
109109
):
110110
kwargs["G"] = voutgrd
111111
lib.call_module(
112-
module="binstats", args=build_arg_string(kwargs, infile=vintbl)
112+
module="binstats", args=build_arg_list(kwargs, infile=vintbl)
113113
)
114114
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)

pygmt/src/blockm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pandas as pd
99
from pygmt.clib import Session
1010
from pygmt.helpers import (
11-
build_arg_string,
11+
build_arg_list,
1212
fmt_docstring,
1313
kwargs_to_strings,
1414
use_alias,
@@ -61,7 +61,7 @@ def _blockm(
6161
):
6262
lib.call_module(
6363
module=block_method,
64-
args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl),
64+
args=build_arg_list(kwargs, infile=vintbl, outfile=vouttbl),
6565
)
6666
return lib.virtualfile_to_dataset(
6767
vfname=vouttbl, output_type=output_type, column_names=column_names

pygmt/src/coast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pygmt.exceptions import GMTInvalidInput
77
from pygmt.helpers import (
88
args_in_kwargs,
9-
build_arg_string,
9+
build_arg_list,
1010
fmt_docstring,
1111
kwargs_to_strings,
1212
use_alias,
@@ -227,4 +227,4 @@ def coast(self, **kwargs):
227227
lakes, land, water, rivers, borders, dcw, Q, or shorelines"""
228228
)
229229
with Session() as lib:
230-
lib.call_module(module="coast", args=build_arg_string(kwargs))
230+
lib.call_module(module="coast", args=build_arg_list(kwargs))

pygmt/src/colorbar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from pygmt.clib import Session
6-
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
6+
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
77

88
__doctest_skip__ = ["colorbar"]
99

@@ -146,4 +146,4 @@ def colorbar(self, **kwargs):
146146
"""
147147
kwargs = self._preprocess(**kwargs)
148148
with Session() as lib:
149-
lib.call_module(module="colorbar", args=build_arg_string(kwargs))
149+
lib.call_module(module="colorbar", args=build_arg_list(kwargs))

pygmt/src/config.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,10 @@ def __init__(self, **kwargs):
199199
self.old_defaults[key] = lib.get_default(key)
200200

201201
# call gmt set to change GMT defaults
202-
arg_str = " ".join([f'{key}="{value}"' for key, value in kwargs.items()])
203202
with Session() as lib:
204-
lib.call_module(module="set", args=arg_str)
203+
lib.call_module(
204+
module="set", args=[f"{key}={value}" for key, value in kwargs.items()]
205+
)
205206

206207
def __enter__(self):
207208
"""
@@ -213,8 +214,8 @@ def __exit__(self, exc_type, exc_value, traceback):
213214
"""
214215
Revert GMT configurations to initial values.
215216
"""
216-
arg_str = " ".join(
217-
[f'{key}="{value}"' for key, value in self.old_defaults.items()]
218-
)
219217
with Session() as lib:
220-
lib.call_module(module="set", args=arg_str)
218+
lib.call_module(
219+
module="set",
220+
args=[f"{key}={value}" for key, value in self.old_defaults.items()],
221+
)

pygmt/src/contour.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from pygmt.clib import Session
6-
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
6+
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
77

88

99
@fmt_docstring
@@ -119,5 +119,5 @@ def contour(self, data=None, x=None, y=None, z=None, **kwargs):
119119
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
120120
) as vintbl:
121121
lib.call_module(
122-
module="contour", args=build_arg_string(kwargs, infile=vintbl)
122+
module="contour", args=build_arg_list(kwargs, infile=vintbl)
123123
)

0 commit comments

Comments
 (0)