|
4 | 4 |
|
5 | 5 | import io
|
6 | 6 | import os
|
7 |
| -import pathlib |
8 | 7 | import shutil
|
9 | 8 | import string
|
10 | 9 | import subprocess
|
|
17 | 16 | from typing import Any, Literal
|
18 | 17 |
|
19 | 18 | import xarray as xr
|
| 19 | +from pygmt._typing import PathLike |
20 | 20 | from pygmt.encodings import charset
|
21 | 21 | from pygmt.exceptions import GMTInvalidInput
|
22 | 22 |
|
@@ -387,10 +387,10 @@ def data_kind(
|
387 | 387 | match data:
|
388 | 388 | case None if required: # No data provided and required=True.
|
389 | 389 | kind = "empty"
|
390 |
| - case str() | pathlib.PurePath(): # One file. |
| 390 | + case str() | os.PathLike(): # One file. |
391 | 391 | kind = "file"
|
392 | 392 | case list() | tuple() if all(
|
393 |
| - isinstance(_file, str | pathlib.PurePath) for _file in data |
| 393 | + isinstance(_file, str | os.PathLike) for _file in data |
394 | 394 | ): # A list/tuple of files.
|
395 | 395 | kind = "file"
|
396 | 396 | case io.StringIO():
|
@@ -482,8 +482,8 @@ def non_ascii_to_octal(argstr: str, encoding: Encoding = "ISOLatin1+") -> str:
|
482 | 482 | def build_arg_list( # noqa: PLR0912
|
483 | 483 | kwdict: dict[str, Any],
|
484 | 484 | confdict: Mapping[str, Any] | None = None,
|
485 |
| - infile: str | pathlib.PurePath | Sequence[str | pathlib.PurePath] | None = None, |
486 |
| - outfile: str | pathlib.PurePath | None = None, |
| 485 | + infile: PathLike | Sequence[PathLike] | None = None, |
| 486 | + outfile: PathLike | None = None, |
487 | 487 | ) -> list[str]:
|
488 | 488 | r"""
|
489 | 489 | Convert keyword dictionaries and input/output files into a list of GMT arguments.
|
@@ -581,19 +581,19 @@ def build_arg_list( # noqa: PLR0912
|
581 | 581 | gmt_args.extend(f"--{key}={value}" for key, value in confdict.items())
|
582 | 582 |
|
583 | 583 | if infile: # infile can be a single file or a list of files
|
584 |
| - if isinstance(infile, str | pathlib.PurePath): |
585 |
| - gmt_args = [str(infile), *gmt_args] |
| 584 | + if isinstance(infile, str | os.PathLike): |
| 585 | + gmt_args = [os.fspath(infile), *gmt_args] |
586 | 586 | else:
|
587 |
| - gmt_args = [str(_file) for _file in infile] + gmt_args |
| 587 | + gmt_args = [os.fspath(_file) for _file in infile] + gmt_args |
588 | 588 | if outfile is not None:
|
589 | 589 | if (
|
590 |
| - not isinstance(outfile, str | pathlib.PurePath) |
591 |
| - or str(outfile) in {"", ".", ".."} |
592 |
| - or str(outfile).endswith(("/", "\\")) |
| 590 | + not isinstance(outfile, str | os.PathLike) |
| 591 | + or os.fspath(outfile) in {"", ".", ".."} |
| 592 | + or os.fspath(outfile).endswith(("/", "\\")) |
593 | 593 | ):
|
594 | 594 | msg = f"Invalid output file name '{outfile}'."
|
595 | 595 | raise GMTInvalidInput(msg)
|
596 |
| - gmt_args.append(f"->{outfile}") |
| 596 | + gmt_args.append(f"->{os.fspath(outfile)}") |
597 | 597 | return gmt_args
|
598 | 598 |
|
599 | 599 |
|
@@ -632,7 +632,7 @@ def is_nonstr_iter(value: Any) -> bool:
|
632 | 632 | return isinstance(value, Iterable) and not isinstance(value, str)
|
633 | 633 |
|
634 | 634 |
|
635 |
| -def launch_external_viewer(fname: str, waiting: float = 0) -> None: |
| 635 | +def launch_external_viewer(fname: PathLike, waiting: float = 0) -> None: |
636 | 636 | """
|
637 | 637 | Open a file in an external viewer program.
|
638 | 638 |
|
|
0 commit comments