From b9e4911f10ccc3a1144e3eb274d873cfa22c1d6d Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Thu, 24 Apr 2025 15:05:11 +0200 Subject: [PATCH] Configurable entry point --- apps/com.example.launcher/assets/main.py | 14 +++++++++----- apps/com.example.wstest/META-INF/MANIFEST.MF | 1 + .../assets/{price.py => bitcoin_price.py} | 0 .../assets/{main.py => bitcoin_transactions.py} | 0 main.py | 15 +++++++++++++-- 5 files changed, 23 insertions(+), 7 deletions(-) rename apps/com.example.wstest/assets/{price.py => bitcoin_price.py} (100%) rename apps/com.example.wstest/assets/{main.py => bitcoin_transactions.py} (100%) diff --git a/apps/com.example.launcher/assets/main.py b/apps/com.example.launcher/assets/main.py index 64722c40..38d03c21 100644 --- a/apps/com.example.launcher/assets/main.py +++ b/apps/com.example.launcher/assets/main.py @@ -4,17 +4,18 @@ import machine def parse_manifest(manifest_path): name = "Unknown" + main_script = None # Default to None if Main-Script is not found try: with uio.open(manifest_path, 'r') as f: for line in f: - #print(f"Parsing line: {line}") line = line.strip() if line.startswith("Name:"): name = line.split(":", 1)[1].strip() - break + elif line.startswith("Main-Script:"): + main_script = line.split(":", 1)[1].strip() except OSError: print(f"Error reading {manifest_path}") - return name + return name, main_script def on_app_click(event, app_name, main_script, app_dir): @@ -44,9 +45,12 @@ for app_dir in [d for d in uos.listdir(apps_dir) if uos.stat(f"{apps_dir}/{d}")[ #icon_path = f"{base_path}/res/mipmap-mdpi/launcher_icon.png" icon_path = "/resources/icon_64x64.bin" manifest_path = f"{base_path}/META-INF/MANIFEST.MF" - main_script = f"{base_path}/assets/main.py" # Get app name from MANIFEST.MF - app_name = parse_manifest(manifest_path) + app_name, main_script = parse_manifest(manifest_path) + if main_script: + main_script = f"{base_path}/{main_script}" + else: + main_script = f"{base_path}/assets/main.py" # default # Create a container for each app (icon + label) app_cont = lv.obj(cont) app_cont.set_size(iconcont_width, iconcont_height) diff --git a/apps/com.example.wstest/META-INF/MANIFEST.MF b/apps/com.example.wstest/META-INF/MANIFEST.MF index 0f6745fb..7d46d4e9 100644 --- a/apps/com.example.wstest/META-INF/MANIFEST.MF +++ b/apps/com.example.wstest/META-INF/MANIFEST.MF @@ -1,2 +1,3 @@ Manifest-Version: 1.0 Name: WSTest +Main-Script: assets/bitcoin_price.py diff --git a/apps/com.example.wstest/assets/price.py b/apps/com.example.wstest/assets/bitcoin_price.py similarity index 100% rename from apps/com.example.wstest/assets/price.py rename to apps/com.example.wstest/assets/bitcoin_price.py diff --git a/apps/com.example.wstest/assets/main.py b/apps/com.example.wstest/assets/bitcoin_transactions.py similarity index 100% rename from apps/com.example.wstest/assets/main.py rename to apps/com.example.wstest/assets/bitcoin_transactions.py diff --git a/main.py b/main.py index 78875086..84ca5fae 100644 --- a/main.py +++ b/main.py @@ -236,6 +236,18 @@ restart_btn.add_event_cb(lambda event: machine.reset(),lv.EVENT.CLICKED,None) import _thread import traceback +def get_filename(path): + try: + if not path or not isinstance(path, str): + return None + # Extract filename using rsplit and take the last part + filename = path.rsplit('/', 1)[-1] + # Limit to the first 7 characters + return filename[:7] + except Exception as e: + print(f"Error extracting filename: {str(e)}") + return None + def execute_script(script_source, is_file, lvgl_obj, return_to_launcher): thread_id = _thread.get_ident() print(f"Thread {thread_id}: executing script") @@ -252,8 +264,7 @@ def execute_script(script_source, is_file, lvgl_obj, return_to_launcher): script_source = f.read() print(f"Thread {thread_id}: starting script") try: - # Use a short filename for compile() to avoid 'name too long' error - compile_name = 'script' if not is_file else 'main.py' # Shortened name + compile_name = 'script' if not is_file else get_filename(script_source) # Only filename, to avoid 'name too long' error compiled_script = compile(script_source, compile_name, 'exec') exec(compiled_script, script_globals) except Exception as e: