Add app's "assets" folder to sys.path

This commit is contained in:
Thomas Farstrike
2025-05-25 12:25:40 +02:00
parent 0317f387e7
commit f858cd0f10
2 changed files with 13 additions and 7 deletions
@@ -3,6 +3,8 @@ import time
import mpos.config
import mpos.ui
from wallet import LNBitsWallet
# screens:
main_screen = None
settings_screen = None
@@ -225,7 +227,7 @@ if wallet_type == "lnbits":
try:
wallet = LNBitsWallet(config.get_string("lnbits_url"), config.get_string("lnbits_readkey"))
except Exception as e:
print("Couldn't initialize LNBitsWallet because {e}")
print(f"Couldn't initialize LNBitsWallet because {e}")
elif wallet_type == "nwc":
try:
wallet = NWCWallet(config.get_string("nwc_url"))
+10 -6
View File
@@ -12,10 +12,10 @@ import mpos.info
import mpos.ui
# Run the script in the current thread:
def execute_script(script_source, is_file):
def execute_script(script_source, is_file, cwd=None):
thread_id = _thread.get_ident()
compile_name = 'script' if not is_file else script_source
print(f"Thread {thread_id}: executing script")
print(f"Thread {thread_id}: executing script with cwd: {cwd}")
try:
if is_file:
print(f"Thread {thread_id}: reading script from file {script_source}")
@@ -26,6 +26,10 @@ def execute_script(script_source, is_file):
'__name__': "__main__"
}
print(f"Thread {thread_id}: starting script")
import sys
path_before = sys.path
if cwd:
sys.path.append(cwd)
try:
compiled_script = compile(script_source, compile_name, 'exec')
exec(compiled_script, script_globals)
@@ -35,7 +39,7 @@ def execute_script(script_source, is_file):
tb = getattr(e, '__traceback__', None)
traceback.print_exception(type(e), e, tb)
print(f"Thread {thread_id}: script {compile_name} finished")
# Note that newscreen isn't deleted, as it might still be foreground, or it might be mpos.ui.rootscreen
sys.path = path_before
except Exception as e:
print(f"Thread {thread_id}: error:")
tb = getattr(e, '__traceback__', None)
@@ -83,7 +87,7 @@ def start_app(app_dir, is_launcher=False):
app = mpos.apps.parse_manifest(manifest_path)
start_script_fullpath = f"{app_dir}/{app.entrypoint}"
#execute_script_new_thread(start_script_fullpath, True, is_launcher, True) # Starting (GUI?) apps in a new thread can cause hangs (GIL lock?)
execute_script(start_script_fullpath, True)
execute_script(start_script_fullpath, True, app_dir + "/assets/")
# Launchers have the bar, other apps don't have it
if is_launcher:
mpos.ui.open_bar()
@@ -158,12 +162,12 @@ def auto_connect():
# Maybe start_app_by_name() and start_app_by_name() could be merged so the try-except logic is not duplicated...
try:
stat = uos.stat(custom_auto_connect)
execute_script_new_thread(custom_auto_connect, True, False, False)
execute_script_new_thread(custom_auto_connect, True)
except Exception as e:
try:
print(f"Couldn't execute {custom_auto_connect} because exception {e}, trying {builtin_auto_connect}...")
stat = uos.stat(builtin_auto_connect)
execute_script_new_thread(builtin_auto_connect, True, False, False)
execute_script_new_thread(builtin_auto_connect, True)
except Exception as e:
print("Couldn't execute {builtin_auto_connect} because exception {e}, continuing...")