From 6f751e56697a7825373922d82cf68d7ec83d705a Mon Sep 17 00:00:00 2001 From: emielvdveen Date: Thu, 13 Jun 2024 13:34:54 +0200 Subject: [PATCH] Added: Support for assets available in Python --- CHANGELOG.md | 19 ++++++++++ README.md | 36 +++++++++++++++++++ package-lock.json | 4 +-- .../processing/py/port/api/assets.py | 14 ++++++++ .../processing/py/port/assets/hello_world.txt | 1 + src/framework/processing/py/port/script.py | 3 ++ src/framework/processing/py/pyproject.toml | 1 + 7 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 src/framework/processing/py/port/api/assets.py create mode 100644 src/framework/processing/py/port/assets/hello_world.txt diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..bfa84f21 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog + +## Types of Changes and How to Note Them + +Added - For any new features that have been added since the last version was released +Changed - To note any changes to the software's existing functionality +Deprecated - To note any features that were once stable but are no longer and have thus been removed +Fixed - Any bugs or errors that have been fixed should be so noted +Removed - This notes any features that have been deleted and removed from the software +Security - This acts as an invitation to users who want to upgrade and avoid any software vulnerabilities + +## 2 - unreleased + +* Added: Support for assets available in Python + + +## 1 - 2014-03-15 + +Initial version \ No newline at end of file diff --git a/README.md b/README.md index 59565cd0..2c9f6c06 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,42 @@ Example script: [`script.py`](src/framework/processing/py/port/script.py). We recommend to use the example script as starting point for your own data donation study. +### Port Assets + +Assets needed in the script can be copied to: `src/framework/processing/py/port/assets/` + +In your script you can access these assets as follows: + +#### Asset path + +```Python +from port.api.assets import * + +def process(sessionId): + path = asset_path("hello_world.txt") + file = open(path, "r") + txt = file.read() +``` + +#### Open asset + +```Python +from port.api.assets import * + +def process(sessionId): + file = open_asset("hello_world.txt") + txt = file.read() +``` + +#### Read asset + +```Python +from port.api.assets import * + +def process(sessionId): + txt = read_asset("hello_world.txt") +``` + ### Port API examples Below some examples on how to use the Port API in your `script.py` diff --git a/package-lock.json b/package-lock.json index 3bed95fa..c71fb7ac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "port", + "name": "feldspar", "version": "0.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "port", + "name": "feldspar", "version": "0.1.0", "dependencies": { "@testing-library/jest-dom": "^5.16.5", diff --git a/src/framework/processing/py/port/api/assets.py b/src/framework/processing/py/port/api/assets.py new file mode 100644 index 00000000..c7b7c060 --- /dev/null +++ b/src/framework/processing/py/port/api/assets.py @@ -0,0 +1,14 @@ +import os + +def asset_path(asset): + return os.path.join(os.path.join(os.path.dirname(__file__), "../assets"), asset) + + +def open_asset(asset): + path = asset_path(asset) + return open(path, "r") + + +def read_asset(asset): + file = open_asset(asset) + return file.read() \ No newline at end of file diff --git a/src/framework/processing/py/port/assets/hello_world.txt b/src/framework/processing/py/port/assets/hello_world.txt new file mode 100644 index 00000000..b45ef6fe --- /dev/null +++ b/src/framework/processing/py/port/assets/hello_world.txt @@ -0,0 +1 @@ +Hello, World! \ No newline at end of file diff --git a/src/framework/processing/py/port/script.py b/src/framework/processing/py/port/script.py index 7f696603..9ec3b24c 100644 --- a/src/framework/processing/py/port/script.py +++ b/src/framework/processing/py/port/script.py @@ -1,4 +1,5 @@ import port.api.props as props +from port.api.assets import * from port.api.commands import (CommandSystemDonate, CommandSystemExit, CommandUIRender) import pandas as pd @@ -7,6 +8,8 @@ def process(sessionId): + print(read_asset("hello_world.txt")) + key = "zip-contents-example" meta_data = [] meta_data.append(("debug", f"{key}: start")) diff --git a/src/framework/processing/py/pyproject.toml b/src/framework/processing/py/pyproject.toml index 6a48c421..271744d4 100644 --- a/src/framework/processing/py/pyproject.toml +++ b/src/framework/processing/py/pyproject.toml @@ -3,6 +3,7 @@ name = "port" version = "0.0.0" description = "Port package with Data Donation logic" authors = ["Emiel van der Veen "] +include = [{ path = "port/assets", format = ["sdist", "wheel"] }] [tool.poetry.dependencies] python = "^3.11"