Skip to main content

`ihook` is a Python module that allows you to configure functions to call when importing modules.

Project description

ihook

codecov PyPI - Python Version GitHub

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

Un-patching

If you want to disable the hooking mechanism, you can use unpatch_meta_path to restore the original import mechanism. This function does not remove the registered hooks but disables the hooking mechanism.

import ihook

ihook.unpatch_meta_path()  # This will restore the original import mechanism

You can use patch_meta_path to re-enable the hooking mechanism. The registered hooks will still be available.

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

ihook-0.2.1.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ihook-0.2.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file ihook-0.2.1.tar.gz.

File metadata

  • Download URL: ihook-0.2.1.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ihook-0.2.1.tar.gz
Algorithm Hash digest
SHA256 6f09a5f3d043097b30123082ebae7af42158a80a779fc49dfbbcd6966e0ac01a
MD5 5b9fc72e61645efd7b3e96e4195f7d6f
BLAKE2b-256 44bad3b86bf84ead73ce8a70a14681fc46f77626eb56a3776c55ce49b203b339

See more details on using hashes here.

Provenance

The following attestation bundles were made for ihook-0.2.1.tar.gz:

Publisher: python-publish.yml on JezaChen/ihook

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ihook-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: ihook-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ihook-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5831159c49c4c13fe4cdd510e397f0485d76e9f0eeab0ea89e6939e66ea79c1c
MD5 537aea9f364c90c255eceaa8510ee998
BLAKE2b-256 cf0eb2a79cdc4052fb47b95cfcfb599df5ba12104bb8386d1e698f05423844a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for ihook-0.2.1-py3-none-any.whl:

Publisher: python-publish.yml on JezaChen/ihook

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page