|
| 1 | +import os |
| 2 | +from typing import Union |
| 3 | + |
| 4 | +from fsspec import AbstractFileSystem |
| 5 | + |
| 6 | +from galaxy.exceptions import MessageException |
| 7 | +from galaxy.files.models import ( |
| 8 | + AnyRemoteEntry, |
| 9 | + FilesSourceRuntimeContext, |
| 10 | + RemoteDirectory, |
| 11 | + RemoteFile, |
| 12 | +) |
| 13 | +from galaxy.files.sources._defaults import DEFAULT_SCHEME |
| 14 | +from galaxy.files.sources._fsspec import ( |
| 15 | + CacheOptionsDictType, |
| 16 | + FsspecBaseFileSourceConfiguration, |
| 17 | + FsspecBaseFileSourceTemplateConfiguration, |
| 18 | + FsspecFilesSource, |
| 19 | +) |
| 20 | +from galaxy.util.config_templates import TemplateExpansion |
| 21 | + |
| 22 | +try: |
| 23 | + from iiif_fsspec import IIIFFileSystem |
| 24 | +except ImportError: |
| 25 | + IIIFFileSystem = None |
| 26 | + |
| 27 | + |
| 28 | +class IIIFFileSourceTemplateConfiguration(FsspecBaseFileSourceTemplateConfiguration): |
| 29 | + manifest_url: Union[str, TemplateExpansion] |
| 30 | + |
| 31 | + |
| 32 | +class IIIFFileSourceConfiguration(FsspecBaseFileSourceConfiguration): |
| 33 | + manifest_url: str |
| 34 | + |
| 35 | + |
| 36 | +class IIIFFilesSource(FsspecFilesSource[IIIFFileSourceTemplateConfiguration, IIIFFileSourceConfiguration]): |
| 37 | + plugin_type = "iiif" |
| 38 | + required_module = IIIFFileSystem |
| 39 | + required_package = "iiif-fsspec" |
| 40 | + |
| 41 | + template_config_class = IIIFFileSourceTemplateConfiguration |
| 42 | + resolved_config_class = IIIFFileSourceConfiguration |
| 43 | + |
| 44 | + def _open_fs( |
| 45 | + self, |
| 46 | + _context: FilesSourceRuntimeContext[IIIFFileSourceConfiguration], |
| 47 | + cache_options: CacheOptionsDictType, |
| 48 | + ) -> AbstractFileSystem: |
| 49 | + if IIIFFileSystem is None: |
| 50 | + raise self.required_package_exception |
| 51 | + |
| 52 | + return IIIFFileSystem(**cache_options) |
| 53 | + |
| 54 | + def _to_filesystem_path(self, path: str, config: IIIFFileSourceConfiguration) -> str: |
| 55 | + if path in ("", "/"): |
| 56 | + return self._normalize_manifest_url(config.manifest_url) |
| 57 | + return path.lstrip("/") |
| 58 | + |
| 59 | + def _info_to_entry(self, info: dict, config: IIIFFileSourceConfiguration) -> AnyRemoteEntry: |
| 60 | + filesystem_path = info["name"] |
| 61 | + entry_path = filesystem_path |
| 62 | + entry_name = self._entry_name_from_info(info, filesystem_path) |
| 63 | + uri = self.uri_from_path(entry_path) |
| 64 | + |
| 65 | + if info.get("type") == "directory": |
| 66 | + return RemoteDirectory(name=entry_name, uri=uri, path=entry_path) |
| 67 | + |
| 68 | + size = int(info.get("size", 0)) |
| 69 | + ctime = self._get_formatted_timestamp(info) |
| 70 | + hashes = self._get_file_hashes(info) |
| 71 | + return RemoteFile(name=entry_name, size=size, ctime=ctime, uri=uri, path=entry_path, hashes=hashes) |
| 72 | + |
| 73 | + def _entry_name_from_info(self, info: dict, filesystem_path: str) -> str: |
| 74 | + iiif_label = info.get("iiif_label") |
| 75 | + if isinstance(iiif_label, str) and iiif_label: |
| 76 | + return iiif_label |
| 77 | + return os.path.basename(filesystem_path.rstrip("/")) |
| 78 | + |
| 79 | + @staticmethod |
| 80 | + def _normalize_manifest_url(manifest_url: str) -> str: |
| 81 | + return manifest_url.rstrip("/") |
| 82 | + |
| 83 | + def _write_from( |
| 84 | + self, |
| 85 | + _target_path: str, |
| 86 | + _native_path: str, |
| 87 | + _context: FilesSourceRuntimeContext[IIIFFileSourceConfiguration], |
| 88 | + ): |
| 89 | + raise MessageException("IIIF file sources are read-only and do not support exporting files.") |
| 90 | + |
| 91 | + def get_scheme(self) -> str: |
| 92 | + return self.scheme if self.scheme and self.scheme != DEFAULT_SCHEME else "iiif" |
| 93 | + |
| 94 | + |
| 95 | +__all__ = ("IIIFFilesSource",) |
0 commit comments