Skip to content

Commit e4317d8

Browse files
committed
adds __name__ for script files; fixes #38
1 parent cdd6988 commit e4317d8

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

custom_components/pyscript/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,16 @@ def glob_files(load_paths, config):
209209
rel_import_path = f"{path}/mod_name"
210210
if path == "":
211211
global_ctx_name = f"file.{mod_name}"
212+
fq_mod_name = mod_name
212213
else:
213214
global_ctx_name = f"{path}.{mod_name}"
215+
fq_mod_name = global_ctx_name
214216
if check_config:
215217
_LOGGER.debug("load_scripts: checking %s in %s", mod_name, apps_config)
216218
if not isinstance(apps_config, dict) or mod_name not in apps_config:
217219
_LOGGER.debug("load_scripts: skipping %s because config not present", this_path)
218220
continue
219-
source_files.append([global_ctx_name, this_path, rel_import_path])
221+
source_files.append([global_ctx_name, this_path, rel_import_path, fq_mod_name])
220222
return source_files
221223

222224
load_paths = [
@@ -226,8 +228,11 @@ def glob_files(load_paths, config):
226228
]
227229

228230
source_files = await hass.async_add_executor_job(glob_files, load_paths, config)
229-
for global_ctx_name, source_file, rel_import_path in source_files:
231+
for global_ctx_name, source_file, rel_import_path, fq_mod_name in source_files:
230232
global_ctx = GlobalContext(
231-
global_ctx_name, global_sym_table={}, manager=GlobalContextMgr, rel_import_path=rel_import_path
233+
global_ctx_name,
234+
global_sym_table={"__name__": fq_mod_name},
235+
manager=GlobalContextMgr,
236+
rel_import_path=rel_import_path,
232237
)
233238
await GlobalContextMgr.load_file(source_file, global_ctx)

tests/test_function.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def func1(var_name=None, value=None):
184184
185185
seq_num += 1
186186
log.info(f"func1 var = {var_name}, value = {value}")
187-
pyscript.done = [seq_num, var_name, int(value), sqrt(1024)]
187+
pyscript.done = [seq_num, var_name, int(value), sqrt(1024), __name__]
188188
189189
@state_trigger("pyscript.f1var1 == '1' or pyscript.f2var2 == '2' or pyscript.no_such_var == '10' or pyscript.no_such_var.attr == 100")
190190
@state_active("pyscript.f2var3 == '3' and pyscript.f2var4 == '4'")
@@ -378,6 +378,7 @@ def func8(var_name=None, value=None, old_value=None):
378378
"pyscript.f1var1",
379379
1,
380380
32,
381+
"hello",
381382
]
382383
assert "func1 var = pyscript.f1var1, value = 1" in caplog.text
383384

@@ -392,6 +393,7 @@ def func8(var_name=None, value=None, old_value=None):
392393
"pyscript.f1var1",
393394
1,
394395
32,
396+
"hello",
395397
]
396398

397399
seq_num += 1

0 commit comments

Comments
 (0)