jens-scheffler-bosch commented on code in PR #34729:
URL: https://github.com/apache/airflow/pull/34729#discussion_r1353202717


##########
airflow/io/fs/__init__.py:
##########
@@ -0,0 +1,987 @@
+# 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.
+from __future__ import annotations
+
+import functools
+import os.path
+from dataclasses import dataclass
+from os import PathLike
+from pathlib import PurePath, PurePosixPath
+from typing import TYPE_CHECKING, cast
+from urllib.parse import urlparse
+
+from fsspec.callbacks import NoOpCallback
+from fsspec.utils import tokenize
+
+from airflow.io.fsspec import SCHEME_TO_FS
+
+if TYPE_CHECKING:
+    from fsspec import AbstractFileSystem
+
+
+@dataclass
+class Mount(PathLike):
+    """Manages a mount point for a filesystem or object storage."""
+
+    source: str
+    mount_point: str
+
+    fs: AbstractFileSystem
+
+    conn_id: str | None = None
+
+    def wrap(self, method: str, *args, **kwargs):
+        """
+        Wrap a filesystem method to replace the mount point with the original 
source.
+
+        :param method: the method to wrap
+        :param args: the arguments to pass to the method
+        :param kwargs: the keyword arguments to pass to the method
+        :return: the result of the method
+        :rtype: Any
+        """
+        path = kwargs.pop("path") if "path" in kwargs else args[0]
+        path = self.replace_mount_point(cast(str, path))
+
+        return getattr(self.fs, method)(path, *args[1:], **kwargs)
+
+    def __str__(self):
+        return self.mount_point
+
+    def __fspath__(self):
+        return self.__str__()
+
+    def __enter__(self):
+        return self.fs
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        unmount(self.mount_point)
+
+    def __truediv__(self, other):
+        # if local we can run on nt or posix
+        if self.fsid == "local":
+            return PurePath(self.mount_point) / other
+
+        # if remote we assume posix
+        return PurePosixPath(self.mount_point) / other
+
+    def __getattr__(self, item):
+        return functools.partial(self.wrap, item)
+
+    def replace_mount_point(self, path: str) -> str:
+        new_path = path.replace(self.mount_point, self.source, 
1).replace("//", "/")
+
+        # check for traversal?
+        if self.source not in new_path:
+            new_path = os.path.join(self.source, new_path.lstrip(os.sep))
+
+        return new_path
+
+    @property
+    def fsid(self) -> str:
+        """
+        Get the filesystem ID for this mount in order to be able to compare 
across instances.
+
+        The underlying `fsid` is returned from the filesystem if available, 
otherwise it is generated
+        from the protocol and connection ID.
+
+        :return: deterministic the filesystem ID
+        """
+        try:
+            return self.fs.fsid
+        except NotImplementedError:
+            return f"{self.fs.protocol}-{self.conn_id or 'env'}"
+
+
+MOUNTS: dict[str, Mount] = {}

Review Comment:
   This is probably module private?
   ```suggestion
   _MOUNTS: dict[str, Mount] = {}
   ```



-- 
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]

Reply via email to