From c8c0e1543466f85fea5ee0c0d25504f8fdd2465f Mon Sep 17 00:00:00 2001 From: Dominik Haentsch Date: Mon, 19 Feb 2024 10:05:04 +0100 Subject: [PATCH] add conversion from Path to various binary types --- renumics/spotlight/dtypes/conversion.py | 45 +++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/renumics/spotlight/dtypes/conversion.py b/renumics/spotlight/dtypes/conversion.py index 45315b28..94d90390 100644 --- a/renumics/spotlight/dtypes/conversion.py +++ b/renumics/spotlight/dtypes/conversion.py @@ -18,6 +18,7 @@ import inspect import io import os +import pathlib from abc import ABCMeta from collections import defaultdict from typing import ( @@ -384,6 +385,17 @@ def _(value: Union[str, np.str_], _: dtypes.DType) -> bytes: raise ConversionError() +@convert("Image", simple=False) +def _(value: Union[pathlib.PosixPath, pathlib.WindowsPath], _: dtypes.DType) -> bytes: + try: + if data := read_external_value(str(value), dtypes.image_dtype): + return data.tolist() + else: + raise ConversionError() + except InvalidFile: + raise ConversionError() + + @convert("Image", simple=False) def _(value: Union[bytes, np.bytes_], _: dtypes.DType) -> bytes: return media.Image.from_bytes(value).encode().tolist() @@ -406,6 +418,17 @@ def _(value: Union[str, np.str_], _: dtypes.DType) -> bytes: raise ConversionError() +@convert("Audio", simple=False) +def _(value: Union[pathlib.PosixPath, pathlib.WindowsPath], _: dtypes.DType) -> bytes: + try: + if data := read_external_value(str(value), dtypes.audio_dtype): + return data.tolist() + else: + raise ConversionError() + except InvalidFile: + raise ConversionError() + + @convert("Audio", simple=False) def _(value: Union[bytes, np.bytes_], _: dtypes.DType) -> bytes: return media.Audio.from_bytes(value).encode().tolist() @@ -421,6 +444,17 @@ def _(value: Union[str, np.str_], _: dtypes.DType) -> bytes: raise ConversionError() +@convert("Video", simple=False) +def _(value: Union[pathlib.PosixPath, pathlib.WindowsPath], _: dtypes.DType) -> bytes: + try: + if data := read_external_value(str(value), dtypes.video_dtype): + return data.tolist() + else: + raise ConversionError() + except InvalidFile: + raise ConversionError() + + @convert("Video", simple=False) def _(value: Union[bytes, np.bytes_], _: dtypes.DType) -> bytes: return media.Video.from_bytes(value).encode().tolist() @@ -436,6 +470,17 @@ def _(value: Union[str, np.str_], _: dtypes.DType) -> bytes: raise ConversionError() +@convert("Mesh", simple=False) +def _(value: Union[pathlib.PosixPath, pathlib.WindowsPath], _: dtypes.DType) -> bytes: + try: + if data := read_external_value(str(value), dtypes.mesh_dtype): + return data.tolist() + else: + raise ConversionError() + except InvalidFile: + raise ConversionError() + + @convert("Mesh", simple=False) def _(value: Union[bytes, np.bytes_], _: dtypes.DType) -> bytes: return value