(filesystem, *, use_mmap=False)
| 83 | |
| 84 | |
| 85 | def _ensure_filesystem(filesystem, *, use_mmap=False): |
| 86 | if isinstance(filesystem, FileSystem): |
| 87 | return filesystem |
| 88 | elif isinstance(filesystem, str): |
| 89 | # create a filesystem from a URI string, note that the `path` part of the URI |
| 90 | # is treated as a prefix if specified, so the filesystem is wrapped in a |
| 91 | # SubTreeFileSystem |
| 92 | if use_mmap: |
| 93 | raise ValueError( |
| 94 | "Specifying to use memory mapping not supported for " |
| 95 | "filesystem specified as an URI string" |
| 96 | ) |
| 97 | fs, path = FileSystem.from_uri(filesystem) |
| 98 | prefix = fs.normalize_path(path) |
| 99 | if prefix: |
| 100 | # validate that the prefix is pointing to a directory |
| 101 | prefix_info = fs.get_file_info([prefix])[0] |
| 102 | if prefix_info.type != FileType.Directory: |
| 103 | raise ValueError( |
| 104 | "The path component of the filesystem URI must point to a " |
| 105 | f"directory but it has a type: `{prefix_info.type.name}`. The path " |
| 106 | f"component is `{prefix_info.path}` and the given filesystem URI " |
| 107 | f"is `{filesystem}`" |
| 108 | ) |
| 109 | fs = SubTreeFileSystem(prefix, fs) |
| 110 | return fs |
| 111 | else: |
| 112 | # handle fsspec-compatible filesystems |
| 113 | try: |
| 114 | import fsspec |
| 115 | except ImportError: |
| 116 | pass |
| 117 | else: |
| 118 | if isinstance(filesystem, fsspec.AbstractFileSystem): |
| 119 | if type(filesystem).__name__ == 'LocalFileSystem': |
| 120 | # In case its a simple LocalFileSystem, use native arrow one |
| 121 | return LocalFileSystem(use_mmap=use_mmap) |
| 122 | return PyFileSystem(FSSpecHandler(filesystem)) |
| 123 | |
| 124 | raise TypeError( |
| 125 | f"Unrecognized filesystem: {type(filesystem)}. `filesystem` argument must " |
| 126 | "be a FileSystem instance or a valid file system URI" |
| 127 | ) |
| 128 | |
| 129 | |
| 130 | def _resolve_filesystem_and_path(path, filesystem=None, *, memory_map=False): |
no test coverage detected