You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/08/26 04:51:11 UTC

[GitHub] [iceberg] samredai commented on a diff in pull request #5332: [Python] FsspecFileIO, a FileIO that wraps any fsspec compliant filesystem

samredai commented on code in PR #5332:
URL: https://github.com/apache/iceberg/pull/5332#discussion_r955647135


##########
python/pyiceberg/io/fsspec.py:
##########
@@ -0,0 +1,186 @@
+# 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 table files that uses fsspec compatible filesystems"""
+
+from typing import Dict, Union
+from urllib.parse import urlparse
+
+from pyiceberg.io import FileIO, InputFile, OutputFile
+from pyiceberg.typedef import EMPTY_DICT
+
+
+class FsspecInputFile(InputFile):
+    """An input file implementation for the FsspecFileIO
+
+    Args:
+        location(str): A URI to a file location
+        fs: An fsspec filesystem instance
+    """
+
+    def __init__(self, location: str, fs):
+        self._fs = fs
+        super().__init__(location=location)
+
+    def __len__(self) -> int:
+        """Returns the total length of the file, in bytes"""
+        object_info = self._fs.info(self.location)
+        if size := object_info.get("Size"):
+            return size
+        elif object_info.get("size"):
+            return object_info["size"]
+        raise RuntimeError(f"Cannot retrieve object info: {self.location}")
+
+    def exists(self) -> bool:
+        """Checks whether the location exists"""
+        return self._fs.lexists(self.location)
+
+    def open(self):
+        """Create an input stream for reading the contents of the file
+
+        Returns:
+            OpenFile: An fsspec compliant file-like object
+        """
+        return self._fs.open(self.location, "rb")
+
+
+class FsspecOutputFile(OutputFile):
+    """An output file implementation for the FsspecFileIO
+
+    Args:
+        location(str): A URI to a file location
+        fs: An fsspec filesystem instance
+    """
+
+    def __init__(self, location: str, fs):
+        self._fs = fs
+        super().__init__(location=location)
+
+    def __len__(self) -> int:
+        """Returns the total length of the file, in bytes"""
+        object_info = self._fs.info(self.location)
+        if object_info.get("Size"):  # fsspec versions seem inconsistent on the case used for size

Review Comment:
   ```suggestion
           if object_info.get("Size"):
   ```



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org