From 5c2204f99d4a127f9191f542a81d87f0360941fc Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 24 May 2024 20:07:24 +0200 Subject: [PATCH] Allow for custom procedure names on the `@procedure` decorator. ``` @procedure("foo") def bar(): ... ``` Will now be published as `procedure.foo` instead of `procedure..bar`. --- platypush/config/__init__.py | 2 +- platypush/procedure/__init__.py | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/platypush/config/__init__.py b/platypush/config/__init__.py index ad64d5c46..7f6915931 100644 --- a/platypush/config/__init__.py +++ b/platypush/config/__init__.py @@ -355,7 +355,7 @@ class Config: prefix = modname + '.' if prefix is None else prefix self.procedures.update( **{ - prefix + name: obj + getattr(obj, 'procedure_name', f'{prefix}.{name}'): obj for name, obj in inspect.getmembers(module) if is_functional_procedure(obj) } diff --git a/platypush/procedure/__init__.py b/platypush/procedure/__init__.py index bf3afc0a0..7ef0cadd3 100644 --- a/platypush/procedure/__init__.py +++ b/platypush/procedure/__init__.py @@ -4,6 +4,7 @@ import re from functools import wraps from queue import LifoQueue +from typing import Optional from ..common import exec_wrapper from ..config import Config @@ -557,18 +558,22 @@ class IfProcedure(Procedure): return response -def procedure(f): - """ - Public decorator to mark a function as a procedure. - """ +def procedure(name: Optional[str] = None): + def func_wrapper(f): + """ + Public decorator to mark a function as a procedure. + """ - f.procedure = True + f.procedure = True + f.procedure_name = name - @wraps(f) - def _execute_procedure(*args, **kwargs): - return exec_wrapper(f, *args, **kwargs) + @wraps(f) + def _execute_procedure(*args, **kwargs): + return exec_wrapper(f, *args, **kwargs) - return _execute_procedure + return _execute_procedure + + return func_wrapper # vim:sw=4:ts=4:et: