|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +from typing import Any, Iterable, Sequence, Union |
| 4 | + |
| 5 | +import fsspec |
| 6 | +from fsspec import AbstractFileSystem |
| 7 | +from fsspec.core import split_protocol |
| 8 | +from fsspec.utils import stringify_path |
| 9 | + |
| 10 | + |
| 11 | +class PrefixFileSystem(AbstractFileSystem): |
| 12 | + def __init__( |
| 13 | + self, |
| 14 | + prefix: str, |
| 15 | + filesystem: fsspec.AbstractFileSystem, |
| 16 | + *args, |
| 17 | + **storage_options, |
| 18 | + ) -> None: |
| 19 | + super().__init__(*args, **storage_options) |
| 20 | + self.prefix = prefix |
| 21 | + self.filesystem = filesystem |
| 22 | + |
| 23 | + def _add_fs_prefix(self, path: Union[str, Path]) -> Union[str, Sequence[str]]: |
| 24 | + if isinstance(path, str): |
| 25 | + path = stringify_path(path) |
| 26 | + protocol, path = split_protocol(path) |
| 27 | + path = os.path.join(self.prefix, path) |
| 28 | + return protocol + "://" + path if protocol is not None else path |
| 29 | + elif isinstance(path, Iterable): |
| 30 | + return [self._add_fs_prefix(x) for x in path] |
| 31 | + assert False |
| 32 | + |
| 33 | + def _remove_fs_prefix(self, path: Union[str, Path]) -> Union[str, Sequence[str]]: |
| 34 | + if isinstance(path, str): |
| 35 | + path = stringify_path(path) |
| 36 | + protocol, path = split_protocol(path) |
| 37 | + path = os.path.relpath(path, start=self.prefix) |
| 38 | + return protocol + "://" + path if protocol is not None else path |
| 39 | + elif isinstance(path, Iterable): |
| 40 | + return [self._remove_fs_prefix(x) for x in path] |
| 41 | + assert False |
| 42 | + |
| 43 | + def mkdir(self, path: str, create_parents: bool = True, **kwargs) -> None: |
| 44 | + path = self._add_fs_prefix(path) |
| 45 | + return self.filesystem.mkdir(path=path, create_parents=create_parents, **kwargs) |
| 46 | + |
| 47 | + def makedirs(self, path: str, exist_ok: bool = False): |
| 48 | + path = self._add_fs_prefix(path) |
| 49 | + return self.filesystem.mkdirs(path=path, exist_ok=exist_ok) |
| 50 | + |
| 51 | + def rmdir(self, path: str): |
| 52 | + path = self._add_fs_prefix(path) |
| 53 | + return self.filesystem.rmdir(path=path) |
| 54 | + |
| 55 | + def ls( |
| 56 | + self, |
| 57 | + path: str, |
| 58 | + detail=False, |
| 59 | + **kwargs, |
| 60 | + ) -> Sequence[str]: |
| 61 | + path = self._add_fs_prefix(path) |
| 62 | + ls_out = self.filesystem.ls(path=path, detail=detail, **kwargs) |
| 63 | + if detail: |
| 64 | + for out in ls_out: |
| 65 | + out["name"] = self._remove_fs_prefix(out["name"]) |
| 66 | + return ls_out |
| 67 | + return self._remove_fs_prefix(ls_out) |
| 68 | + |
| 69 | + def glob(self, path: str, **kwargs): |
| 70 | + path = self._add_fs_prefix(path) |
| 71 | + glob_out = self.filesystem.glob(path=path, **kwargs) |
| 72 | + return [self._remove_fs_prefix(x) for x in glob_out] |
| 73 | + |
| 74 | + def info(self, path: str, **kwargs): |
| 75 | + path = self._add_fs_prefix(path) |
| 76 | + return self.filesystem.info(path=path, **kwargs) |
| 77 | + |
| 78 | + def cp_file(self, path1: str, path2: str, **kwargs): |
| 79 | + path1 = self._add_fs_prefix(path1) |
| 80 | + path2 = self._add_fs_prefix(path2) |
| 81 | + return self.filesystem.cp_file(path1, path2, **kwargs) |
| 82 | + |
| 83 | + def get_file(self, path1: str, path2: str, callback=None, **kwargs): |
| 84 | + path1 = self._add_fs_prefix(path1) |
| 85 | + path2 = self._add_fs_prefix(path2) |
| 86 | + return self.filesystem.get_file(path1, path2, callback, **kwargs) |
| 87 | + |
| 88 | + def put_file(self, path1: str, path2: str, callback=None, **kwargs): |
| 89 | + path1 = self._add_fs_prefix(path1) |
| 90 | + path2 = self._add_fs_prefix(path2) |
| 91 | + return self.filesystem.put_file(path1, path2, callback, **kwargs) |
| 92 | + |
| 93 | + def mv_file(self, path1: str, path2: str, **kwargs): |
| 94 | + path1 = self._add_fs_prefix(path1) |
| 95 | + path2 = self._add_fs_prefix(path2) |
| 96 | + return self.filesystem.mv_file(path1, path2, **kwargs) |
| 97 | + |
| 98 | + def rm_file(self, path: str): |
| 99 | + path = self._add_fs_prefix(path) |
| 100 | + return self.filesystem.rm_file(path) |
| 101 | + |
| 102 | + def rm(self, path: str, recursive=False, maxdepth=None): |
| 103 | + path = self._add_fs_prefix(path) |
| 104 | + return self.filesystem.rm(path, recursive=False, maxdepth=None) |
| 105 | + |
| 106 | + def touch(self, path: str, **kwargs): |
| 107 | + path = self._add_fs_prefix(path) |
| 108 | + return self.filesystem.touch(path, **kwargs) |
| 109 | + |
| 110 | + def created(self, path: str): |
| 111 | + path = self._add_fs_prefix(path) |
| 112 | + return self.filesystem.created(path) |
| 113 | + |
| 114 | + def modified(self, path: str): |
| 115 | + path = self._add_fs_prefix(path) |
| 116 | + return self.filesystem.modified(path) |
| 117 | + |
| 118 | + def sign(self, path: str, expiration=100, **kwargs): |
| 119 | + path = self._add_fs_prefix(path) |
| 120 | + return self.filesystem.sign(path, expiration=100, **kwargs) |
| 121 | + |
| 122 | + def cat( |
| 123 | + self, |
| 124 | + path: str, |
| 125 | + recursive: bool = False, |
| 126 | + on_error: str = "raise", |
| 127 | + **kwargs: Any, |
| 128 | + ): |
| 129 | + path = self._add_fs_prefix(path) |
| 130 | + return self.filesystem.cat( |
| 131 | + path, recursive=recursive, on_error=on_error, **kwargs |
| 132 | + ) |
| 133 | + |
| 134 | + def __repr__(self) -> str: |
| 135 | + return f"{self.__class__.__qualname__}(prefix='{self.prefix}', filesystem={self.filesystem})" |
0 commit comments