From 2e2e95a1298b28d6910a4a75194290afd87660d9 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Mon, 1 May 2023 19:39:21 -0400 Subject: [PATCH 1/3] ENH: Parallel specification of next API --- bids_ng/__init__.py | 0 bids_ng/types/__init__.py | 18 ++++++ bids_ng/types/api1.py | 113 ++++++++++++++++++++++++++++++++++++++ bids_ng/types/enums.py | 17 ++++++ bids_ng/types/utils.py | 76 +++++++++++++++++++++++++ 5 files changed, 224 insertions(+) create mode 100644 bids_ng/__init__.py create mode 100644 bids_ng/types/__init__.py create mode 100644 bids_ng/types/api1.py create mode 100644 bids_ng/types/enums.py create mode 100644 bids_ng/types/utils.py diff --git a/bids_ng/__init__.py b/bids_ng/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/bids_ng/types/__init__.py b/bids_ng/types/__init__.py new file mode 100644 index 000000000..09c59a321 --- /dev/null +++ b/bids_ng/types/__init__.py @@ -0,0 +1,18 @@ +from .api1 import BIDSDataset, BIDSFile, File, Index, Label +from .enums import Query +from .utils import PaddedInt + +NONE, REQUIRED, OPTIONAL = tuple(Query) + +__all__ = ( + "BIDSDataset", + "BIDSFile", + "File", + "Index", + "Label", + "NONE", + "OPTIONAL", + "REQUIRED", + "Query", + "PaddedInt", +) diff --git a/bids_ng/types/api1.py b/bids_ng/types/api1.py new file mode 100644 index 000000000..357c0e733 --- /dev/null +++ b/bids_ng/types/api1.py @@ -0,0 +1,113 @@ +"""PyBIDS 1.0 API specification""" + +from pathlib import Path +from typing import Any, Dict, List, Optional, Protocol, TypeVar, Union + +from .utils import PaddedInt + +try: + from typing import TypeAlias +except ImportError: + from typing_extensions import TypeAlias + + +# Datasets should be parameterizable on some kind of schema object. +# External API users should not depend on it, so this is bound to Any, +# but once a Schema type is defined for an API implementation, type checkers +# should be able to introspect it. +SchemaT = TypeVar("SchemaT") + + +Index: TypeAlias = PaddedInt +Label: TypeAlias = str + + +class File(Protocol[SchemaT]): + """Generic file holder + + This serves as a base class for :class:`BIDSFile` and can represent + non-BIDS files. + """ + + path: Path + relative_path: Path + dataset: Optional["BIDSDataset[SchemaT]"] + + def __fspath__(self) -> str: + ... + + +class BIDSFile(File[SchemaT], Protocol): + """BIDS file + + This provides access to BIDS concepts such as path components + and sidecar metadata. + + BIDS paths take the form:: + + [sub-