Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor all wrappers to pass an argument list to Session.call_module #3132

Merged
merged 8 commits into from
Apr 18, 2024
15 changes: 5 additions & 10 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pygmt.clib import Session
from pygmt.exceptions import GMTError, GMTInvalidInput
from pygmt.helpers import (
build_arg_string,
build_arg_list,
fmt_docstring,
kwargs_to_strings,
launch_external_viewer,
Expand Down Expand Up @@ -108,7 +108,7 @@ def _activate_figure(self):
# Passing format '-' tells pygmt.end to not produce any files.
fmt = "-"
with Session() as lib:
lib.call_module(module="figure", args=f"{self._name} {fmt}")
lib.call_module(module="figure", args=[self._name, fmt])

def _preprocess(self, **kwargs):
"""
Expand Down Expand Up @@ -234,15 +234,12 @@ def psconvert(self, **kwargs):
# Default cropping the figure to True
if kwargs.get("A") is None:
kwargs["A"] = ""
# Manually handle prefix -F argument so spaces aren't converted to \040
# by build_arg_string function. For more information, see
# https://github.com/GenericMappingTools/pygmt/pull/1487
prefix = kwargs.pop("F", None)

prefix = kwargs.get("F")
if prefix in ["", None, False, True]:
raise GMTInvalidInput(
"The 'prefix' parameter must be specified with a valid value."
)
prefix_arg = f'-F"{prefix}"'

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

with Session() as lib:
lib.call_module(
module="psconvert", args=f"{prefix_arg} {build_arg_string(kwargs)}"
)
lib.call_module(module="psconvert", args=build_arg_list(kwargs))

def savefig( # noqa: PLR0912
self,
Expand Down
6 changes: 5 additions & 1 deletion pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ def data_kind(data=None, x=None, y=None, z=None, required_z=False, required_data
'image'
"""
# determine the data kind
if isinstance(data, str | pathlib.PurePath):
if isinstance(data, str | pathlib.PurePath) or (
isinstance(data, list | tuple)
and all(isinstance(_file, str | pathlib.PurePath) for _file in data)
):
# One or more files
kind = "file"
elif isinstance(data, bool | int | float) or (data is None and not required_data):
kind = "arg"
Expand Down
6 changes: 3 additions & 3 deletions pygmt/session_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def begin():

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


def end():
Expand All @@ -38,4 +38,4 @@ def end():
``pygmt.begin``), and bring the figures to the working directory.
"""
with Session() as lib:
lib.call_module(module="end", args="")
lib.call_module(module="end", args=[])
4 changes: 2 additions & 2 deletions pygmt/src/basemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from pygmt.clib import Session
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias


@fmt_docstring
Expand Down Expand Up @@ -84,4 +84,4 @@ def basemap(self, **kwargs):
"""
kwargs = self._preprocess(**kwargs)
with Session() as lib:
lib.call_module(module="basemap", args=build_arg_string(kwargs))
lib.call_module(module="basemap", args=build_arg_list(kwargs))
4 changes: 2 additions & 2 deletions pygmt/src/binstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from pygmt.clib import Session
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias


