Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace sketchy opener interfaces with ABCs in fiona.abc #1415

Merged
merged 3 commits into from
Jul 29, 2024
Merged

Conversation

sgillies
Copy link
Member

@sgillies sgillies commented Jul 24, 2024

Also adds support for GDAL VSI multi-range reads. This is an attempt to take rasterio/rasterio#3117 all the way to a stable interface and API.

io.open(), fsspec filesystems, and to a lesser degree, tiledb.VFS, are privileged openers. There are built-in adapters for these objects.

Otherwise, users should implement fiona.abc.FileContainer and register their class as a virtual subclass within their application like FileContainer.register(NewClass).

fiona.abc.MultiByteRangeResourceContainer exists for completeness, to provide a template for Rasterio, and for compatibility with future GDAL/OGR drivers that may exploit it. Currently no vectors drivers do and there is no point in implementing this interface.

This also adds support for GDAL VSI multi-range reads.
@sgillies sgillies added this to the 1.10 milestone Jul 24, 2024
@sgillies sgillies self-assigned this Jul 24, 2024
Comment on lines +179 to +200
from fiona.abc import FileContainer

class CustomContainer:
"""GDAL's VSI ReadDir() uses 5 of FileContainer's methods."""
def isdir(self, path):
return pathlib.Path(path).is_dir()

def isfile(self, path):
return pathlib.Path(path).is_file()

def ls(self, path):
return list(pathlib.Path(path).iterdir())

def mtime(self, path):
return pathlib.Path(path).stat().st_mtime

def size(self, path):
return pathlib.Path(path).stat().st_size

FileContainer.register(CustomContainer)

listing = fiona.listdir("tests/data", opener=CustomContainer())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how a Fiona user creates a custom opener for their own application.

@sgillies
Copy link
Member Author

@vincentsarago @mwtoews @snorfalorpagus what do you think of a fiona.abc module?

Vincent, I think this is what we should do for Rasterio.

@@ -353,7 +376,7 @@ def _opener_registration(urlpath, obj):
cdef bytes prefix_bytes = f"/{namespace}/".encode("utf-8")

# Might raise.
opener = _create_opener(obj)
opener = to_pyopener(obj)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

def size(self, path):
return pathlib.Path(path).stat().st_size

FileContainer.register(CustomContainer)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@sgillies sgillies merged commit f225ff7 into main Jul 29, 2024
9 checks passed
@sgillies sgillies deleted the pyopener-abc branch July 29, 2024 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants