|
13 | 13 |
|
14 | 14 | from collections import deque
|
15 | 15 | from collections.abc import Iterator
|
16 |
| -from dataclasses import dataclass |
17 |
| -from enum import Enum |
18 | 16 | from pathlib import Path
|
19 | 17 | from typing import Optional
|
20 |
| -import weakref |
21 | 18 |
|
22 | 19 | from dandi import get_logger
|
23 |
| -from dandi.consts import ( |
24 |
| - BIDS_DATASET_DESCRIPTION, |
25 |
| - VIDEO_FILE_EXTENSIONS, |
26 |
| - ZARR_EXTENSIONS, |
27 |
| - dandiset_metadata_file, |
28 |
| -) |
| 20 | +from dandi.consts import BIDS_DATASET_DESCRIPTION, dandiset_metadata_file |
29 | 21 | from dandi.exceptions import UnknownAssetError
|
30 | 22 |
|
| 23 | +from ._private import BIDSFileFactory, DandiFileFactory |
31 | 24 | from .bases import (
|
32 | 25 | DandiFile,
|
33 | 26 | DandisetMetadataFile,
|
@@ -196,81 +189,6 @@ def dandi_file(
|
196 | 189 | return factory(filepath, path)
|
197 | 190 |
|
198 | 191 |
|
199 |
| -class DandiFileType(Enum): |
200 |
| - """:meta private:""" |
201 |
| - |
202 |
| - NWB = 1 |
203 |
| - ZARR = 2 |
204 |
| - VIDEO = 3 |
205 |
| - GENERIC = 4 |
206 |
| - BIDS_DATASET_DESCRIPTION = 5 |
207 |
| - |
208 |
| - @staticmethod |
209 |
| - def classify(path: Path) -> DandiFileType: |
210 |
| - if path.is_dir(): |
211 |
| - if not any(path.iterdir()): |
212 |
| - raise UnknownAssetError("Empty directories cannot be assets") |
213 |
| - if path.suffix in ZARR_EXTENSIONS: |
214 |
| - return DandiFileType.ZARR |
215 |
| - raise UnknownAssetError( |
216 |
| - f"Directory has unrecognized suffix {path.suffix!r}" |
217 |
| - ) |
218 |
| - elif path.name == BIDS_DATASET_DESCRIPTION: |
219 |
| - return DandiFileType.BIDS_DATASET_DESCRIPTION |
220 |
| - elif path.suffix == ".nwb": |
221 |
| - return DandiFileType.NWB |
222 |
| - elif path.suffix in VIDEO_FILE_EXTENSIONS: |
223 |
| - return DandiFileType.VIDEO |
224 |
| - else: |
225 |
| - return DandiFileType.GENERIC |
226 |
| - |
227 |
| - |
228 |
| -class DandiFileFactory: |
229 |
| - """:meta private:""" |
230 |
| - |
231 |
| - CLASSES: dict[DandiFileType, type[LocalAsset]] = { |
232 |
| - DandiFileType.NWB: NWBAsset, |
233 |
| - DandiFileType.ZARR: ZarrAsset, |
234 |
| - DandiFileType.VIDEO: VideoAsset, |
235 |
| - DandiFileType.GENERIC: GenericAsset, |
236 |
| - DandiFileType.BIDS_DATASET_DESCRIPTION: BIDSDatasetDescriptionAsset, |
237 |
| - } |
238 |
| - |
239 |
| - def __call__(self, filepath: Path, path: str) -> DandiFile: |
240 |
| - return self.CLASSES[DandiFileType.classify(filepath)]( |
241 |
| - filepath=filepath, path=path |
242 |
| - ) |
243 |
| - |
244 |
| - |
245 |
| -@dataclass |
246 |
| -class BIDSFileFactory(DandiFileFactory): |
247 |
| - """:meta private:""" |
248 |
| - |
249 |
| - bids_dataset_description: BIDSDatasetDescriptionAsset |
250 |
| - |
251 |
| - CLASSES = { |
252 |
| - DandiFileType.NWB: NWBBIDSAsset, |
253 |
| - DandiFileType.ZARR: ZarrBIDSAsset, |
254 |
| - DandiFileType.VIDEO: GenericBIDSAsset, |
255 |
| - DandiFileType.GENERIC: GenericBIDSAsset, |
256 |
| - } |
257 |
| - |
258 |
| - def __call__(self, filepath: Path, path: str) -> DandiFile: |
259 |
| - ftype = DandiFileType.classify(filepath) |
260 |
| - if ftype is DandiFileType.BIDS_DATASET_DESCRIPTION: |
261 |
| - if filepath == self.bids_dataset_description.filepath: |
262 |
| - return self.bids_dataset_description |
263 |
| - else: |
264 |
| - return BIDSDatasetDescriptionAsset(filepath=filepath, path=path) |
265 |
| - df = self.CLASSES[ftype]( |
266 |
| - filepath=filepath, |
267 |
| - path=path, |
268 |
| - bids_dataset_description_ref=weakref.ref(self.bids_dataset_description), |
269 |
| - ) |
270 |
| - self.bids_dataset_description.dataset_files.append(df) |
271 |
| - return df |
272 |
| - |
273 |
| - |
274 | 192 | def find_bids_dataset_description(
|
275 | 193 | dirpath: str | Path, dandiset_path: Optional[str | Path] = None
|
276 | 194 | ) -> Optional[BIDSDatasetDescriptionAsset]:
|
|
0 commit comments