uranusjr commented on code in PR #34729: URL: https://github.com/apache/airflow/pull/34729#discussion_r1363114610
########## 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() Review Comment: I don’t think the provider can be initialised this eagerly (not sure). It may be better to make this something like ```python def _get_filesystem(scheme: str) -> AbstractFileSystem: if scheme == "file": return LocalFileSystem return ProvidersManager().get_filesystem(scheme) ``` and move most logic in the provider manager. ########## 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() Review Comment: I don’t think the provider can be initialised this eagerly (not sure). It may be better to make this something like ```python def _get_filesystem(scheme: str) -> AbstractFileSystem: if scheme == "file": return LocalFileSystem return ProvidersManager().get_filesystem(scheme) ``` and move most logic in the provider manager. -- 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: commits-unsubscr...@airflow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org