Wraps a user-defined function to accept arguments as a dictionary. Args: user_function (Callable): The user-defined function to wrap. Returns: Callable: The wrapped function.
(user_function: Callable, with_task=False)
| 15 | |
| 16 | |
| 17 | def wrap_user_function(user_function: Callable, with_task=False) -> Callable: |
| 18 | """ |
| 19 | Wraps a user-defined function to accept arguments as a dictionary. |
| 20 | |
| 21 | Args: |
| 22 | user_function (Callable): The user-defined function to wrap. |
| 23 | |
| 24 | Returns: |
| 25 | Callable: The wrapped function. |
| 26 | """ |
| 27 | |
| 28 | @functools.wraps(user_function) |
| 29 | async def wrapper(*args): |
| 30 | # Get the parameter names of the user-defined function |
| 31 | user_function_params = list(inspect.signature(user_function).parameters.keys()) |
| 32 | |
| 33 | # Create a dictionary of parameter names and their corresponding values from *args |
| 34 | params_values = { |
| 35 | param_name: arg for param_name, arg in zip(user_function_params, args) |
| 36 | } |
| 37 | |
| 38 | if with_task: |
| 39 | await context.emitter.task_start() |
| 40 | |
| 41 | try: |
| 42 | # Call the user-defined function with the arguments |
| 43 | if inspect.iscoroutinefunction(user_function): |
| 44 | return await user_function(**params_values) |
| 45 | else: |
| 46 | return user_function(**params_values) |
| 47 | except CancelledError: |
| 48 | pass |
| 49 | except Exception as e: |
| 50 | logger.exception(e) |
| 51 | if with_task: |
| 52 | await ErrorMessage( |
| 53 | content=str(e) or e.__class__.__name__, author="Error" |
| 54 | ).send() |
| 55 | finally: |
| 56 | if with_task: |
| 57 | await context.emitter.task_end() |
| 58 | |
| 59 | return wrapper |
| 60 | |
| 61 | |
| 62 | def make_module_getattr(registry): |
no outgoing calls
no test coverage detected