# apfel.impt.transform

*module*

This module provides a low-level hook mechanism to transform the source code of a package or module during its import.
It must be imported **before** any other packages and modules that you want to hook.

> **Warning**
>
> **Effectful**
> 
> This package has an effectful behavior: modifying the `sys.meta_path` finder list, which will affect **the import process**, and is **sensitive to the order of imports**.

Normally you would use adapters defined in the `impt` package, instead of directly using the low level [`register_transform_hook`][apfel.impt.transform.register_transform_hook] function.

[View source](https://github.com/Yixuan-Wang/apfel/blob/main/src/apfel/impt/transform.py#L1-L1)

## apfel.impt.transform.TransformPathFinder

*class*

```python
class TransformPathFinder(_frozen_importlib_external.PathFinder)
```

Bases: `_frozen_importlib_external.PathFinder`

A subclass of [:python `importlib.machinery.PathFinder`](https://docs.python.org/3/library/importlib.html#importlib.machinery.PathFinder)
that injects [`TransformSourceFileLoader`][apfel.impt.transform.TransformSourceFileLoader] to the import process.

[View source](https://github.com/Yixuan-Wang/apfel/blob/main/src/apfel/impt/transform.py#L49-L76)

### apfel.impt.transform.TransformPathFinder.find_spec

*method* · *classmethod*

```python
def find_spec(fullname: str, path: Sequence[str] | None = None, target: ModuleType | None = None) -> importlib.machinery.ModuleSpec | None
```

[View source](https://github.com/Yixuan-Wang/apfel/blob/main/src/apfel/impt/transform.py#L57-L76)

## apfel.impt.transform.TransformSourceFileLoader

*class*

```python
class TransformSourceFileLoader(_frozen_importlib_external.SourceFileLoader)
```

Bases: `_frozen_importlib_external.SourceFileLoader`

A subclass of [:python `importlib.machinery.SourceFileLoader`](https://docs.python.org/3/library/importlib.html#importlib.machinery.SourceFileLoader)
that applies all registered hooks to the source code of a module or package during import.

> **Note**
>
> This loader will directly read the source code from the `.py` file, completely ignoring the `.pyc` file.
> The transformed source code will not have a bytecode cache (`.pyc`) generated.
> If your file system has high latency, this may slow down the import process.

[View source](https://github.com/Yixuan-Wang/apfel/blob/main/src/apfel/impt/transform.py#L20-L46)

### apfel.impt.transform.TransformSourceFileLoader.hook

*attribute* · *instance attribute*

```python
hook = hook
```

[View source](https://github.com/Yixuan-Wang/apfel/blob/main/src/apfel/impt/transform.py#L33-L33)

### apfel.impt.transform.TransformSourceFileLoader.__init__

*method*

```python
def __init__(fullname: str, path: str, *, hook: Sequence[Callable[[str], str]]) -> None
```

[View source](https://github.com/Yixuan-Wang/apfel/blob/main/src/apfel/impt/transform.py#L31-L33)

### apfel.impt.transform.TransformSourceFileLoader.get_data

*method*

```python
def get_data(path: str) -> bytes
```

[View source](https://github.com/Yixuan-Wang/apfel/blob/main/src/apfel/impt/transform.py#L35-L43)

### apfel.impt.transform.TransformSourceFileLoader.set_data

*method*

```python
def set_data(path, data, *, _mode: int = 438)
```

[View source](https://github.com/Yixuan-Wang/apfel/blob/main/src/apfel/impt/transform.py#L45-L46)

## apfel.impt.transform.register_transform_hook

*function*

```python
def register_transform_hook(name: str, hook: Callable[[str], str]) -> None
```

Register a hook function to replace the source code of a module or package.

**Parameters**

- `name` (`str`) — The name of the module or package to hook.
- `hook` (`Callable[[str], str]`) — The hook function that takes the original source code as input and returns the transformed source code.

[View source](https://github.com/Yixuan-Wang/apfel/blob/main/src/apfel/impt/transform.py#L79-L89)
