`ihook` is a Python module that allows you to configure functions to call when importing modules.
Project description
ihook
ihook is a Python module that allows you to configure functions to call when importing modules.
It supports both case-sensitive and case-insensitive module names and can handle hooks for modules that are imported using methods of importlib.
Features
- Hook for modules that are imported.
- Support for case-sensitive and case-insensitive module names.
- Chain multiple hooks for the same or different modules.
- Handle hooks for sub-packages and namespace packages.
Installation
You can install ihook using pip:
pip install ihook
Usage
Registering Hooks
You can register hooks using the @on_import decorator or by calling the on_import function directly.
Using the Decorator
import ihook
@ihook.on_import('math')
def on_math_import():
print('math module imported')
import math # This will trigger the hook and print 'math module imported'
Using the Function Directly
import ihook
def on_socket_import():
print('socket module imported')
ihook.on_import('socket', on_socket_import)
import socket # This will trigger the hook and print 'socket module imported'
Case-Insensitive Hooks
You can register hooks for module names in a case-insensitive manner by setting the case_sensitive parameter to False.
import ihook
@ihook.on_import('SocKet', case_sensitive=False)
def on_socket_import():
print('socket module imported (case-insensitive)')
import socket # This will trigger the hook and print 'socket module imported (case-insensitive)'
Hooks with Module Information
You can define hooks that take a ModuleInfo parameter, which provides more detailed information about the imported module.
You can handle direct access to the module object using the module_object attribute.
import ihook
@ihook.on_import('socket')
def on_socket_import(module_info: ihook.ModuleInfo):
print(f'{module_info.module_name} module imported')
print(f'Module object: {module_info.module_object}')
import socket # This will trigger the hook and print module information
Clearing Hooks
You can clear all registered hooks using the clear_hooks function.
import ihook
ihook.clear_hooks() # This will clear all registered hooks
Advanced Usage
Handling Importlib
You can register hooks for modules that are imported or reloaded using importlib.
import ihook
@ihook.on_import('hashlib')
def on_hashlib_import():
print('hashlib module imported')
import importlib
hashlib = importlib.import_module('hashlib') # This will trigger the hook and print 'hashlib module imported'
importlib.reload(hashlib) # This will trigger the hook again and print 'hashlib module imported'
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ihook-0.1.1.tar.gz.
File metadata
- Download URL: ihook-0.1.1.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd7690b21c9165e703d0f92cb054d2fcf22d686081275f2c4444267c597ea711
|
|
| MD5 |
78dd3b72d2f036163ad779892b58b8ba
|
|
| BLAKE2b-256 |
e1fe530036c48a8109c50dfbf04899877b19a49c3eb58fb4264dbe4f09acfdf8
|
Provenance
The following attestation bundles were made for ihook-0.1.1.tar.gz:
Publisher:
python-publish.yml on JezaChen/ihook
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ihook-0.1.1.tar.gz -
Subject digest:
dd7690b21c9165e703d0f92cb054d2fcf22d686081275f2c4444267c597ea711 - Sigstore transparency entry: 158008179
- Sigstore integration time:
-
Permalink:
JezaChen/ihook@42c6d8b0d6b3be0ec2ba62d4f24e6648eee46923 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/JezaChen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@42c6d8b0d6b3be0ec2ba62d4f24e6648eee46923 -
Trigger Event:
release
-
Statement type:
File details
Details for the file ihook-0.1.1-py3-none-any.whl.
File metadata
- Download URL: ihook-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4abcb3880220e12836c587dc65cf7b90e1b3a8993b74b370b568154ce0f6df8
|
|
| MD5 |
cdb5707ca6daa1b5c5a3d456d63640fb
|
|
| BLAKE2b-256 |
19c8d03941106508b9ca5816dad75587a6372a9e5b83133ac29588222066821d
|
Provenance
The following attestation bundles were made for ihook-0.1.1-py3-none-any.whl:
Publisher:
python-publish.yml on JezaChen/ihook
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ihook-0.1.1-py3-none-any.whl -
Subject digest:
f4abcb3880220e12836c587dc65cf7b90e1b3a8993b74b370b568154ce0f6df8 - Sigstore transparency entry: 158008180
- Sigstore integration time:
-
Permalink:
JezaChen/ihook@42c6d8b0d6b3be0ec2ba62d4f24e6648eee46923 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/JezaChen
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@42c6d8b0d6b3be0ec2ba62d4f24e6648eee46923 -
Trigger Event:
release
-
Statement type: