@@ -630,42 +630,43 @@ def is_nonstr_iter(value):
630
630
return isinstance (value , Iterable ) and not isinstance (value , str )
631
631
632
632
633
- def launch_external_viewer (fname , waiting = 0 ):
633
+ def launch_external_viewer (fname : str , waiting : float = 0 ):
634
634
"""
635
635
Open a file in an external viewer program.
636
636
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.
640
639
641
640
Parameters
642
641
----------
643
- fname : str
642
+ fname
644
643
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.
645
647
"""
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
648
649
run_args = {
649
650
"stdout" : subprocess .DEVNULL ,
650
651
"stderr" : subprocess .DEVNULL ,
651
652
}
652
653
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 } " )
666
666
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.
669
670
time .sleep (waiting )
670
671
671
672
0 commit comments