Skip to content

Commit

Permalink
add conversion from Path to various binary types
Browse files Browse the repository at this point in the history
  • Loading branch information
neindochoh committed Feb 19, 2024
1 parent 290448d commit c8c0e15
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions renumics/spotlight/dtypes/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import inspect
import io
import os
import pathlib
from abc import ABCMeta
from collections import defaultdict
from typing import (
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit c8c0e15

Please sign in to comment.