From c49b4ca2730822a92de25e63e70b58c744d629fa Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 24 May 2024 20:20:25 +0200 Subject: [PATCH] `platypush.run` should also support procedures in the format `procedure.`. --- platypush/utils/__init__.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/platypush/utils/__init__.py b/platypush/utils/__init__.py index 6cd4c48455..29448e217a 100644 --- a/platypush/utils/__init__.py +++ b/platypush/utils/__init__.py @@ -19,7 +19,7 @@ from importlib.util import spec_from_loader, module_from_spec from multiprocessing import Lock as PLock from tempfile import gettempdir from threading import Event, Lock as TLock -from typing import Callable, Generator, Optional, Tuple, Type, Union +from typing import Generator, Optional, Tuple, Type, Union from dateutil import parser, tz from redis import Redis @@ -469,7 +469,28 @@ def run(action, *args, **kwargs): >>> run('music.mpd.play', resource='file:///home/user/music.mp3') """ + from platypush.config import Config from platypush.context import get_plugin + from platypush.procedure import Procedure + + if action.startswith('procedure.'): + procedure_name = action.removeprefix('procedure.') + procedures = Config.get_procedures() + procedure = procedures.get(procedure_name) + if not procedure: + raise RuntimeError(f'No such procedure: {procedure_name}') + + if isinstance(procedure, dict): + procedure = Procedure.build( + name=procedure_name, + requests=procedure.get('actions', []), + args=procedure.get('args', {}), + _async=procedure.get('async', False), + ) + + return procedure.execute(*args, **kwargs) + + return procedure(*args, **kwargs) (module_name, method_name) = get_module_and_method_from_action(action) plugin = get_plugin(module_name)