10
10
import shutil
11
11
import subprocess as sp
12
12
import sys
13
+ from collections .abc import Iterator , Mapping
13
14
from ctypes .util import find_library
14
15
from pathlib import Path
15
16
16
17
from pygmt .exceptions import GMTCLibError , GMTCLibNotFoundError , GMTOSError
17
18
18
19
19
- def load_libgmt (lib_fullnames = None ):
20
+ def load_libgmt (lib_fullnames : Iterator [ str ] | None = None ) -> ctypes . CDLL :
20
21
"""
21
22
Find and load ``libgmt`` as a :py:class:`ctypes.CDLL`.
22
23
23
24
Will look for the GMT shared library in the directories determined by
24
- clib_full_names().
25
+ `` clib_full_names()`` .
25
26
26
27
Parameters
27
28
----------
28
- lib_fullnames : list of str or None
29
- List of possible full names of GMT's shared library. If ``None``, will
30
- default to ``clib_full_names()``.
29
+ lib_fullnames
30
+ List of possible full names of GMT's shared library. If ``None``, will default
31
+ to ``clib_full_names()``.
31
32
32
33
Returns
33
34
-------
34
- :py:class:`ctypes.CDLL` object
35
+ libgmt
35
36
The loaded shared library.
36
37
37
38
Raises
38
39
------
39
40
GMTCLibNotFoundError
40
- If there was any problem loading the library (couldn't find it or
41
- couldn't access the functions).
41
+ If there was any problem loading the library (couldn't find it or couldn't
42
+ access the functions).
42
43
"""
43
44
if lib_fullnames is None :
44
45
lib_fullnames = clib_full_names ()
@@ -96,20 +97,27 @@ def clib_names(os_name: str) -> list[str]:
96
97
return libnames
97
98
98
99
99
- def clib_full_names (env = None ):
100
+ def clib_full_names (env : Mapping | None = None ) -> Iterator [ str ] :
100
101
"""
101
- Return the full path of GMT's shared library for the current OS.
102
+ Return full path(s) of GMT shared library for the current operating system.
103
+
104
+ The GMT shared library is searched for in following ways, sorted by priority:
105
+
106
+ 1. Path defined by environmental variable GMT_LIBRARY_PATH
107
+ 2. Path returned by command "gmt --show-library"
108
+ 3. Path defined by environmental variable PATH (Windows only)
109
+ 4. System default search path
102
110
103
111
Parameters
104
112
----------
105
- env : dict or None
106
- A dictionary containing the environment variables. If ``None``, will
107
- default to ``os.environ``.
113
+ env
114
+ A dictionary containing the environment variables. If ``None``, will default to
115
+ ``os.environ``.
108
116
109
117
Yields
110
118
------
111
- lib_fullnames: list of str
112
- List of possible full names of GMT's shared library.
119
+ lib_fullnames
120
+ List of possible full names of GMT shared library.
113
121
"""
114
122
if env is None :
115
123
env = os .environ
@@ -118,21 +126,18 @@ def clib_full_names(env=None):
118
126
119
127
# Search for the library in different ways, sorted by priority.
120
128
# 1. Search for the library in GMT_LIBRARY_PATH if defined.
121
- libpath = env .get ("GMT_LIBRARY_PATH" , "" ) # e.g. $HOME/miniconda/envs/pygmt/lib
122
- if libpath :
129
+ if libpath := env .get ("GMT_LIBRARY_PATH" ): # e.g. $HOME/miniconda/envs/pygmt/lib
123
130
for libname in libnames :
124
131
libfullpath = Path (libpath ) / libname
125
132
if libfullpath .exists ():
126
133
yield str (libfullpath )
127
134
128
- # 2. Search for the library returned by command "gmt --show-library"
129
- # Use `str(Path(realpath))` to avoid mixture of separators "\\" and "/"
130
- if ( gmtbin := shutil .which ("gmt" )) is not None :
135
+ # 2. Search for the library returned by command "gmt --show-library".
136
+ # Use `str(Path(realpath))` to avoid mixture of separators "\\" and "/".
137
+ if gmtbin := shutil .which ("gmt" ):
131
138
try :
132
139
libfullpath = Path (
133
- sp .check_output ([gmtbin , "--show-library" ], encoding = "utf-8" ).rstrip (
134
- "\n "
135
- )
140
+ sp .check_output ([gmtbin , "--show-library" ], encoding = "utf-8" ).rstrip ()
136
141
)
137
142
if libfullpath .exists ():
138
143
yield str (libfullpath )
@@ -142,8 +147,7 @@ def clib_full_names(env=None):
142
147
# 3. Search for DLLs in PATH by calling find_library() (Windows only)
143
148
if sys .platform == "win32" :
144
149
for libname in libnames :
145
- libfullpath = find_library (libname )
146
- if libfullpath :
150
+ if libfullpath := find_library (libname ):
147
151
yield libfullpath
148
152
149
153
# 4. Search for library names in the system default path
0 commit comments