From 09c851a3ddce0a583147474f3a039df503e418c7 Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Mon, 20 Jan 2025 13:41:41 -0800 Subject: [PATCH] [python] add support for access in load() and save() --- CHANGELOG.md | 4 ++++ python/hal9/iobind.py | 31 +++++++++++++++++++++---------- python/pyproject.toml | 2 +- website/reference/io.md | 10 ++++++---- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c7bd217..aeddd5c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.8.4 + +- Add `access` parameter to `save()` to share files + ## 2.8.3 - Fix encoding issue in `save()` diff --git a/python/hal9/iobind.py b/python/hal9/iobind.py index 42c21948..e1e133f4 100644 --- a/python/hal9/iobind.py +++ b/python/hal9/iobind.py @@ -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) @@ -71,9 +81,10 @@ 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))}") @@ -81,7 +92,7 @@ def save(name, contents = None, hidden = False, files = None, encoding = None): name = "." + name if files is None: - target_path = './.storage' + target_path = f"./{storage_path}" files = { name: contents } else: target_path = tempfile.mkdtemp() @@ -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 = []): diff --git a/python/pyproject.toml b/python/pyproject.toml index 9e9aadbf..c239f50a 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "hal9" -version = "2.8.3" +version = "2.8.4" description = "" authors = ["Javier Luraschi "] readme = "README.md" diff --git a/website/reference/io.md b/website/reference/io.md index bcbcdcce..1fe8138e 100644 --- a/website/reference/io.md +++ b/website/reference/io.md @@ -15,7 +15,7 @@ Wraps `input()` with an optional `extract` parameter to convert URLs to text ## Load -`load (name, default)`

+`load (name, default, access)`

Reload the contents stored with `remember()` with `name`. @@ -28,7 +28,7 @@ Returns the contents of `name` file, `default` if it does not exist. ## Save -`save (name, contents, hidden)`

+`save (name, contents, hidden, files, encoding, access)`

Saves as `name` the given `contents`. Useful to share binary files with users. @@ -36,7 +36,9 @@ Saves as `name` the given `contents`. Useful to share binary files with users. | --- | --- | --- | | name | String | The file name of the file to save. | | contents | String | The contents of the file to save. | -| hidden | Boolean | `True` to hide file from user, defaults to `False`. -| files | Dictionary | A dictionary mapping additional file names to contents to save. +| hidden | Boolean | `True` to hide file from user, defaults to `False`. | +| files | Dictionary | A dictionary mapping additional file names to contents to save. | +| encoding | String | The encoding to use when saving text. | +| access | String | 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`.