ihook


Nameihook JSON
Version 0.1 PyPI version JSON
download
home_pagehttps://github.com/JezaChen/ihook
Summary`ihook` is a Python module that allows you to configure functions to call when importing modules.
upload_time2024-12-20 15:36:05
maintainerNone
docs_urlNone
authorJianzhang Chen
requires_python<4,>=3.7
licenseMIT
keywords hook import importlib
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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`:

```sh
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

```python
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

```python
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`.

```python
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.

```python
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.

```python
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`.

```python
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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/JezaChen/ihook",
    "name": "ihook",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.7",
    "maintainer_email": null,
    "keywords": "hook import importlib",
    "author": "Jianzhang Chen",
    "author_email": "jezachen@163.com",
    "download_url": "https://files.pythonhosted.org/packages/e9/4c/35ed0813214ee7fd7b94e46ef2eb25bcb5544f0d2be1c8b3ab2b41fd9028/ihook-0.1.tar.gz",
    "platform": null,
    "description": "# ihook\n\n`ihook` is a Python module that allows you to configure functions to call when importing modules.\nIt supports both case-sensitive and case-insensitive module names and can handle hooks for modules that are imported using methods of  `importlib`.\n\n## Features\n\n- Hook for modules that are imported.\n- Support for case-sensitive and case-insensitive module names.\n- Chain multiple hooks for the same or different modules.\n- Handle hooks for sub-packages and namespace packages.\n\n## Installation\n\nYou can install `ihook` using `pip`:\n\n```sh\npip install ihook\n```\n\n## Usage\n\n### Registering Hooks\n\nYou can register hooks using the `@on_import` decorator or by calling the `on_import` function directly.\n\n#### Using the Decorator\n\n```python\nimport ihook\n\n@ihook.on_import('math')\ndef on_math_import():\n    print('math module imported')\n\nimport math  # This will trigger the hook and print 'math module imported'\n```\n\n#### Using the Function Directly\n\n```python\nimport ihook\n\ndef on_socket_import():\n    print('socket module imported')\n\nihook.on_import('socket', on_socket_import)\n\nimport socket  # This will trigger the hook and print 'socket module imported'\n```\n\n### Case-Insensitive Hooks\n\nYou can register hooks for module names in a case-insensitive manner by setting the `case_sensitive` parameter to `False`.\n\n```python\nimport ihook\n\n@ihook.on_import('SocKet', case_sensitive=False)\ndef on_socket_import():\n    print('socket module imported (case-insensitive)')\n\nimport socket  # This will trigger the hook and print 'socket module imported (case-insensitive)'\n```\n\n### Hooks with Module Information\n\nYou can define hooks that take a `ModuleInfo` parameter, which provides more detailed information about the imported module.\nYou can handle direct access to the module object using the `module_object` attribute.\n\n```python\nimport ihook\n\n@ihook.on_import('socket')\ndef on_socket_import(module_info: ihook.ModuleInfo):\n    print(f'{module_info.module_name} module imported')\n    print(f'Module object: {module_info.module_object}')\n\nimport socket  # This will trigger the hook and print module information\n```\n\n### Clearing Hooks\n\nYou can clear all registered hooks using the `clear_hooks` function.\n\n```python\nimport ihook\n\nihook.clear_hooks()  # This will clear all registered hooks\n```\n\n## Advanced Usage\n\n### Handling Importlib\n\nYou can register hooks for modules that are imported or reloaded using `importlib`.\n\n```python\nimport ihook\n\n@ihook.on_import('hashlib')\ndef on_hashlib_import():\n    print('hashlib module imported')\n\nimport importlib\nhashlib = importlib.import_module('hashlib')  # This will trigger the hook and print 'hashlib module imported'\nimportlib.reload(hashlib)  # This will trigger the hook again and print 'hashlib module imported'\n```\n\n## License\n\nThis project is licensed under the MIT License. See the `LICENSE` file for details.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "`ihook` is a Python module that allows you to configure functions to call when importing modules.",
    "version": "0.1",
    "project_urls": {
        "Homepage": "https://github.com/JezaChen/ihook"
    },
    "split_keywords": [
        "hook",
        "import",
        "importlib"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6c5816103238f1f53d26534a64b1b2e1bf92b4cd970d947c3ea707662b4430d7",
                "md5": "e835289b8a291063cf25ad25e51d6206",
                "sha256": "1e1d7023d0395549a13e676a0d0527e70e6476b556e20395e2d7f868ef125871"
            },
            "downloads": -1,
            "filename": "ihook-0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e835289b8a291063cf25ad25e51d6206",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.7",
            "size": 6811,
            "upload_time": "2024-12-20T15:36:00",
            "upload_time_iso_8601": "2024-12-20T15:36:00.430369Z",
            "url": "https://files.pythonhosted.org/packages/6c/58/16103238f1f53d26534a64b1b2e1bf92b4cd970d947c3ea707662b4430d7/ihook-0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e94c35ed0813214ee7fd7b94e46ef2eb25bcb5544f0d2be1c8b3ab2b41fd9028",
                "md5": "b6dfd515d33065477d4fbc6cd7db076e",
                "sha256": "feec4d8921b9c1ce05a7251d923162e6a2cbe0fa849bf096006e223b3e760869"
            },
            "downloads": -1,
            "filename": "ihook-0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "b6dfd515d33065477d4fbc6cd7db076e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.7",
            "size": 9744,
            "upload_time": "2024-12-20T15:36:05",
            "upload_time_iso_8601": "2024-12-20T15:36:05.409002Z",
            "url": "https://files.pythonhosted.org/packages/e9/4c/35ed0813214ee7fd7b94e46ef2eb25bcb5544f0d2be1c8b3ab2b41fd9028/ihook-0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-20 15:36:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "JezaChen",
    "github_project": "ihook",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "ihook"
}
        
Elapsed time: 4.27943s