|
46 | 46 | import pathlib
|
47 | 47 | import shutil
|
48 | 48 | import stat
|
49 |
| -from typing import IO, Any, Callable, Iterable, List, Optional |
| 49 | +import sys |
| 50 | +from typing import IO, Any, Callable, Iterable, List, Optional, TypeVar, Union |
50 | 51 |
|
51 | 52 | # this package
|
52 | 53 | from domdf_python_tools.stringlist import StringList
|
|
67 | 68 | "PosixPathPlus",
|
68 | 69 | "WindowsPathPlus",
|
69 | 70 | "in_directory",
|
| 71 | + "_P", |
70 | 72 | ]
|
71 | 73 |
|
72 | 74 | newline_default = object()
|
| 75 | +_P = TypeVar('_P', bound=pathlib.PurePath) |
| 76 | +""" |
| 77 | +.. versionadded:: 0.11.0 |
| 78 | +""" |
73 | 79 |
|
74 | 80 |
|
75 | 81 | def append(var: str, filename: PathLike, **kwargs) -> int:
|
@@ -589,6 +595,121 @@ def load_json(
|
589 | 595 | **kwargs,
|
590 | 596 | )
|
591 | 597 |
|
| 598 | + if sys.version_info < (3, 7): |
| 599 | + |
| 600 | + def is_mount(self) -> bool: |
| 601 | + """ |
| 602 | + Check if this path is a POSIX mount point. |
| 603 | +
|
| 604 | + :rtype: |
| 605 | +
|
| 606 | + .. versionadded:: 0.3.8 for Python 3.7 and above |
| 607 | + .. versionadded:: 0.11.0 for Python 3.6 |
| 608 | + """ |
| 609 | + |
| 610 | + # Need to exist and be a dir |
| 611 | + if not self.exists() or not self.is_dir(): |
| 612 | + return False |
| 613 | + |
| 614 | + parent = pathlib.Path(self.parent) |
| 615 | + try: |
| 616 | + parent_dev = parent.stat().st_dev |
| 617 | + except OSError: |
| 618 | + return False |
| 619 | + |
| 620 | + dev = self.stat().st_dev |
| 621 | + if dev != parent_dev: |
| 622 | + return True |
| 623 | + ino = self.stat().st_ino |
| 624 | + parent_ino = parent.stat().st_ino |
| 625 | + return ino == parent_ino |
| 626 | + |
| 627 | + if sys.version_info < (3, 8): |
| 628 | + |
| 629 | + def rename(self: _P, target: Union[str, pathlib.PurePath]) -> _P: # type: ignore |
| 630 | + """ |
| 631 | + Rename this path to the target path. |
| 632 | +
|
| 633 | + The target path may be absolute or relative. Relative paths are |
| 634 | + interpreted relative to the current working directory, *not* the |
| 635 | + directory of the Path object. |
| 636 | +
|
| 637 | + :param target: |
| 638 | +
|
| 639 | + :returns: The new Path instance pointing to the target path. |
| 640 | +
|
| 641 | + .. versionadded:: 0.3.8 for Python 3.8 and above |
| 642 | + .. versionadded:: 0.11.0 for Python 3.6 and Python 3.7 |
| 643 | + """ |
| 644 | + |
| 645 | + self._accessor.rename(self, target) # type: ignore |
| 646 | + return self.__class__(target) |
| 647 | + |
| 648 | + def replace(self: _P, target: Union[str, pathlib.PurePath]) -> _P: # type: ignore |
| 649 | + """ |
| 650 | + Rename this path to the target path, overwriting if that path exists. |
| 651 | +
|
| 652 | + The target path may be absolute or relative. Relative paths are |
| 653 | + interpreted relative to the current working directory, *not* the |
| 654 | + directory of the Path object. |
| 655 | +
|
| 656 | + Returns the new Path instance pointing to the target path. |
| 657 | +
|
| 658 | + :param target: |
| 659 | +
|
| 660 | + :returns: The new Path instance pointing to the target path. |
| 661 | +
|
| 662 | + .. versionadded:: 0.3.8 for Python 3.8 and above |
| 663 | + .. versionadded:: 0.11.0 for Python 3.6 and Python 3.7 |
| 664 | + """ |
| 665 | + |
| 666 | + self._accessor.replace(self, target) # type: ignore |
| 667 | + return self.__class__(target) |
| 668 | + |
| 669 | + def unlink(self, missing_ok: bool = False) -> None: |
| 670 | + """ |
| 671 | + Remove this file or link. |
| 672 | +
|
| 673 | + If the path is a directory, use :meth:`~domdf_python_tools.paths.PathPlus.rmdir()` instead. |
| 674 | +
|
| 675 | + .. versionadded:: 0.3.8 for Python 3.8 and above |
| 676 | + .. versionadded:: 0.11.0 for Python 3.6 and Python 3.7 |
| 677 | + """ |
| 678 | + |
| 679 | + try: |
| 680 | + self._accessor.unlink(self) # type: ignore |
| 681 | + except FileNotFoundError: |
| 682 | + if not missing_ok: |
| 683 | + raise |
| 684 | + |
| 685 | + def link_to(self, target: Union[str, bytes, os.PathLike[str]]) -> None: |
| 686 | + """ |
| 687 | + Create a hard link pointing to a path named target. |
| 688 | +
|
| 689 | + :param target: |
| 690 | +
|
| 691 | + .. versionadded:: 0.3.8 for Python 3.8 and above |
| 692 | + .. versionadded:: 0.11.0 for Python 3.6 and Python 3.7 |
| 693 | + """ |
| 694 | + |
| 695 | + self._accessor.link_to(self, target) # type: ignore |
| 696 | + |
| 697 | + if sys.version_info < (3, 9): |
| 698 | + |
| 699 | + def __enter__(self): |
| 700 | + return self |
| 701 | + |
| 702 | + def __exit__(self, t, v, tb): |
| 703 | + # https://bugs.python.org/issue39682 |
| 704 | + # In previous versions of pathlib, this method marked this path as |
| 705 | + # closed; subsequent attempts to perform I/O would raise an IOError. |
| 706 | + # This functionality was never documented, and had the effect of |
| 707 | + # making Path objects mutable, contrary to PEP 428. In Python 3.9 the |
| 708 | + # _closed attribute was removed, and this method made a no-op. |
| 709 | + # This method and __enter__()/__exit__() should be deprecated and |
| 710 | + # removed in the future. |
| 711 | + pass |
| 712 | + |
592 | 713 |
|
593 | 714 | class PosixPathPlus(PathPlus, pathlib.PurePosixPath):
|
594 | 715 | """
|
|
0 commit comments