Skip to content

Commit

Permalink
Fixed encoding in h9.save (#457)
Browse files Browse the repository at this point in the history
* Added encoding parameter to h9.save()

* Refactored if-else statements
  • Loading branch information
diegoarceof authored Dec 10, 2024
1 parent 97d4c45 commit 6dc680d
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions python/hal9/iobind.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def load(name, default):

return contents

def save(name, contents = None, hidden = False, files = None):
def save(name, contents = None, hidden = False, files = None, encoding = None):
ensure_storage()

if not isinstance(name, str):
Expand Down Expand Up @@ -100,24 +100,22 @@ def save(name, contents = None, hidden = False, files = None):
extension = get_extension(file_name)
asset_files.append(file_path)

if (extension == "json"):
if extension == "json":
contents = json.dumps(contents, indent=2)
file_path.write_text(contents)

if isinstance(contents, str):
file_path.write_text(contents)
file_path.write_text(contents, encoding=encoding)
elif extension == "pkl":
with open(file_path, 'wb') as file:
pickle.dump(contents, file)
elif extension == "jpg":
temp_path = Path(tempfile.mkdtemp()) / name
contents.save(temp_path, format="JPEG")
shutil.copy(temp_path, file_path)
elif isinstance(contents, str):
file_path.write_text(contents, encoding=encoding)
elif contents is None:
raise Exception(f"Can't save empty contents for {name}")
else:
if extension == "pkl":
with open(file_path, 'wb') as file:
pickle.dump(contents, file)
elif extension == "jpg":
temp_path = Path(tempfile.mkdtemp()) / name
contents.save(temp_path, format="JPEG")
shutil.copy(temp_path, file_path)
else:
raise Exception(f"Don't know how to save {extension} for {contents_type}")
raise Exception(f"Don't know how to save {extension} for {contents_type}")

if target_path != './.storage':
asset_definition = json.dumps({
Expand Down

0 comments on commit 6dc680d

Please sign in to comment.