MCPcopy Create free account
hub / github.com/apache/arrow / _ensure_filesystem

Function _ensure_filesystem

python/pyarrow/fs.py:85–127  ·  view source on GitHub ↗
(filesystem, *, use_mmap=False)

Source from the content-addressed store, hash-verified

83
84
85def _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
130def _resolve_filesystem_and_path(path, filesystem=None, *, memory_map=False):

Callers 6

_ensure_multiple_sourcesFunction · 0.90
_filesystem_datasetFunction · 0.90
parquet_datasetFunction · 0.90
__init__Method · 0.90
write_to_datasetFunction · 0.90

Calls 7

SubTreeFileSystemClass · 0.85
LocalFileSystemClass · 0.85
FSSpecHandlerClass · 0.85
typeEnum · 0.50
TypeErrorFunction · 0.50
normalize_pathMethod · 0.45
get_file_infoMethod · 0.45

Tested by

no test coverage detected