bolkedebruin commented on code in PR #34729: URL: https://github.com/apache/airflow/pull/34729#discussion_r1362619000
########## airflow/io/fsspec.py: ########## @@ -0,0 +1,237 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""FileIO implementation for reading and writing files that uses fsspec compatible filesystems.""" +from __future__ import annotations + +import errno +import logging +import os +from functools import lru_cache +from typing import ( + TYPE_CHECKING, + Callable, +) +from urllib.parse import urlparse + +from fsspec.implementations.local import LocalFileSystem + +from airflow.io import ( + FileIO, + InputFile, + InputStream, + OutputFile, + OutputStream, +) +from airflow.providers_manager import ProvidersManager +from airflow.stats import Stats +from airflow.utils.module_loading import import_string + +if TYPE_CHECKING: + from fsspec import AbstractFileSystem + +log = logging.getLogger(__name__) + + +def _file(_: str | None) -> LocalFileSystem: + return LocalFileSystem() + + +# always support local file systems +SCHEME_TO_FS: dict[str, Callable] = { + "file": _file, +} + + +def _register_schemes() -> None: + with Stats.timer("airflow.io.load_filesystems") as timer: + manager = ProvidersManager() + for fs_module_name in manager.filesystem_module_names: + fs_module = import_string(fs_module_name) + for scheme in getattr(fs_module, "schemes", []): + if scheme in SCHEME_TO_FS: + log.warning("Overriding scheme %s for %s", scheme, fs_module_name) + + method = getattr(fs_module, "get_fs", None) + if method is None: + raise ImportError(f"Filesystem {fs_module_name} does not have a get_fs method") + SCHEME_TO_FS[scheme] = method + + log.debug("loading filesystems from providers took %.3f seconds", timer.duration) + + +_register_schemes() + + +class FsspecInputFile(InputFile): + """An input file implementation for the FsspecFileIO. + + Args: + location (str): A URI to a file location. + fs (AbstractFileSystem): An fsspec filesystem instance. + """ + + def __init__(self, location: str, fs: AbstractFileSystem): + self._fs = fs + super().__init__(location=location) + + def __len__(self) -> int: + """Return the total length of the file, in bytes.""" + object_info = self._fs.info(self.location) + if size := object_info.get("Size"): + return size + elif size := object_info.get("size"): + return size + raise RuntimeError(f"Cannot retrieve object info: {self.location}") + + def exists(self) -> bool: + """Check whether the location exists.""" + return self._fs.lexists(self.location) + + def open(self, seekable: bool = True) -> InputStream: + """Create an input stream for reading the contents of the file. + + Args: + seekable: If the stream should support seek, or if it is consumed sequential. + + Returns: + OpenFile: An fsspec compliant file-like object. + + Raises: + FileNotFoundError: If the file does not exist. + """ + try: + return self._fs.open(self.location, "rb") + except FileNotFoundError as e: + # To have a consistent error handling experience, + # make sure exception contains missing file location. + raise e if e.filename else FileNotFoundError( + errno.ENOENT, os.strerror(errno.ENOENT), self.location + ) from e + + +class FsspecOutputFile(OutputFile): + """An output file implementation for the FsspecFileIO. + + Args: + location (str): A URI to a file location. + fs (AbstractFileSystem): An fsspec filesystem instance. + """ + + def __init__(self, location: str, fs: AbstractFileSystem): + self._fs = fs + super().__init__(location=location) + + def __len__(self) -> int: + """Return the total length of the file, in bytes.""" + object_info = self._fs.info(self.location) + if size := object_info.get("Size"): + return size + elif size := object_info.get("size"): + return size + raise RuntimeError(f"Cannot retrieve object info: {self.location}") + + def exists(self) -> bool: + """Check whether the location exists.""" + return self._fs.lexists(self.location) + + def create(self, overwrite: bool = False) -> OutputStream: + """Create an output stream for reading the contents of the file. + + Args: + overwrite (bool): Whether to overwrite the file if it already exists. + + Returns: + OpenFile: An fsspec compliant file-like object. + + Raises: + FileExistsError: If the file already exists at the location and overwrite is set to False. + + Note: + If overwrite is set to False, a check is first performed to verify that the file does not exist. + This is not thread-safe and a possibility does exist that the file can be created by a concurrent + process after the existence check yet before the output stream is created. In such a case, the + default behavior will truncate the contents of the existing file when opening the output stream. + """ + if not overwrite and self.exists(): + raise FileExistsError(f"Cannot create file, file already exists: {self.location}") + return self._fs.open(self.location, "wb") + + def to_input_file(self) -> FsspecInputFile: + """Return a new FsspecInputFile for the location at `self.location`.""" + return FsspecInputFile(location=self.location, fs=self._fs) + + +class FsspecFileIO(FileIO): Review Comment: The reason for this is that you might, in the future, have a different implementation not based on fsspec (e.g. smart_open) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
