|  | 
|  | 1 | +from pathlib import Path | 
|  | 2 | +from shutil import copyfile | 
|  | 3 | +from typing import Any, TypeVar | 
|  | 4 | + | 
|  | 5 | +from typing_extensions import TypeIs | 
|  | 6 | + | 
|  | 7 | + | 
|  | 8 | +class Unserializable: ... | 
|  | 9 | + | 
|  | 10 | + | 
|  | 11 | +T = TypeVar("T") | 
|  | 12 | + | 
|  | 13 | + | 
|  | 14 | +def is_unserializable(x: Any) -> TypeIs[Unserializable]: | 
|  | 15 | +    return isinstance(x, Unserializable) | 
|  | 16 | + | 
|  | 17 | + | 
|  | 18 | +async def serializer_unserializable( | 
|  | 19 | +    value: Any = None, state_dir: Path | None = None | 
|  | 20 | +) -> Unserializable: | 
|  | 21 | +    return Unserializable() | 
|  | 22 | + | 
|  | 23 | + | 
|  | 24 | +async def serializer_default(value: T, state_dir: Path | None) -> T: | 
|  | 25 | +    return value | 
|  | 26 | + | 
|  | 27 | + | 
|  | 28 | +# TODO-barret; Integrate | 
|  | 29 | +def serializer_file_input( | 
|  | 30 | +    value: Any, | 
|  | 31 | +    state_dir: Path | None, | 
|  | 32 | +) -> Any | Unserializable: | 
|  | 33 | +    if state_dir is None: | 
|  | 34 | +        return Unserializable() | 
|  | 35 | + | 
|  | 36 | +    # TODO: barret; Double check this logic! | 
|  | 37 | + | 
|  | 38 | +    # `value` is a data frame. When persisting files, we need to copy the file to | 
|  | 39 | +    # the persistent dir and then strip the original path before saving. | 
|  | 40 | +    datapath = Path(value["datapath"]) | 
|  | 41 | +    new_paths = state_dir / datapath.name | 
|  | 42 | + | 
|  | 43 | +    if new_paths.exists(): | 
|  | 44 | +        new_paths.unlink() | 
|  | 45 | +    copyfile(datapath, new_paths) | 
|  | 46 | + | 
|  | 47 | +    value["datapath"] = new_paths.name | 
|  | 48 | + | 
|  | 49 | +    return value | 
0 commit comments