Add a Python function to the module.
(self, name: str, func: callable)
| 475 | } |
| 476 | |
| 477 | def add_python_function(self, name: str, func: callable): |
| 478 | """Add a Python function to the module.""" |
| 479 | self.pyfuncs[name] = func |
| 480 | |
| 481 | # Create a wrapper that handles both instance methods and static functions |
| 482 | # pylint: disable=import-outside-toplevel |
| 483 | import functools |
| 484 | |
| 485 | @functools.wraps(func) |
| 486 | def wrapper(*args, **kwargs): |
| 487 | sig = inspect.signature(func) |
| 488 | params = list(sig.parameters.keys()) |
| 489 | |
| 490 | if params and params[0] == "self": |
| 491 | return func(self, *args, **kwargs) |
| 492 | else: |
| 493 | return func(*args, **kwargs) |
| 494 | |
| 495 | # Set the wrapper as an instance attribute |
| 496 | setattr(self, name, wrapper) |
| 497 | |
| 498 | def script( |
| 499 | self, |
no outgoing calls