|  | 
|  | 1 | +""" | 
|  | 2 | +Implementation of ReadablePath for local paths, for use in pathlib tests. | 
|  | 3 | +
 | 
|  | 4 | +LocalPathGround is also defined here. It helps establish the "ground truth" | 
|  | 5 | +about local paths in tests. | 
|  | 6 | +""" | 
|  | 7 | + | 
|  | 8 | +import os | 
|  | 9 | +import pathlib.types | 
|  | 10 | + | 
|  | 11 | +from test.support import os_helper | 
|  | 12 | +from test.test_pathlib.support.lexical_path import LexicalPath | 
|  | 13 | + | 
|  | 14 | + | 
|  | 15 | +class LocalPathGround: | 
|  | 16 | +    can_symlink = os_helper.can_symlink() | 
|  | 17 | + | 
|  | 18 | +    def __init__(self, path_cls): | 
|  | 19 | +        self.path_cls = path_cls | 
|  | 20 | + | 
|  | 21 | +    def setup(self, local_suffix=""): | 
|  | 22 | +        root = self.path_cls(os_helper.TESTFN + local_suffix) | 
|  | 23 | +        os.mkdir(root) | 
|  | 24 | +        return root | 
|  | 25 | + | 
|  | 26 | +    def teardown(self, root): | 
|  | 27 | +        os_helper.rmtree(root) | 
|  | 28 | + | 
|  | 29 | +    def create_file(self, p, data=b''): | 
|  | 30 | +        with open(p, 'wb') as f: | 
|  | 31 | +            f.write(data) | 
|  | 32 | + | 
|  | 33 | +    def create_dir(self, p): | 
|  | 34 | +        os.mkdir(p) | 
|  | 35 | + | 
|  | 36 | +    def create_symlink(self, p, target): | 
|  | 37 | +        os.symlink(target, p) | 
|  | 38 | + | 
|  | 39 | +    def create_hierarchy(self, p): | 
|  | 40 | +        os.mkdir(os.path.join(p, 'dirA')) | 
|  | 41 | +        os.mkdir(os.path.join(p, 'dirB')) | 
|  | 42 | +        os.mkdir(os.path.join(p, 'dirC')) | 
|  | 43 | +        os.mkdir(os.path.join(p, 'dirC', 'dirD')) | 
|  | 44 | +        with open(os.path.join(p, 'fileA'), 'wb') as f: | 
|  | 45 | +            f.write(b"this is file A\n") | 
|  | 46 | +        with open(os.path.join(p, 'dirB', 'fileB'), 'wb') as f: | 
|  | 47 | +            f.write(b"this is file B\n") | 
|  | 48 | +        with open(os.path.join(p, 'dirC', 'fileC'), 'wb') as f: | 
|  | 49 | +            f.write(b"this is file C\n") | 
|  | 50 | +        with open(os.path.join(p, 'dirC', 'novel.txt'), 'wb') as f: | 
|  | 51 | +            f.write(b"this is a novel\n") | 
|  | 52 | +        with open(os.path.join(p, 'dirC', 'dirD', 'fileD'), 'wb') as f: | 
|  | 53 | +            f.write(b"this is file D\n") | 
|  | 54 | +        if self.can_symlink: | 
|  | 55 | +            # Relative symlinks. | 
|  | 56 | +            os.symlink('fileA', os.path.join(p, 'linkA')) | 
|  | 57 | +            os.symlink('non-existing', os.path.join(p, 'brokenLink')) | 
|  | 58 | +            os.symlink('dirB', | 
|  | 59 | +                       os.path.join(p, 'linkB'), | 
|  | 60 | +                       target_is_directory=True) | 
|  | 61 | +            os.symlink(os.path.join('..', 'dirB'), | 
|  | 62 | +                       os.path.join(p, 'dirA', 'linkC'), | 
|  | 63 | +                       target_is_directory=True) | 
|  | 64 | +            # Broken symlink (pointing to itself). | 
|  | 65 | +            os.symlink('brokenLinkLoop', os.path.join(p, 'brokenLinkLoop')) | 
|  | 66 | + | 
|  | 67 | +    isdir = staticmethod(os.path.isdir) | 
|  | 68 | +    isfile = staticmethod(os.path.isfile) | 
|  | 69 | +    islink = staticmethod(os.path.islink) | 
|  | 70 | +    readlink = staticmethod(os.readlink) | 
|  | 71 | + | 
|  | 72 | +    def readtext(self, p): | 
|  | 73 | +        with open(p, 'r') as f: | 
|  | 74 | +            return f.read() | 
|  | 75 | + | 
|  | 76 | +    def readbytes(self, p): | 
|  | 77 | +        with open(p, 'rb') as f: | 
|  | 78 | +            return f.read() | 
|  | 79 | + | 
|  | 80 | + | 
|  | 81 | +class LocalPathInfo(pathlib.types.PathInfo): | 
|  | 82 | +    """ | 
|  | 83 | +    Simple implementation of PathInfo for a local path | 
|  | 84 | +    """ | 
|  | 85 | +    __slots__ = ('_path', '_exists', '_is_dir', '_is_file', '_is_symlink') | 
|  | 86 | + | 
|  | 87 | +    def __init__(self, path): | 
|  | 88 | +        self._path = str(path) | 
|  | 89 | +        self._exists = None | 
|  | 90 | +        self._is_dir = None | 
|  | 91 | +        self._is_file = None | 
|  | 92 | +        self._is_symlink = None | 
|  | 93 | + | 
|  | 94 | +    def exists(self, *, follow_symlinks=True): | 
|  | 95 | +        """Whether this path exists.""" | 
|  | 96 | +        if not follow_symlinks and self.is_symlink(): | 
|  | 97 | +            return True | 
|  | 98 | +        if self._exists is None: | 
|  | 99 | +            self._exists = os.path.exists(self._path) | 
|  | 100 | +        return self._exists | 
|  | 101 | + | 
|  | 102 | +    def is_dir(self, *, follow_symlinks=True): | 
|  | 103 | +        """Whether this path is a directory.""" | 
|  | 104 | +        if not follow_symlinks and self.is_symlink(): | 
|  | 105 | +            return False | 
|  | 106 | +        if self._is_dir is None: | 
|  | 107 | +            self._is_dir = os.path.isdir(self._path) | 
|  | 108 | +        return self._is_dir | 
|  | 109 | + | 
|  | 110 | +    def is_file(self, *, follow_symlinks=True): | 
|  | 111 | +        """Whether this path is a regular file.""" | 
|  | 112 | +        if not follow_symlinks and self.is_symlink(): | 
|  | 113 | +            return False | 
|  | 114 | +        if self._is_file is None: | 
|  | 115 | +            self._is_file = os.path.isfile(self._path) | 
|  | 116 | +        return self._is_file | 
|  | 117 | + | 
|  | 118 | +    def is_symlink(self): | 
|  | 119 | +        """Whether this path is a symbolic link.""" | 
|  | 120 | +        if self._is_symlink is None: | 
|  | 121 | +            self._is_symlink = os.path.islink(self._path) | 
|  | 122 | +        return self._is_symlink | 
|  | 123 | + | 
|  | 124 | + | 
|  | 125 | +class ReadableLocalPath(pathlib.types._ReadablePath, LexicalPath): | 
|  | 126 | +    """ | 
|  | 127 | +    Simple implementation of a ReadablePath class for local filesystem paths. | 
|  | 128 | +    """ | 
|  | 129 | +    __slots__ = ('info',) | 
|  | 130 | + | 
|  | 131 | +    def __init__(self, *pathsegments): | 
|  | 132 | +        super().__init__(*pathsegments) | 
|  | 133 | +        self.info = LocalPathInfo(self) | 
|  | 134 | + | 
|  | 135 | +    def __fspath__(self): | 
|  | 136 | +        return str(self) | 
|  | 137 | + | 
|  | 138 | +    def __open_rb__(self, buffering=-1): | 
|  | 139 | +        return open(self, 'rb') | 
|  | 140 | + | 
|  | 141 | +    def iterdir(self): | 
|  | 142 | +        return (self / name for name in os.listdir(self)) | 
|  | 143 | + | 
|  | 144 | +    def readlink(self): | 
|  | 145 | +        return self.with_segments(os.readlink(self)) | 
0 commit comments