Skip to content

Commit

Permalink
add support for multiple files in save
Browse files Browse the repository at this point in the history
  • Loading branch information
javierluraschi committed Jul 25, 2024
1 parent 8b7c786 commit e124c82
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.5.1

- Add support for `files` in `save()` to persist multiple files

## 2.4.3

- Add `--data` to CLI
Expand Down
24 changes: 14 additions & 10 deletions python/hal9/iobind.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
import pickle
import tempfile

def get_extension(file_path):
_, extension = os.path.splitext(file_path)
return extension.lstrip('.')

def add_extension(name, contents):
extension = get_extension(name)
if not extension:
if isinstance(contents, dict) or isinstance(contents, list) or isinstance(contents, str):
path = path + ".json"
name = name + ".json"
else:
path = path + ".pkl"
return path
name = name + ".pkl"
return name

def find_extension(file_path):
if Path(file_path + '.json').exists() or get_hidden(Path(file_path + '.json')).exists():
Expand All @@ -27,10 +32,6 @@ def get_hidden(file_path):
return hidden_path
return file_path

def get_extension(file_path):
_, extension = os.path.splitext(file_path)
return extension.lstrip('.')

def load(name, default):
file_path = find_extension(name)
file_path = get_hidden(file_path)
Expand Down Expand Up @@ -63,11 +64,14 @@ def save(name, contents = None, hidden = False, files = None):
files = { name: contents }
else:
target_path = tempfile.mkdtemp()
files[name] = contents

asset_files = []
for file_name, contents in files.items():
file_name = add_extension(file_name, contents)
file_path = Path(target_path) / file_name
extension = get_extension(file_name)
asset_files.append(file_path)

if (extension == "json"):
contents = json.dumps(contents, indent=2)
Expand All @@ -84,10 +88,10 @@ def save(name, contents = None, hidden = False, files = None):

if target_path != '.':
asset_definition = json.dumps({
"name": name
"files": files.keys()
"name": name,
"files": [str(file) for file in asset_files]
}, indent=2)
Path(name + '.asset').write_text(contents)
Path(name + '.asset').write_text(asset_definition)

original_input = input
def input(prompt = "", extract = True):
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hal9"
version = "2.5.0"
version = "2.5.1"
description = ""
authors = ["Javier Luraschi <javier@hal9.ai>"]
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion website/reference/io.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ An appropriate extension for the `name` will be generated based on the type of `
| name | <code>String</code> | The file name of the file to save. |
| contents | <code>String</code> | The contents of the file to save. |
| hidden | <code>Boolean</code> | `True` to hide file from user, defaults to `False`.
| files | <code>Dictionary</code> | A dictionary mapping multiple file names to contents to save.
| files | <code>Dictionary</code> | A dictionary mapping additional file names to contents to save.

0 comments on commit e124c82

Please sign in to comment.