Skip to content

Commit 68d4a5f

Browse files
committed
💥 Add ChainedHook
1 parent 2109f36 commit 68d4a5f

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ repos:
7575
additional_dependencies:
7676
- markdown-it-texmath
7777
- repo: https://github.com/astral-sh/ruff-pre-commit
78-
rev: v0.13.0
78+
rev: v0.13.3
7979
hooks:
8080
- id: ruff-check
8181
- id: ruff-format
8282
- repo: https://github.com/kumaraditya303/mirrors-pyright
83-
rev: v1.1.405
83+
rev: v1.1.406
8484
hooks:
8585
- id: pyright
8686

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,12 @@ StopHook(Wakatime())
123123
You can use many hooks at the same time:
124124

125125
```python
126+
from repl_python_wakatime.backends.chainedhook import ChainedHook
126127
from repl_python_wakatime.backends.codestats import CodeStats
127128
from repl_python_wakatime.backends.wakatime import Wakatime
128129
from repl_python_wakatime.frontends.python import Python
129130

130-
sys.ps1 = Python(Wakatime() | CodeStats())
131+
sys.ps1 = Python(ChainedHook(hooks=(Wakatime(), CodeStats())))
131132
```
132133

133134
## APIs

src/repl_python_wakatime/backends/__init__.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import os
66
from dataclasses import dataclass
77
from pathlib import Path
8-
from typing import Self
98

109

1110
@dataclass
@@ -19,7 +18,6 @@ class Hook:
1918
frontend: str = "python"
2019
language: str = "python"
2120
category: str = "coding"
22-
hooks: tuple[Self, ...] = ()
2321

2422
@property
2523
def plugin(self) -> str:
@@ -57,10 +55,5 @@ def get_project(
5755
return parent.name
5856
return cwd.name
5957

60-
def __or__(self, hook: Self) -> "Hook":
61-
return Hook(hooks=(self, hook))
62-
6358
def __call__(self) -> None:
6459
r"""Run hook"""
65-
for hook in self.hooks:
66-
hook()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Chained Hook
2+
===============
3+
4+
"""
5+
6+
from dataclasses import dataclass
7+
from typing import Any
8+
9+
from . import Hook
10+
11+
12+
@dataclass
13+
class ChainedHook(Hook):
14+
hooks: tuple[Hook, ...] = ()
15+
16+
def __call__(self) -> None:
17+
r"""Run hook"""
18+
for hook in self.hooks:
19+
hook()
20+
21+
def __setattr__(self, k: Any, v: Any) -> None:
22+
if k in {"frontend", "language", "category"}:
23+
for hook in self.hooks:
24+
setattr(hook, k, v)
25+
super().__setattr__(k, v)

0 commit comments

Comments
 (0)