Skip to content

Commit

Permalink
add put_yaml()
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnBe committed Jul 8, 2024
1 parent 1fabf5b commit 7b45d5d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
9 changes: 2 additions & 7 deletions bioimageio_collection_backoffice/remote_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,13 +1161,8 @@ def create_collection_entries(
test_summary = TestSummary(
status=bioimageio_status, tests=compat_tests
).model_dump(mode="json")
stream = io.StringIO()
yaml.dump(test_summary, stream)
test_summary_data = stream.getvalue().encode()
record_version.client.put(
f"{record_version.folder}test_summary.yaml",
io.BytesIO(test_summary_data),
length=len(test_summary_data),
record_version.client.put_yaml(
test_summary, f"{record_version.folder}test_summary.yaml"
)

# upload 'versions.json' summary
Expand Down
18 changes: 16 additions & 2 deletions bioimageio_collection_backoffice/s3_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
from minio.datatypes import Object
from minio.deleteobjects import DeleteObject
from pydantic import BaseModel, SecretStr
from ruyaml import YAML

from ._settings import settings
from .cache import SizedValueLRU

yaml = YAML(typ="safe")

M = TypeVar("M", bound=BaseModel)


Expand Down Expand Up @@ -104,16 +107,27 @@ def put(
logger.info("Uploaded {}", self.get_file_url(path))

def put_pydantic(self, path: str, obj: BaseModel):
"""convenience method to upload a json file from a pydantic model"""
"""upload a json file from a pydantic model"""
self.put_json_string(path, obj.model_dump_json(exclude_defaults=False))

def put_json(
self, path: str, json_value: Any # TODO: type json_value as JsonValue
):
"""convenience method to upload a json file from a json serializable value"""
"""upload a json file from a json serializable value"""
json_str = json.dumps(json_value)
self.put_json_string(path, json_str)

def put_yaml(self, yaml_value: Any, path: str):
"""upload a yaml file from a yaml serializable value"""
stream = io.StringIO()
yaml.dump(yaml_value, stream)
data = stream.getvalue().encode()
self.put(
path,
io.BytesIO(data),
length=len(data),
)

def put_json_string(self, path: str, json_str: str):
data = json_str.encode()
self.put_and_cache(path, data)
Expand Down

0 comments on commit 7b45d5d

Please sign in to comment.