def _ensure_filesystem(filesystem, use_mmap=False, allow_legacy_filesystem=False): if isinstance(filesystem, FileSystem): return filesystem # handle fsspec-compatible filesystems try: import fsspec except ImportError: pass else: if isinstance(filesystem, fsspec.AbstractFileSystem): if type(filesystem).__name__ == 'LocalFileSystem': # In case its a simple LocalFileSystem, use native arrow one return LocalFileSystem(use_mmap=use_mmap) return PyFileSystem(FSSpecHandler(filesystem)) # map old filesystems to new ones import pyarrow.filesystem as legacyfs if isinstance(filesystem, legacyfs.LocalFileSystem): return LocalFileSystem(use_mmap=use_mmap) # TODO handle HDFS? if allow_legacy_filesystem and isinstance(filesystem, legacyfs.FileSystem): return filesystem raise TypeError("Unrecognized filesystem: {}".format(type(filesystem)))
def _resolve_filesystem_and_path( path, filesystem=None, allow_legacy_filesystem=False ): """ Return filesystem/path from path which could be an URI or a plain filesystem path. """ if not _is_path_like(path): if filesystem is not None: raise ValueError( "'filesystem' passed but the specified path is file-like, so" " there is nothing to open with 'filesystem'." ) return filesystem, path if filesystem is not None: filesystem = _ensure_filesystem( filesystem, allow_legacy_filesystem=allow_legacy_filesystem ) if isinstance(filesystem, LocalFileSystem): path = _stringify_path(path) elif not isinstance(path, str): raise TypeError( "Expected string path; path-like objects are only allowed " "with a local filesystem" ) if not allow_legacy_filesystem: path = filesystem.normalize_path(path) return filesystem, path path = _stringify_path(path) # if filesystem is not given, try to automatically determine one # first check if the file exists as a local (relative) file path # if not then try to parse the path as an URI filesystem = LocalFileSystem() try: file_info = filesystem.get_file_info(path) except ValueError: # ValueError means path is likely an URI file_info = None exists_locally = False else: exists_locally = (file_info.type != FileType.NotFound) # if the file or directory doesn't exists locally, then assume that # the path is an URI describing the file system as well if not exists_locally: try: filesystem, path = FileSystem.from_uri(path) except ValueError as e: # neither an URI nor a locally existing path, so assume that # local path was given and propagate a nicer file not found error # instead of a more confusing scheme parsing error if "empty scheme" not in str(e): raise else: path = filesystem.normalize_path(path) return filesystem, path
def _ensure_filesystem( filesystem, use_mmap=False, allow_legacy_filesystem=False ): if isinstance(filesystem, FileSystem): return filesystem elif isinstance(filesystem, str): if use_mmap: raise ValueError( "Specifying to use memory mapping not supported for " "filesystem specified as an URI string" ) return _filesystem_from_str(filesystem) # handle fsspec-compatible filesystems try: import fsspec except ImportError: pass else: if isinstance(filesystem, fsspec.AbstractFileSystem): if type(filesystem).__name__ == 'LocalFileSystem': # In case its a simple LocalFileSystem, use native arrow one return LocalFileSystem(use_mmap=use_mmap) return PyFileSystem(FSSpecHandler(filesystem)) # map old filesystems to new ones import pyarrow.filesystem as legacyfs if isinstance(filesystem, legacyfs.LocalFileSystem): return LocalFileSystem(use_mmap=use_mmap) # TODO handle HDFS? if allow_legacy_filesystem and isinstance(filesystem, legacyfs.FileSystem): return filesystem raise TypeError( "Unrecognized filesystem: {}. `filesystem` argument must be a " "FileSystem instance or a valid file system URI'".format( type(filesystem)) )
def _ensure_filesystem(filesystem): if isinstance(filesystem, FileSystem): return filesystem # handle fsspec-compatible filesystems try: import fsspec except ImportError: pass else: if isinstance(filesystem, fsspec.AbstractFileSystem): if type(filesystem).__name__ == 'LocalFileSystem': # In case its a simple LocalFileSystem, use native arrow one return LocalFileSystem() return PyFileSystem(FSSpecHandler(filesystem)) # map old filesystems to new ones from pyarrow.filesystem import LocalFileSystem as LegacyLocalFileSystem if isinstance(filesystem, LegacyLocalFileSystem): return LocalFileSystem() # TODO handle HDFS? raise TypeError("Unrecognized filesystem: {}".format(type(filesystem)))