Skip to content

Commit 8d6a06f

Browse files
authored
Refactor launch_external_viewer to use match-case statements (#3412)
1 parent 991cafb commit 8d6a06f

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed

pygmt/_show_versions.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ def _get_ghostscript_version() -> str | None:
5252
Get Ghostscript version.
5353
"""
5454
match sys.platform:
55-
case "linux" | "darwin":
56-
cmds = ["gs"]
57-
case os_name if os_name.startswith("freebsd"):
55+
case name if name in {"linux", "darwin"} or name.startswith("freebsd"):
5856
cmds = ["gs"]
5957
case "win32":
6058
cmds = ["gswin64c.exe", "gswin32c.exe"]

pygmt/clib/loading.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,12 @@ def clib_names(os_name: str) -> list[str]:
111111
If the operating system is not supported yet.
112112
"""
113113
match os_name:
114-
case "linux": # Linux
114+
case name if name == "linux" or name.startswith("freebsd"): # Linux or FreeBSD
115115
libnames = ["libgmt.so"]
116116
case "darwin": # macOS
117117
libnames = ["libgmt.dylib"]
118118
case "win32": # Windows
119119
libnames = ["gmt.dll", "gmt_w64.dll", "gmt_w32.dll"]
120-
case name if name.startswith("freebsd"): # FreeBSD
121-
libnames = ["libgmt.so"]
122120
case _:
123121
raise GMTOSError(f"Operating system '{os_name}' is not supported.")
124122
return libnames

pygmt/helpers/utils.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -630,42 +630,43 @@ def is_nonstr_iter(value):
630630
return isinstance(value, Iterable) and not isinstance(value, str)
631631

632632

633-
def launch_external_viewer(fname, waiting=0):
633+
def launch_external_viewer(fname: str, waiting: float = 0):
634634
"""
635635
Open a file in an external viewer program.
636636
637-
Uses the ``xdg-open`` command on Linux, the ``open`` command on macOS, the
638-
associated application on Windows, and the default web browser on other
639-
systems.
637+
Uses the ``xdg-open`` command on Linux/FreeBSD, the ``open`` command on macOS, the
638+
associated application on Windows, and the default web browser on other systems.
640639
641640
Parameters
642641
----------
643-
fname : str
642+
fname
644643
The file name of the file (preferably a full path).
644+
waiting
645+
Wait for a few seconds before exiting the function, to allow the external viewer
646+
open the file before it's deleted.
645647
"""
646-
# Redirect stdout and stderr to devnull so that the terminal isn't filled
647-
# with noise
648+
# Redirect stdout and stderr to devnull so that the terminal isn't filled with noise
648649
run_args = {
649650
"stdout": subprocess.DEVNULL,
650651
"stderr": subprocess.DEVNULL,
651652
}
652653

653-
# Open the file with the default viewer.
654-
# Fall back to the browser if can't recognize the operating system.
655-
os_name = sys.platform
656-
if os_name.startswith(("linux", "freebsd")) and (
657-
xdgopen := shutil.which("xdg-open")
658-
):
659-
subprocess.run([xdgopen, fname], check=False, **run_args)
660-
elif os_name == "darwin": # Darwin is macOS
661-
subprocess.run([shutil.which("open"), fname], check=False, **run_args)
662-
elif os_name == "win32":
663-
os.startfile(fname) # noqa: S606
664-
else:
665-
webbrowser.open_new_tab(f"file://{fname}")
654+
match sys.platform:
655+
case name if (
656+
(name == "linux" or name.startswith("freebsd"))
657+
and (xdgopen := shutil.which("xdg-open"))
658+
): # Linux/FreeBSD
659+
subprocess.run([xdgopen, fname], check=False, **run_args) # type:ignore[call-overload]
660+
case "darwin": # macOS
661+
subprocess.run([shutil.which("open"), fname], check=False, **run_args) # type:ignore[call-overload]
662+
case "win32": # Windows
663+
os.startfile(fname) # type:ignore[attr-defined] # noqa: S606
664+
case _: # Fall back to the browser if can't recognize the operating system.
665+
webbrowser.open_new_tab(f"file://{fname}")
666666
if waiting > 0:
667-
# suspend the execution for a few seconds to avoid the images being
668-
# deleted when a Python script exits
667+
# Preview images will be deleted when a GMT modern-mode session ends, but the
668+
# external viewer program may take a few seconds to open the images.
669+
# Suspend the execution for a few seconds.
669670
time.sleep(waiting)
670671

671672

0 commit comments

Comments
 (0)