Skip to content

Commit a107192

Browse files
committed
handle pyscript decs (per craig)
1 parent 658a2a0 commit a107192

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

custom_components/pyscript/eval.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@
4141
"print",
4242
}
4343

44+
TRIG_DECORATORS = {
45+
"time_trigger",
46+
"state_trigger",
47+
"event_trigger",
48+
"mqtt_trigger",
49+
"state_active",
50+
"time_active",
51+
"task_unique",
52+
}
53+
54+
ALL_DECORATORS = TRIG_DECORATORS.union({"service"})
4455

4556
def ast_eval_exec_factory(ast_ctx, mode):
4657
"""Generate a function that executes eval() or exec() with given ast_ctx."""
@@ -280,15 +291,7 @@ async def trigger_init(self):
280291
"event_trigger",
281292
"mqtt_trigger",
282293
}
283-
trig_decorators = {
284-
"time_trigger",
285-
"state_trigger",
286-
"event_trigger",
287-
"mqtt_trigger",
288-
"state_active",
289-
"time_active",
290-
"task_unique",
291-
}
294+
292295
decorator_used = set()
293296
for dec in self.decorators:
294297
dec_name, dec_args, dec_kwargs = dec[0], dec[1], dec[2]
@@ -303,7 +306,7 @@ async def trigger_init(self):
303306
decorator_used.add(dec_name)
304307
if dec_name in trig_decorators_reqd:
305308
got_reqd_dec = True
306-
if dec_name in trig_decorators:
309+
if dec_name in TRIG_DECORATORS:
307310
if dec_name not in trig_args:
308311
trig_args[dec_name] = {}
309312
trig_args[dec_name]["args"] = []
@@ -383,7 +386,7 @@ async def do_service_call(func, ast_ctx, data):
383386
dec_name,
384387
)
385388

386-
for dec_name in trig_decorators:
389+
for dec_name in TRIG_DECORATORS:
387390
if dec_name in trig_args and len(trig_args[dec_name]["args"]) == 0:
388391
trig_args[dec_name]["args"] = None
389392

@@ -518,21 +521,23 @@ async def eval_decorators(self, ast_ctx):
518521
self.decorators = []
519522
code_str, code_list = ast_ctx.code_str, ast_ctx.code_list
520523
ast_ctx.code_str, ast_ctx.code_list = self.code_str, self.code_list
524+
525+
dec_funcs = []
521526
for dec in self.func_def.decorator_list:
522-
if isinstance(dec, ast.Call) and isinstance(dec.func, ast.Name):
523-
args = []
524-
kwargs = {}
525-
for arg in dec.args:
526-
args.append(await ast_ctx.aeval(arg))
527-
for keyw in dec.keywords:
528-
kwargs[keyw.arg] = await ast_ctx.aeval(keyw.value)
527+
if isinstance(dec, ast.Call) and isinstance(dec.func, ast.Name) and dec.func.id in ALL_DECORATORS:
528+
args = [await ast_ctx.aeval(arg) for arg in dec.args]
529+
kwargs = {keyw.arg: await ast_ctx.aeval(keyw.value) for keyw in dec.keywords}
529530
if len(kwargs) == 0:
530531
kwargs = None
531532
self.decorators.append([dec.func.id, args, kwargs])
532-
elif isinstance(dec, ast.Name):
533+
elif isinstance(dec, ast.Name) and dec.id in ALL_DECORATORS:
533534
self.decorators.append([dec.id, None, None])
534535
else:
535-
_LOGGER.error("function %s has unexpected decorator type %s", self.name, dec)
536+
dec_funcs.append(await ast_ctx.aeval(dec))
537+
538+
for func in reversed(dec_funcs):
539+
self.call = await ast_ctx.call_func(func, None, self.call)
540+
536541
ast_ctx.code_str, ast_ctx.code_list = code_str, code_list
537542

538543
async def resolve_nonlocals(self, ast_ctx):

0 commit comments

Comments
 (0)