platypush.run should also support procedures in the format procedure.<name>.

This commit is contained in:
Fabio Manganiello 2024-05-24 20:20:25 +02:00
parent 5c2204f99d
commit c49b4ca273
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -19,7 +19,7 @@ from importlib.util import spec_from_loader, module_from_spec
from multiprocessing import Lock as PLock from multiprocessing import Lock as PLock
from tempfile import gettempdir from tempfile import gettempdir
from threading import Event, Lock as TLock 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 dateutil import parser, tz
from redis import Redis from redis import Redis
@ -469,7 +469,28 @@ def run(action, *args, **kwargs):
>>> run('music.mpd.play', resource='file:///home/user/music.mp3') >>> run('music.mpd.play', resource='file:///home/user/music.mp3')
""" """
from platypush.config import Config
from platypush.context import get_plugin 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) (module_name, method_name) = get_module_and_method_from_action(action)
plugin = get_plugin(module_name) plugin = get_plugin(module_name)