1212import mpos .ui
1313
1414# Run the script in the current thread:
15- def execute_script (script_source , is_file ):
15+ def execute_script (script_source , is_file , cwd = None ):
1616 thread_id = _thread .get_ident ()
1717 compile_name = 'script' if not is_file else script_source
18- print (f"Thread { thread_id } : executing script" )
18+ print (f"Thread { thread_id } : executing script with cwd: { cwd } " )
1919 try :
2020 if is_file :
2121 print (f"Thread { thread_id } : reading script from file { script_source } " )
@@ -26,6 +26,10 @@ def execute_script(script_source, is_file):
2626 '__name__' : "__main__"
2727 }
2828 print (f"Thread { thread_id } : starting script" )
29+ import sys
30+ path_before = sys .path
31+ if cwd :
32+ sys .path .append (cwd )
2933 try :
3034 compiled_script = compile (script_source , compile_name , 'exec' )
3135 exec (compiled_script , script_globals )
@@ -35,7 +39,7 @@ def execute_script(script_source, is_file):
3539 tb = getattr (e , '__traceback__' , None )
3640 traceback .print_exception (type (e ), e , tb )
3741 print (f"Thread { thread_id } : script { compile_name } finished" )
38- # Note that newscreen isn't deleted, as it might still be foreground, or it might be mpos.ui.rootscreen
42+ sys . path = path_before
3943 except Exception as e :
4044 print (f"Thread { thread_id } : error:" )
4145 tb = getattr (e , '__traceback__' , None )
@@ -83,7 +87,7 @@ def start_app(app_dir, is_launcher=False):
8387 app = mpos .apps .parse_manifest (manifest_path )
8488 start_script_fullpath = f"{ app_dir } /{ app .entrypoint } "
8589 #execute_script_new_thread(start_script_fullpath, True, is_launcher, True) # Starting (GUI?) apps in a new thread can cause hangs (GIL lock?)
86- execute_script (start_script_fullpath , True )
90+ execute_script (start_script_fullpath , True , app_dir + "/assets/" )
8791 # Launchers have the bar, other apps don't have it
8892 if is_launcher :
8993 mpos .ui .open_bar ()
@@ -158,12 +162,12 @@ def auto_connect():
158162 # Maybe start_app_by_name() and start_app_by_name() could be merged so the try-except logic is not duplicated...
159163 try :
160164 stat = uos .stat (custom_auto_connect )
161- execute_script_new_thread (custom_auto_connect , True , False , False )
165+ execute_script_new_thread (custom_auto_connect , True )
162166 except Exception as e :
163167 try :
164168 print (f"Couldn't execute { custom_auto_connect } because exception { e } , trying { builtin_auto_connect } ..." )
165169 stat = uos .stat (builtin_auto_connect )
166- execute_script_new_thread (builtin_auto_connect , True , False , False )
170+ execute_script_new_thread (builtin_auto_connect , True )
167171 except Exception as e :
168172 print ("Couldn't execute {builtin_auto_connect} because exception {e}, continuing..." )
169173
0 commit comments