|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | + |
| 13 | +import warnings |
| 14 | + |
| 15 | + |
| 16 | +from pyfakefs.fake_pathlib import FakePathlibModule |
| 17 | +from pyfakefs.fake_scandir import scandir, walk |
| 18 | + |
| 19 | + |
| 20 | +def legacy_warning(module_name): |
| 21 | + msg = ( |
| 22 | + f"You are using the legacy package '{module_name}' instead of the " |
| 23 | + f"built-in module." |
| 24 | + "Patching this package will no longer be supported in pyfakefs >= 6" |
| 25 | + ) |
| 26 | + warnings.warn(msg, category=DeprecationWarning) |
| 27 | + |
| 28 | + |
| 29 | +class FakePathlib2Module(FakePathlibModule): |
| 30 | + """Uses FakeFilesystem to provide a fake pathlib module replacement. |
| 31 | + for the `pathlib2` package available on PyPi. |
| 32 | + The usage of `pathlib2` is deprecated and will no longer be supported |
| 33 | + in future pyfakefs versions. |
| 34 | + """ |
| 35 | + |
| 36 | + has_warned = False |
| 37 | + |
| 38 | + def __getattribute__(self, name): |
| 39 | + attr = object.__getattribute__(self, name) |
| 40 | + if hasattr(attr, "__call__") and not FakePathlib2Module.has_warned: |
| 41 | + FakePathlib2Module.has_warned = True |
| 42 | + legacy_warning("pathlib2") |
| 43 | + return attr |
| 44 | + |
| 45 | + |
| 46 | +class FakeScanDirModule: |
| 47 | + """Uses FakeFilesystem to provide a fake module replacement |
| 48 | + for the `scandir` package available on PyPi. |
| 49 | +
|
| 50 | + The usage of the `scandir` package is deprecated and will no longer be supported |
| 51 | + in future pyfakefs versions. |
| 52 | +
|
| 53 | + You need a fake_filesystem to use this: |
| 54 | + `filesystem = fake_filesystem.FakeFilesystem()` |
| 55 | + `fake_scandir_module = fake_filesystem.FakeScanDirModule(filesystem)` |
| 56 | + """ |
| 57 | + |
| 58 | + @staticmethod |
| 59 | + def dir(): |
| 60 | + """Return the list of patched function names. Used for patching |
| 61 | + functions imported from the module. |
| 62 | + """ |
| 63 | + return "scandir", "walk" |
| 64 | + |
| 65 | + def __init__(self, filesystem): |
| 66 | + self.filesystem = filesystem |
| 67 | + |
| 68 | + has_warned = False |
| 69 | + |
| 70 | + def scandir(self, path="."): |
| 71 | + """Return an iterator of DirEntry objects corresponding to the entries |
| 72 | + in the directory given by path. |
| 73 | +
|
| 74 | + Args: |
| 75 | + path: Path to the target directory within the fake filesystem. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + an iterator to an unsorted list of os.DirEntry objects for |
| 79 | + each entry in path. |
| 80 | +
|
| 81 | + Raises: |
| 82 | + OSError: if the target is not a directory. |
| 83 | + """ |
| 84 | + if not self.has_warned: |
| 85 | + self.__class__.has_warned = True |
| 86 | + legacy_warning("scandir") |
| 87 | + return scandir(self.filesystem, path) |
| 88 | + |
| 89 | + def walk(self, top, topdown=True, onerror=None, followlinks=False): |
| 90 | + """Perform a walk operation over the fake filesystem. |
| 91 | +
|
| 92 | + Args: |
| 93 | + top: The root directory from which to begin walk. |
| 94 | + topdown: Determines whether to return the tuples with the root as |
| 95 | + the first entry (`True`) or as the last, after all the child |
| 96 | + directory tuples (`False`). |
| 97 | + onerror: If not `None`, function which will be called to handle the |
| 98 | + `os.error` instance provided when `os.listdir()` fails. |
| 99 | + followlinks: If `True`, symbolic links are followed. |
| 100 | +
|
| 101 | + Yields: |
| 102 | + (path, directories, nondirectories) for top and each of its |
| 103 | + subdirectories. See the documentation for the builtin os module |
| 104 | + for further details. |
| 105 | + """ |
| 106 | + if not self.has_warned: |
| 107 | + self.__class__.has_warned = True |
| 108 | + legacy_warning("scandir") |
| 109 | + |
| 110 | + return walk(self.filesystem, top, topdown, onerror, followlinks) |
0 commit comments