@fmt_docstring
Expand Down Expand Up @@ -109,6 +109,6 @@ def binstats(data, outgrid: str | None = None, **kwargs):
):
kwargs["G"] = voutgrd
lib.call_module(
module="binstats", args=build_arg_string(kwargs, infile=vintbl)
module="binstats", args=build_arg_list(kwargs, infile=vintbl)
)
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)
4 changes: 2 additions & 2 deletions pygmt/src/blockm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pandas as pd
from pygmt.clib import Session
from pygmt.helpers import (
build_arg_string,
build_arg_list,
fmt_docstring,
kwargs_to_strings,
use_alias,
Expand Down Expand Up @@ -61,7 +61,7 @@ def _blockm(
):
lib.call_module(
module=block_method,
args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl),
args=build_arg_list(kwargs, infile=vintbl, outfile=vouttbl),
)
return lib.virtualfile_to_dataset(
vfname=vouttbl, output_type=output_type, column_names=column_names
Expand Down
4 changes: 2 additions & 2 deletions pygmt/src/coast.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import (
args_in_kwargs,
build_arg_string,
build_arg_list,
fmt_docstring,
kwargs_to_strings,
use_alias,
Expand Down Expand Up @@ -227,4 +227,4 @@ def coast(self, **kwargs):
lakes, land, water, rivers, borders, dcw, Q, or shorelines"""
)
with Session() as lib:
lib.call_module(module="coast", args=build_arg_string(kwargs))
lib.call_module(module="coast", args=build_arg_list(kwargs))
4 changes: 2 additions & 2 deletions pygmt/src/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from pygmt.clib import Session
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias

__doctest_skip__ = ["colorbar"]

Expand Down Expand Up @@ -146,4 +146,4 @@ def colorbar(self, **kwargs):
"""
kwargs = self._preprocess(**kwargs)
with Session() as lib:
lib.call_module(module="colorbar", args=build_arg_string(kwargs))
lib.call_module(module="colorbar", args=build_arg_list(kwargs))
13 changes: 7 additions & 6 deletions pygmt/src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,10 @@ def __init__(self, **kwargs):
self.old_defaults[key] = lib.get_default(key)

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

def __enter__(self):
"""
Expand All @@ -213,8 +214,8 @@ def __exit__(self, exc_type, exc_value, traceback):
"""
Revert GMT configurations to initial values.
"""
arg_str = " ".join(
[f'{key}="{value}"' for key, value in self.old_defaults.items()]
)
with Session() as lib:
lib.call_module(module="set", args=arg_str)
lib.call_module(
module="set",
args=[f"{key}={value}" for key, value in self.old_defaults.items()],
)
4 changes: 2 additions & 2 deletions pygmt/src/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from pygmt.clib import Session
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias


@fmt_docstring
Expand Down Expand Up @@ -119,5 +119,5 @@ def contour(self, data=None, x=None, y=None, z=None, **kwargs):
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
) as vintbl:
lib.call_module(
module="contour", args=build_arg_string(kwargs, infile=vintbl)
module="contour", args=build_arg_list(kwargs, infile=vintbl)
)
4 changes: 2 additions & 2 deletions pygmt/src/dimfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias

__doctest_skip__ = ["dimfilter"]

Expand Down Expand Up @@ -148,6 +148,6 @@ def dimfilter(grid, outgrid: str | None = None, **kwargs):
):
kwargs["G"] = voutgrd
lib.call_module(
module="dimfilter", args=build_arg_string(kwargs, infile=vingrd)
module="dimfilter", args=build_arg_list(kwargs, infile=vingrd)
)
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)
4 changes: 2 additions & 2 deletions pygmt/src/filter1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import (
build_arg_string,
build_arg_list,
fmt_docstring,
use_alias,
validate_output_table_type,
Expand Down Expand Up @@ -121,6 +121,6 @@ def filter1d(
):
lib.call_module(
module="filter1d",
args=build_arg_string(kwargs, infile=vintbl, outfile=vouttbl),
args=build_arg_list(kwargs, infile=vintbl, outfile=vouttbl),
)
return lib.virtualfile_to_dataset(vfname=vouttbl, output_type=output_type)
6 changes: 3 additions & 3 deletions pygmt/src/grd2cpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias

__doctest_skip__ = ["grd2cpt"]

Expand Down Expand Up @@ -186,10 +186,10 @@ def grd2cpt(grid, **kwargs):
with Session() as lib:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
if kwargs.get("H") is None: # if no output is set
arg_str = build_arg_string(kwargs, infile=vingrd)
arg_str = build_arg_list(kwargs, infile=vingrd)
else: # if output is set
outfile, kwargs["H"] = kwargs["H"], True
if not outfile or not isinstance(outfile, str):
raise GMTInvalidInput("'output' should be a proper file name.")
arg_str = build_arg_string(kwargs, infile=vingrd, outfile=outfile)
arg_str = build_arg_list(kwargs, infile=vingrd, outfile=outfile)
lib.call_module(module="grd2cpt", args=arg_str)
4 changes: 2 additions & 2 deletions pygmt/src/grd2xyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import (
build_arg_string,
build_arg_list,
fmt_docstring,
kwargs_to_strings,
use_alias,
Expand Down Expand Up @@ -165,7 +165,7 @@ def grd2xyz(
):
lib.call_module(
module="grd2xyz",
args=build_arg_string(kwargs, infile=vingrd, outfile=vouttbl),
args=build_arg_list(kwargs, infile=vingrd, outfile=vouttbl),
)
return lib.virtualfile_to_dataset(
vfname=vouttbl, output_type=output_type, column_names=column_names
Expand Down
4 changes: 2 additions & 2 deletions pygmt/src/grdclip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from pygmt.clib import Session
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias

__doctest_skip__ = ["grdclip"]

Expand Down Expand Up @@ -94,6 +94,6 @@ def grdclip(grid, outgrid: str | None = None, **kwargs):
):
kwargs["G"] = voutgrd
lib.call_module(
module="grdclip", args=build_arg_string(kwargs, infile=vingrd)
module="grdclip", args=build_arg_list(kwargs, infile=vingrd)
)
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)
4 changes: 2 additions & 2 deletions pygmt/src/grdcontour.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from pygmt.clib import Session
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias

__doctest_skip__ = ["grdcontour"]

Expand Down Expand Up @@ -125,5 +125,5 @@ def grdcontour(self, grid, **kwargs):
with Session() as lib:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
lib.call_module(
module="grdcontour", args=build_arg_string(kwargs, infile=vingrd)
module="grdcontour", args=build_arg_list(kwargs, infile=vingrd)
)
4 changes: 2 additions & 2 deletions pygmt/src/grdcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pygmt.clib import Session
from pygmt.helpers import (
GMTTempFile,
build_arg_string,
build_arg_list,
fmt_docstring,
kwargs_to_strings,
use_alias,
Expand Down Expand Up @@ -105,7 +105,7 @@ def grdcut(grid, **kwargs):
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module(
module="grdcut", args=build_arg_string(kwargs, infile=vingrd)
module="grdcut", args=build_arg_list(kwargs, infile=vingrd)
)

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
4 changes: 2 additions & 2 deletions pygmt/src/grdfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias

__doctest_skip__ = ["grdfill"]

Expand Down Expand Up @@ -77,6 +77,6 @@ def grdfill(grid, outgrid: str | None = None, **kwargs):
):
kwargs["G"] = voutgrd
lib.call_module(
module="grdfill", args=build_arg_string(kwargs, infile=vingrd)
module="grdfill", args=build_arg_list(kwargs, infile=vingrd)
)
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)
4 changes: 2 additions & 2 deletions pygmt/src/grdfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from pygmt.clib import Session
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias


@fmt_docstring
Expand Down Expand Up @@ -131,6 +131,6 @@ def grdfilter(grid, outgrid: str | None = None, **kwargs):
):
kwargs["G"] = voutgrd
lib.call_module(
module="grdfilter", args=build_arg_string(kwargs, infile=vingrd)
module="grdfilter", args=build_arg_list(kwargs, infile=vingrd)
)
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)
4 changes: 2 additions & 2 deletions pygmt/src/grdgradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import (
args_in_kwargs,
build_arg_string,
build_arg_list,
fmt_docstring,
kwargs_to_strings,
use_alias,
Expand Down Expand Up @@ -171,6 +171,6 @@ def grdgradient(grid, outgrid: str | None = None, **kwargs):
):
kwargs["G"] = voutgrd
lib.call_module(
module="grdgradient", args=build_arg_string(kwargs, infile=vingrd)
module="grdgradient", args=build_arg_list(kwargs, infile=vingrd)
)
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)
Loading