|
| 1 | +"""fsspec-related utilities.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import fsspec |
| 7 | +import upath.registry |
| 8 | +from s3fs import S3FileSystem |
| 9 | +from upath._flavour import WrappedFileSystemFlavour |
| 10 | +from upath.implementations.cloud import CloudPath |
| 11 | + |
| 12 | + |
| 13 | +class WekaFileSystem(S3FileSystem): |
| 14 | + """fsspec FileSystem implementation for Weka. |
| 15 | +
|
| 16 | + This way we can still provide keys through environment variables, but use different |
| 17 | + environment variables for Weka. |
| 18 | + """ |
| 19 | + |
| 20 | + protocol = ("weka",) |
| 21 | + |
| 22 | + def __init__(self, **kwargs: dict[str, Any]): |
| 23 | + """Create a new WekaFileSystem. |
| 24 | +
|
| 25 | + Args: |
| 26 | + kwargs: see S3FileSystem. |
| 27 | + """ |
| 28 | + super().__init__( |
| 29 | + key=os.environ["WEKA_ACCESS_KEY_ID"], |
| 30 | + secret=os.environ["WEKA_SECRET_ACCESS_KEY"], |
| 31 | + endpoint_url=os.environ["WEKA_ENDPOINT_URL"], |
| 32 | + **kwargs, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +class WekaPath(CloudPath): |
| 37 | + """UPath implementation for Weka.""" |
| 38 | + |
| 39 | + __slots__ = () |
| 40 | + |
| 41 | + def __init__( |
| 42 | + self, *args: list[Any], protocol: str | None = None, **storage_options: Any |
| 43 | + ) -> None: |
| 44 | + """Create a new WekaPath. |
| 45 | +
|
| 46 | + Args: |
| 47 | + args: see CloudPath. |
| 48 | + protocol: the protocol name, should be "weka". |
| 49 | + storage_options: filesystem options. |
| 50 | + """ |
| 51 | + super().__init__(*args, protocol=protocol, **storage_options) |
| 52 | + if not self.drive and len(self.parts) > 1: |
| 53 | + raise ValueError("non key-like path provided (bucket/container missing)") |
| 54 | + |
| 55 | + |
| 56 | +fsspec.register_implementation("weka", WekaFileSystem) |
| 57 | +upath.registry.register_implementation("weka", WekaPath) |
| 58 | +WrappedFileSystemFlavour.protocol_config["netloc_is_anchor"].add("weka") |
| 59 | +WrappedFileSystemFlavour.protocol_config["supports_empty_parts"].add("weka") |
0 commit comments