Support both @procedure and @procedure(name) notations.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Fabio Manganiello 2024-05-26 11:02:19 +02:00
parent a4a776986b
commit de2bbc53c6
Signed by: blacklight
GPG key ID: D90FBA7F76362774
2 changed files with 9 additions and 2 deletions

View file

@ -355,7 +355,7 @@ class Config:
prefix = modname + '.' if prefix is None else prefix prefix = modname + '.' if prefix is None else prefix
self.procedures.update( self.procedures.update(
**{ **{
getattr(obj, 'procedure_name', f'{prefix}.{name}'): obj (getattr(obj, 'procedure_name', None) or f'{prefix}{name}'): obj
for name, obj in inspect.getmembers(module) for name, obj in inspect.getmembers(module)
if is_functional_procedure(obj) if is_functional_procedure(obj)
} }

View file

@ -558,7 +558,9 @@ class IfProcedure(Procedure):
return response return response
def procedure(name: Optional[str] = None): def procedure(name_or_func: Optional[str] = None, *upper_args, **upper_kwargs):
name = name_or_func if isinstance(name_or_func, str) else None
def func_wrapper(f): def func_wrapper(f):
""" """
Public decorator to mark a function as a procedure. Public decorator to mark a function as a procedure.
@ -569,10 +571,15 @@ def procedure(name: Optional[str] = None):
@wraps(f) @wraps(f)
def _execute_procedure(*args, **kwargs): def _execute_procedure(*args, **kwargs):
args = [*upper_args, *args]
kwargs = {**upper_kwargs, **kwargs}
return exec_wrapper(f, *args, **kwargs) return exec_wrapper(f, *args, **kwargs)
return _execute_procedure return _execute_procedure
if callable(name_or_func):
return func_wrapper(name_or_func)
return func_wrapper return func_wrapper