Skip to content

Commit

Permalink
feat(config): allow fileformat to be set by fileformat subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
ljgray committed Mar 17, 2023
1 parent a3787b9 commit 4320fe3
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions caput/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
Richard 40.0 Sooty
"""
from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from . import fileformats

import logging

Expand Down Expand Up @@ -478,19 +483,19 @@ def _prop(config):
return prop


def file_format(default=None):
def file_format(default: str | fileformats.FileFormat | None = None) -> Property:
"""A property type that accepts only "zarr", or "hdf5".
Returns the selected `caput.fileformat.FileFormat` subclass or `caput.fileformats.HDF5` if `value == default`.
Parameters
----------
default : optional
The optional default value.
default
A string or type object specifying the fileformat
Returns
-------
prop : Property
prop
A property instance setup to validate a file format.
Raises
Expand All @@ -514,16 +519,21 @@ def _prop(val):
if val is None:
return None

if issubclass(val, fileformats.FileFormat):
return val

if not isinstance(val, str):
CaputConfigError(
raise CaputConfigError(
f"Input {repr(val)} is of type {type(val).__name__} (expected str or None)."
)

val = val.lower()

if val == "hdf5":
return fileformats.HDF5
if val == "zarr":
return fileformats.Zarr

raise CaputConfigError(f"Input {repr(val)} needs to be one of {options})")

if default is not None and (
Expand Down

0 comments on commit 4320fe3

Please sign in to comment.