Exemplo n.º 1
0
class _FakeAccessor(accessor):
    """Accessor which forwards some of the functions to FakeFilesystem methods.
    """

    stat = _wrap_strfunc(FakeFilesystem.stat)

    lstat = _wrap_strfunc(
        lambda fs, path: FakeFilesystem.stat(fs, path, follow_symlinks=False))

    listdir = _wrap_strfunc(FakeFilesystem.listdir)

    chmod = _wrap_strfunc(FakeFilesystem.chmod)

    if use_scandir:
        scandir = _wrap_strfunc(fake_scandir.scandir)

    if hasattr(os, "lchmod"):
        lchmod = _wrap_strfunc(lambda fs, path, mode: FakeFilesystem.chmod(
            fs, path, mode, follow_symlinks=False))
    else:

        def lchmod(self, pathobj, mode):
            """Raises not implemented for Windows systems."""
            raise NotImplementedError("lchmod() not available on this system")

    mkdir = _wrap_strfunc(FakeFilesystem.makedir)

    unlink = _wrap_strfunc(FakeFilesystem.remove)

    rmdir = _wrap_strfunc(FakeFilesystem.rmdir)

    rename = _wrap_binary_strfunc(FakeFilesystem.rename)

    replace = _wrap_binary_strfunc(
        lambda fs, old_path, new_path: FakeFilesystem.rename(
            fs, old_path, new_path, force_replace=True))

    symlink = _wrap_binary_strfunc_reverse(
        lambda fs, file_path, link_target, target_is_directory: FakeFilesystem.
        create_symlink(fs, file_path, link_target, create_missing_dirs=False))

    if sys.version_info >= (3, 8):
        link_to = _wrap_binary_strfunc(
            lambda fs, file_path, link_target: FakeFilesystem.link(
                fs, file_path, link_target))

    if sys.version_info >= (3, 9):
        readlink = _wrap_strfunc(FakeFilesystem.readlink)

    utime = _wrap_strfunc(FakeFilesystem.utime)
Exemplo n.º 2
0
class _FakeAccessor(accessor):  # type: ignore [valid-type, misc]
    """Accessor which forwards some of the functions to FakeFilesystem methods.
    """

    stat = _wrap_strfunc(FakeFilesystem.stat)

    lstat = _wrap_strfunc(
        lambda fs, path: FakeFilesystem.stat(fs, path, follow_symlinks=False))

    listdir = _wrap_strfunc(FakeFilesystem.listdir)

    if use_scandir:
        scandir = _wrap_strfunc(fake_scandir.scandir)

    chmod = _wrap_strfunc(FakeFilesystem.chmod)

    if hasattr(os, "lchmod"):
        lchmod = _wrap_strfunc(lambda fs, path, mode: FakeFilesystem.chmod(
            fs, path, mode, follow_symlinks=False))
    else:

        def lchmod(self, pathobj, *args, **kwargs):
            """Raises not implemented for Windows systems."""
            raise NotImplementedError("lchmod() not available on this system")

        def chmod(self, pathobj, *args, **kwargs):
            if "follow_symlinks" in kwargs:
                if sys.version_info < (3, 10):
                    raise TypeError("chmod() got an unexpected keyword "
                                    "argument 'follow_synlinks'")
                if (not kwargs["follow_symlinks"]
                        and os.chmod not in os.supports_follow_symlinks):
                    raise NotImplementedError(
                        "`follow_symlinks` for chmod() is not available "
                        "on this system")
            return pathobj.filesystem.chmod(str(pathobj), *args, **kwargs)

    mkdir = _wrap_strfunc(FakeFilesystem.makedir)

    unlink = _wrap_strfunc(FakeFilesystem.remove)

    rmdir = _wrap_strfunc(FakeFilesystem.rmdir)

    rename = _wrap_binary_strfunc(FakeFilesystem.rename)

    replace = _wrap_binary_strfunc(
        lambda fs, old_path, new_path: FakeFilesystem.rename(
            fs, old_path, new_path, force_replace=True))

    symlink = _wrap_binary_strfunc_reverse(
        lambda fs, file_path, link_target, target_is_directory: FakeFilesystem.
        create_symlink(fs, file_path, link_target, create_missing_dirs=False))

    if (3, 8) <= sys.version_info:
        link_to = _wrap_binary_strfunc(
            lambda fs, file_path, link_target: FakeFilesystem.link(
                fs, file_path, link_target))

    if sys.version_info >= (3, 10):
        link = _wrap_binary_strfunc(
            lambda fs, file_path, link_target: FakeFilesystem.link(
                fs, file_path, link_target))

        # this will use the fake filesystem because os is patched
        def getcwd(self):
            return os.getcwd()

    readlink = _wrap_strfunc(FakeFilesystem.readlink)

    utime = _wrap_strfunc(FakeFilesystem.utime)