Skip to content

Commit 70c4146

Browse files
committed
reload does log.info Reloaded/Unloaded/Loaded; see #106
1 parent e59345f commit 70c4146

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

custom_components/pyscript/__init__.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,8 @@ def import_recurse(ctx_name, visited, ctx2imports):
495495
if global_ctx_name in ctx_all:
496496
global_ctx = ctx_all[global_ctx_name]
497497
global_ctx.stop()
498-
_LOGGER.debug("reload: deleting global_ctx=%s", global_ctx_name)
498+
if global_ctx_name not in ctx2files or not ctx2files[global_ctx_name].autoload:
499+
_LOGGER.info("Unloaded %s", global_ctx.get_file_path())
499500
GlobalContextMgr.delete(global_ctx_name)
500501
await Function.reaper_sync()
501502

@@ -514,4 +515,7 @@ def import_recurse(ctx_name, visited, ctx2imports):
514515
source=src_info.source,
515516
mtime=src_info.mtime,
516517
)
517-
await GlobalContextMgr.load_file(global_ctx, src_info.file_path, source=src_info.source, force=True)
518+
reload = src_info.global_ctx_name in ctx_delete
519+
await GlobalContextMgr.load_file(
520+
global_ctx, src_info.file_path, source=src_info.source, reload=reload
521+
)

custom_components/pyscript/global_ctx.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __init__(
3636
self.module = None
3737
self.rel_import_path = rel_import_path
3838
self.source = source
39+
self.file_path = None
3940
self.mtime = mtime
4041
self.app_config = app_config
4142
self.imports = set()
@@ -97,6 +98,10 @@ def get_mtime(self):
9798
"""Return the mtime."""
9899
return self.mtime
99100

101+
def get_file_path(self):
102+
"""Return the file path."""
103+
return self.file_path
104+
100105
def get_imports(self):
101106
"""Return the imports."""
102107
return self.imports
@@ -287,7 +292,7 @@ def new_name(cls, root):
287292
return name
288293

289294
@classmethod
290-
async def load_file(cls, global_ctx, file_path, source=None, force=False):
295+
async def load_file(cls, global_ctx, file_path, source=None, reload=False):
291296
"""Load, parse and run the given script file; returns error ast_ctx on error, or None if ok."""
292297

293298
mtime = None
@@ -331,10 +336,11 @@ def read_file(path):
331336
global_ctx.stop()
332337
return False, ast_ctx
333338
global_ctx.source = source
339+
global_ctx.file_path = file_path
334340
if mtime is not None:
335341
global_ctx.mtime = mtime
336342
cls.set(global_ctx.get_name(), global_ctx)
337343

338-
_LOGGER.info("Loaded %s", file_path)
344+
_LOGGER.info("%s %s", "Reloaded" if reload else "Loaded", file_path)
339345

340346
return True, None

0 commit comments

Comments
 (0)