Skip to content

New Wrapping PR 1: Install wrapt and add helper functions for new wrapping system #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions aikido_zen/sinks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,0 +1,71 @@
from wrapt import wrap_object, FunctionWrapper, when_imported

from aikido_zen.background_process.packages import ANY_VERSION, is_package_compatible
from aikido_zen.errors import AikidoException
from aikido_zen.helpers.logging import logger


def on_import(name, package="", version_requirement=ANY_VERSION):
"""
Decorator to register a function to be called when a package is imported.
It checks if the package is compatible with the specified version requirement.
"""

def decorator(func):
if package and not is_package_compatible(package, version_requirement):
return

Check warning on line 16 in aikido_zen/sinks/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/__init__.py#L14-L16

Added lines #L14 - L16 were not covered by tests

when_imported(name)(func) # Register the function to be called on import

Check warning on line 18 in aikido_zen/sinks/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/__init__.py#L18

Added line #L18 was not covered by tests

return decorator

Check warning on line 20 in aikido_zen/sinks/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/__init__.py#L20

Added line #L20 was not covered by tests


def patch_function(module, name, wrapper):
"""
Patches a function in the specified module with a wrapper function.
"""
try:
wrap_object(module, name, FunctionWrapper, (wrapper,))
except Exception as e:
logger.info("Failed to wrap %s:%s, due to: %s", module, name, e)

Check warning on line 30 in aikido_zen/sinks/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/__init__.py#L27-L30

Added lines #L27 - L30 were not covered by tests


def before(wrapper):
"""
Surrounds a patch with try-except and calls the original function at the end
"""

def decorator(func, instance, args, kwargs):
try:
wrapper(func, instance, args, kwargs) # Call the patch
except AikidoException as e:
raise e # Re-raise AikidoException
except Exception as e:
logger.debug(

Check warning on line 44 in aikido_zen/sinks/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/__init__.py#L38-L44

Added lines #L38 - L44 were not covered by tests
"%s:%s wrapping-before error: %s", func.__module__, func.__name__, e
)

return func(*args, **kwargs) # Call the original function

Check warning on line 48 in aikido_zen/sinks/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/__init__.py#L48

Added line #L48 was not covered by tests

return decorator

Check warning on line 50 in aikido_zen/sinks/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/__init__.py#L50

Added line #L50 was not covered by tests


def after(wrapper):
"""
Surrounds a patch with try-except, calls the original function and gives the return value to the patch
"""

def decorator(func, instance, args, kwargs):
return_value = func(*args, **kwargs) # Call the original function
try:
wrapper(func, instance, args, kwargs, return_value) # Call the patch
except AikidoException as e:
raise e # Re-raise AikidoException
except Exception as e:
logger.debug(

Check warning on line 65 in aikido_zen/sinks/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/__init__.py#L58-L65

Added lines #L58 - L65 were not covered by tests
"%s:%s wrapping-after error: %s", func.__module__, func.__name__, e
)

return return_value

Check warning on line 69 in aikido_zen/sinks/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/__init__.py#L69

Added line #L69 was not covered by tests

return decorator

Check warning on line 71 in aikido_zen/sinks/__init__.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/__init__.py#L71

Added line #L71 was not covered by tests
91 changes: 90 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ python-dotenv = "^1.0.1"
regex = "^2024.5.15"
requests = "^2.32.3"
packaging = "^24.1"
wrapt = "^1.17.2"

[tool.poetry.group.dev.dependencies]
black = "^24.4.2"
Expand Down
Loading