Skip to content

Commit 0607bc5

Browse files
authored
clib: Refactor the function clib_names using match statements (#3069)
1 parent d2e0c08 commit 0607bc5

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

pygmt/clib/loading.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,36 @@ def load_libgmt(lib_fullnames=None):
6262
return libgmt
6363

6464

65-
def clib_names(os_name):
65+
def clib_names(os_name: str) -> list[str]:
6666
"""
67-
Return the name of GMT's shared library for the current OS.
67+
Return the name(s) of GMT's shared library for the current operating system.
6868
6969
Parameters
7070
----------
71-
os_name : str
71+
os_name
7272
The operating system name as given by ``sys.platform``.
7373
7474
Returns
7575
-------
76-
libnames : list of str
76+
libnames
7777
List of possible names of GMT's shared library.
78+
79+
Raises
80+
------
81+
GMTOSError
82+
If the operating system is not supported yet.
7883
"""
79-
if os_name.startswith(("linux", "freebsd")):
80-
libnames = ["libgmt.so"]
81-
elif os_name == "darwin": # Darwin is macOS
82-
libnames = ["libgmt.dylib"]
83-
elif os_name == "win32":
84-
libnames = ["gmt.dll", "gmt_w64.dll", "gmt_w32.dll"]
85-
else:
86-
raise GMTOSError(f"Operating system '{os_name}' not supported.")
84+
match os_name:
85+
case "linux": # Linux
86+
libnames = ["libgmt.so"]
87+
case "darwin": # macOS
88+
libnames = ["libgmt.dylib"]
89+
case "win32": # Windows
90+
libnames = ["gmt.dll", "gmt_w64.dll", "gmt_w32.dll"]
91+
case name if name.startswith("freebsd"): # FreeBSD
92+
libnames = ["libgmt.so"]
93+
case _:
94+
raise GMTOSError(f"Operating system '{os_name}' is not supported.")
8795
return libnames
8896

8997

pygmt/tests/test_clib_loading.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def test_clib_names():
5050
"""
5151
Make sure we get the correct library name for different OS names.
5252
"""
53-
for linux in ["linux", "linux2", "linux3"]:
54-
assert clib_names(linux) == ["libgmt.so"]
53+
assert clib_names("linux") == ["libgmt.so"]
5554
assert clib_names("darwin") == ["libgmt.dylib"]
5655
assert clib_names("win32") == ["gmt.dll", "gmt_w64.dll", "gmt_w32.dll"]
5756
for freebsd in ["freebsd10", "freebsd11", "freebsd12"]:

0 commit comments

Comments
 (0)