Skip to content

Commit

Permalink
[python] add support for access in load() and save()
Browse files Browse the repository at this point in the history
  • Loading branch information
javierluraschi committed Jan 20, 2025
1 parent ea6bde3 commit 09c851a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 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.8.4

- Add `access` parameter to `save()` to share files

## 2.8.3

- Fix encoding issue in `save()`
Expand Down
31 changes: 21 additions & 10 deletions python/hal9/iobind.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,24 @@ def get_hidden(file_path):
return hidden_path
return file_path

allowed_access_types = ["storage", "shared", "user"]

def ensure_storage():
if not os.path.exists('.storage'):
os.mkdir('.storage')
for access_type in allowed_access_types:
folder_name = f".{access_type}"
if not os.path.exists(folder_name):
os.mkdir(folder_name)

def validate_storage(access):
if access not in allowed_access_types:
raise ValueError(f"Invalid storage access: '{access}'")
return f".{access}"

def load(name, default):
def load(name, default, access = "storage"):
ensure_storage()
storage_path = validate_storage(access)

file_path = ".storage/" + name
file_path = f"{storage_path}/{name}"
file_path = find_extension(file_path)
file_path = get_hidden(file_path)

Expand All @@ -71,17 +81,18 @@ def load(name, default):

return contents

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

storage_path = validate_storage(access)

if not isinstance(name, str):
raise Exception(f"The name parameter in save() must be a string, got {str(type(name))}")

if hidden and not name.startswith('.'):
name = "." + name

if files is None:
target_path = './.storage'
target_path = f"./{storage_path}"
files = { name: contents }
else:
target_path = tempfile.mkdtemp()
Expand Down Expand Up @@ -117,15 +128,15 @@ def save(name, contents = None, hidden = False, files = None, encoding = None):
else:
raise Exception(f"Don't know how to save {extension} for {contents_type}")

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

def ready():
with open(".storage/.output", 'w') as file:
with open(f"{storage_path}/.output", 'w') as file:
file.write("")

def input(prompt = "", extract = False, messages = []):
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.8.3"
version = "2.8.4"
description = ""
authors = ["Javier Luraschi <[email protected]>"]
readme = "README.md"
Expand Down
10 changes: 6 additions & 4 deletions website/reference/io.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Wraps `input()` with an optional `extract` parameter to convert URLs to text

## Load

`load (name, default)` <br/><br/>
`load (name, default, access)` <br/><br/>

Reload the contents stored with `remember()` with `name`.

Expand All @@ -28,15 +28,17 @@ Returns the contents of `name` file, `default` if it does not exist.

## Save

`save (name, contents, hidden)` <br/><br/>
`save (name, contents, hidden, files, encoding, access)` <br/><br/>

Saves as `name` the given `contents`. Useful to share binary files with users.

| Param | Type | Description |
| --- | --- | --- |
| 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 additional file names to contents to save.
| hidden | <code>Boolean</code> | `True` to hide file from user, defaults to `False`. |
| files | <code>Dictionary</code> | A dictionary mapping additional file names to contents to save. |
| encoding | <code>String</code> | The encoding to use when saving text. |
| access | <code>String</code> | Modify access level to `user` or `shared`. Defaults to `storage` to save file for current session. The `shared` level allows any user using this chat to access this file. The `user` level allows other sessions for the same user to access the file. |

Saves to `name` file the given `contents` under the `.storage` subfolder. An appropriate extension for the `name` will be generated based on the type of `contents`.

0 comments on commit 09c851a

Please sign in to comment.