Skip to content

Commit 5d697f4

Browse files
committed
clib.Session: Add type hints and improve docstrings of the Session.open_virtualfile method (#3510)
1 parent 9067edb commit 5d697f4

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

pygmt/clib/session.py

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,41 +1203,46 @@ def write_data(self, family, geometry, mode, wesn, output, data):
12031203
raise GMTCLibError(f"Failed to write dataset to '{output}'")
12041204

12051205
@contextlib.contextmanager
1206-
def open_virtualfile(self, family, geometry, direction, data):
1206+
def open_virtualfile(
1207+
self,
1208+
family: str,
1209+
geometry: str,
1210+
direction: str,
1211+
data: ctp.c_void_p | None,
1212+
) -> Generator[str, None, None]:
12071213
"""
1208-
Open a GMT virtual file to pass data to and from a module.
1214+
Open a GMT virtual file associated with a data object for reading or writing.
12091215
1210-
GMT uses a virtual file scheme to pass in data or get data from API
1211-
modules. Use it to pass in your GMT data structure (created using
1212-
:meth:`pygmt.clib.Session.create_data`) to a module that expects an
1213-
input file, or get the output from a module that writes to a file.
1216+
GMT uses a virtual file scheme to pass in data or get data from API modules. Use
1217+
it to pass in your GMT data structure (created using
1218+
:meth:`pygmt.clib.Session.create_data`) to a module that expects an input file,
1219+
or get the output from a module that writes to a file.
12141220
1215-
Use in a ``with`` block. Will automatically close the virtual file when
1216-
leaving the ``with`` block. Because of this, no wrapper for
1217-
``GMT_Close_VirtualFile`` is provided.
1221+
Use in a ``with`` block. Will automatically close the virtual file when leaving
1222+
the ``with`` block. Because of this, no wrapper for ``GMT_Close_VirtualFile``
1223+
is provided.
12181224
12191225
Parameters
12201226
----------
1221-
family : str
1222-
A valid GMT data family name (e.g., ``"GMT_IS_DATASET"``). Should
1223-
be the same as the one you used to create your data structure.
1224-
geometry : str
1225-
A valid GMT data geometry name (e.g., ``"GMT_IS_POINT"``). Should
1226-
be the same as the one you used to create your data structure.
1227-
direction : str
1228-
Either ``"GMT_IN"`` or ``"GMT_OUT"`` to indicate if passing data to
1229-
GMT or getting it out of GMT, respectively.
1230-
By default, GMT can modify the data you pass in. Add modifier
1231-
``"GMT_IS_REFERENCE"`` to tell GMT the data are read-only, or
1232-
``"GMT_IS_DUPLICATE"`` to tell GMT to duplicate the data.
1233-
data : int or None
1234-
The ctypes void pointer to your GMT data structure. For output
1235-
(i.e., ``direction="GMT_OUT"``), it can be ``None`` to have GMT
1236-
automatically allocate the output GMT data structure.
1227+
family
1228+
A valid GMT data family name (e.g., ``"GMT_IS_DATASET"``). Should be the
1229+
same as the one you used to create your data structure.
1230+
geometry
1231+
A valid GMT data geometry name (e.g., ``"GMT_IS_POINT"``). Should be the
1232+
same as the one you used to create your data structure.
1233+
direction
1234+
Either ``"GMT_IN"`` or ``"GMT_OUT"`` to indicate if passing data to GMT or
1235+
getting it out of GMT, respectively. By default, GMT can modify the data you
1236+
pass in. Add modifier ``"GMT_IS_REFERENCE"`` to tell GMT the data are
1237+
read-only, or ``"GMT_IS_DUPLICATE"`` to tell GMT to duplicate the data.
1238+
data
1239+
The ctypes void pointer to the GMT data structure. For output (i.e.,
1240+
``direction="GMT_OUT"``), it can be ``None`` to have GMT automatically
1241+
allocate the output GMT data structure.
12371242
12381243
Yields
12391244
------
1240-
vfname : str
1245+
vfname
12411246
The name of the virtual file that you can pass to a GMT module.
12421247
12431248
Examples
@@ -1270,19 +1275,19 @@ def open_virtualfile(self, family, geometry, direction, data):
12701275
c_open_virtualfile = self.get_libgmt_func(
12711276
"GMT_Open_VirtualFile",
12721277
argtypes=[
1273-
ctp.c_void_p,
1274-
ctp.c_uint,
1275-
ctp.c_uint,
1276-
ctp.c_uint,
1277-
ctp.c_void_p,
1278-
ctp.c_char_p,
1278+
ctp.c_void_p, # V_API
1279+
ctp.c_uint, # family
1280+
ctp.c_uint, # geometry
1281+
ctp.c_uint, # direction
1282+
ctp.c_void_p, # data
1283+
ctp.c_char_p, # name
12791284
],
12801285
restype=ctp.c_int,
12811286
)
12821287

12831288
c_close_virtualfile = self.get_libgmt_func(
12841289
"GMT_Close_VirtualFile",
1285-
argtypes=[ctp.c_void_p, ctp.c_char_p],
1290+
argtypes=[ctp.c_void_p, ctp.c_char_p], # V_API, name
12861291
restype=ctp.c_int,
12871292
)
12881293

@@ -1297,19 +1302,24 @@ def open_virtualfile(self, family, geometry, direction, data):
12971302
self.session_pointer, family_int, geometry_int, direction_int, data, buff
12981303
)
12991304
if status != 0:
1300-
raise GMTCLibError("Failed to create a virtual file.")
1305+
msg = (
1306+
f"Failed to create a virtual file with {family=}, {geometry=}, "
1307+
f"{direction=}."
1308+
)
1309+
raise GMTCLibError(msg)
13011310

13021311
vfname = buff.value.decode()
13031312
try:
13041313
yield vfname
13051314
finally:
13061315
status = c_close_virtualfile(self.session_pointer, vfname.encode())
13071316
if status != 0:
1308-
raise GMTCLibError(f"Failed to close virtual file '{vfname}'.")
1317+
msg = f"Failed to close virtual file '{vfname}'."
1318+
raise GMTCLibError(msg)
13091319

13101320
def open_virtual_file(self, family, geometry, direction, data):
13111321
"""
1312-
Open a GMT virtual file to pass data to and from a module.
1322+
Open a GMT virtual file associated with a data object for reading or writing.
13131323
13141324
.. deprecated: 0.11.0
13151325

0 commit comments

Comments
 (0)