Skip to content

Commit

Permalink
Allow io.BytesIO as webviz storage type (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans Kallekleiv authored Jan 16, 2020
1 parent 8727c4d commit 1ccfcb0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ The core of `webviz-config` will do the following:
functions will return the dataframe from the stored `.parquet` files,
instead of running the actual function code.

Currently only functions returning `pd.DataFrame` and `pathlib.Path` are
Currently only functions returning `pd.DataFrame`, `io.BytesIO` or `pathlib.Path` are
supported. A `NotImplementedError` will be raised if the function being
decorated with `@webvizstore` does not have a type hint being supported.

Expand Down
13 changes: 10 additions & 3 deletions webviz_config/webviz_store.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import io
import glob
import shutil
import functools
Expand All @@ -13,6 +14,9 @@


class WebvizStorage:

RETURN_TYPES = [pd.DataFrame, pathlib.Path, io.BytesIO]

def __init__(self):
self._use_storage = False
self.storage_functions = set()
Expand All @@ -25,10 +29,9 @@ def register_function(self, func):

return_type = inspect.getfullargspec(func).annotations["return"]

if return_type not in [pd.DataFrame, pathlib.Path]:
if return_type not in WebvizStorage.RETURN_TYPES:
raise NotImplementedError(
"Currently only storage of dataframes "
"and file resources are implemented."
f"Webviz storage type must be one of {WebvizStorage.RETURN_TYPES}"
)

self.storage_functions.add(func)
Expand Down Expand Up @@ -140,6 +143,8 @@ def get_stored_data(self, func, *args, **kwargs):
return pd.read_parquet(f"{path}.parquet")
if return_type == pathlib.Path:
return pathlib.Path(glob.glob(f"{path}*")[0])
if return_type == io.BytesIO:
return io.BytesIO(pathlib.Path(path).read_bytes())
raise ValueError(f"Unknown return type {return_type}")

except OSError:
Expand Down Expand Up @@ -175,6 +180,8 @@ def build_store(self):
output.to_parquet(f"{path}.parquet")
elif isinstance(output, pathlib.Path):
shutil.copy(output, f"{path}{output.suffix}")
elif isinstance(output, io.BytesIO):
pathlib.Path(path).write_bytes(output.getvalue())
else:
raise ValueError(f"Unknown return type {type(output)}")

Expand Down

0 comments on commit 1ccfcb0

Please sign in to comment.