From 7b45d5d505b2972789eb92290d54a515b0b36eac Mon Sep 17 00:00:00 2001 From: fynnbe Date: Mon, 8 Jul 2024 13:37:32 +0200 Subject: [PATCH] add put_yaml() --- .../remote_collection.py | 9 ++------- bioimageio_collection_backoffice/s3_client.py | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/bioimageio_collection_backoffice/remote_collection.py b/bioimageio_collection_backoffice/remote_collection.py index 9041dfef..9054ba6b 100644 --- a/bioimageio_collection_backoffice/remote_collection.py +++ b/bioimageio_collection_backoffice/remote_collection.py @@ -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 diff --git a/bioimageio_collection_backoffice/s3_client.py b/bioimageio_collection_backoffice/s3_client.py index 0c444943..74e54be0 100644 --- a/bioimageio_collection_backoffice/s3_client.py +++ b/bioimageio_collection_backoffice/s3_client.py @@ -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) @@ -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)