From 109b894c2cedc827004639fcb0189c921f2d6e9e Mon Sep 17 00:00:00 2001 From: JGStew Date: Wed, 11 Oct 2023 15:02:56 -0400 Subject: [PATCH] add upload files example, improve upload function --- examples/upload_files.py | 32 ++++++++++++++++++++++++++++++++ examples/upload_files/README.md | 1 + src/besapi/besapi.py | 7 +++++++ 3 files changed, 40 insertions(+) create mode 100644 examples/upload_files.py create mode 100644 examples/upload_files/README.md diff --git a/examples/upload_files.py b/examples/upload_files.py new file mode 100644 index 0000000..428d6de --- /dev/null +++ b/examples/upload_files.py @@ -0,0 +1,32 @@ +""" +Upload files in folder. + +requires `besapi`, install with command `pip install besapi` +""" + +import os + +import besapi + + +def main(path_folder="./tmp"): + """Execution starts here""" + print("main()") + bes_conn = besapi.besapi.get_bes_conn_using_config_file() + bes_conn.login() + + print(f"INFO: Uploading new files within: {os.path.abspath(path_folder)}") + + for entry in os.scandir(path_folder): + if entry.is_file() and "README.md" not in entry.path: + if " " in os.path.basename(entry.path): + print(f"ERROR: files cannot contain spaces! skipping: {entry.path}") + continue + print(f"Processing: {entry.path}") + output = bes_conn.upload(entry.path) + # print(output) + print(bes_conn.parse_upload_result_to_prefetch(output)) + + +if __name__ == "__main__": + main("./examples/upload_files") diff --git a/examples/upload_files/README.md b/examples/upload_files/README.md new file mode 100644 index 0000000..5ace3f2 --- /dev/null +++ b/examples/upload_files/README.md @@ -0,0 +1 @@ +put files in this folder to have them be uploaded to the root server by the script upload_files.py diff --git a/src/besapi/besapi.py b/src/besapi/besapi.py index e86feb9..4622a4c 100644 --- a/src/besapi/besapi.py +++ b/src/besapi/besapi.py @@ -591,6 +591,13 @@ def upload(self, file_path, file_name=None, file_hash=None): if not file_name: file_name = os.path.basename(file_path) + # files cannot contain spaces: + if " " in file_name: + besapi_logger.warning( + "Replacing spaces with underscores in `%s`", file_name + ) + file_name = file_name.replace(" ", "_") + if not file_hash: besapi_logger.warning( "SHA1 hash of file to be uploaded not provided, calculating it."