simplug 0.5.7__tar.gz → 0.5.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- simplug-0.5.8/.editorconfig +8 -0
- simplug-0.5.8/.github/workflows/build.yml +56 -0
- simplug-0.5.8/.github/workflows/docs.yml +29 -0
- simplug-0.5.8/.gitignore +155 -0
- simplug-0.5.8/.pre-commit-config.yaml +46 -0
- simplug-0.5.8/AGENTS.md +74 -0
- {simplug-0.5.7/simplug.egg-info → simplug-0.5.8}/PKG-INFO +6 -7
- simplug-0.5.8/docs/examples.md +296 -0
- simplug-0.5.8/docs/getting-started.md +147 -0
- simplug-0.5.8/docs/guide/async-hooks.md +287 -0
- simplug-0.5.8/docs/guide/defining-hooks.md +182 -0
- simplug-0.5.8/docs/guide/entrypoints.md +266 -0
- simplug-0.5.8/docs/guide/implementing-hooks.md +279 -0
- simplug-0.5.8/docs/guide/plugin-registry.md +313 -0
- simplug-0.5.8/docs/guide/priority-system.md +286 -0
- simplug-0.5.8/docs/guide/result-collection.md +367 -0
- simplug-0.5.8/docs/index.md +85 -0
- simplug-0.5.8/docs/requirements.txt +4 -0
- simplug-0.5.8/docs/style.css +115 -0
- simplug-0.5.8/examples/complete/AGENTS.md +46 -0
- simplug-0.5.8/examples/complete/__init__.py +0 -0
- simplug-0.5.8/examples/complete/__main__.py +4 -0
- simplug-0.5.8/examples/complete/hookspecs.py +19 -0
- simplug-0.5.8/examples/complete/host.py +44 -0
- simplug-0.5.8/examples/complete/lib.py +15 -0
- simplug-0.5.8/examples/complete/plugin/plugin.py +24 -0
- simplug-0.5.8/examples/complete/plugin/setup.py +7 -0
- simplug-0.5.8/examples/toy.py +30 -0
- simplug-0.5.8/mkdocs.yml +77 -0
- {simplug-0.5.7 → simplug-0.5.8}/pyproject.toml +6 -3
- {simplug-0.5.7 → simplug-0.5.8}/simplug/__init__.py +37 -21
- simplug-0.5.8/simplug/py.typed +0 -0
- simplug-0.5.8/tests/__init__.py +0 -0
- simplug-0.5.8/tests/entrypoint_plugin/__init__.py +0 -0
- simplug-0.5.8/tests/entrypoint_plugin/entrypoint_plugin.py +7 -0
- simplug-0.5.8/tests/entrypoint_plugin/setup.py +7 -0
- simplug-0.5.8/tests/plugin_module.py +10 -0
- simplug-0.5.8/tox.ini +4 -0
- simplug-0.5.8/uv.lock +2025 -0
- simplug-0.5.7/PKG-INFO +0 -88
- simplug-0.5.7/setup.cfg +0 -4
- simplug-0.5.7/simplug.egg-info/SOURCES.txt +0 -11
- simplug-0.5.7/simplug.egg-info/dependency_links.txt +0 -1
- simplug-0.5.7/simplug.egg-info/requires.txt +0 -1
- simplug-0.5.7/simplug.egg-info/top_level.txt +0 -1
- {simplug-0.5.7 → simplug-0.5.8}/LICENSE +0 -0
- {simplug-0.5.7 → simplug-0.5.8}/README.md +0 -0
- /simplug-0.5.7/simplug/py.typed → /simplug-0.5.8/examples/__init__.py +0 -0
- {simplug-0.5.7 → simplug-0.5.8}/tests/test_simplug.py +0 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
name: Build and Deploy
|
|
2
|
+
|
|
3
|
+
on: [push, pull_request]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
|
|
7
|
+
build:
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
if: "! contains(github.event.head_commit.message, 'wip')"
|
|
10
|
+
strategy:
|
|
11
|
+
matrix:
|
|
12
|
+
python-version: [3.9, "3.10", "3.11", "3.12"]
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v6
|
|
16
|
+
- name: Install uv
|
|
17
|
+
uses: astral-sh/setup-uv@v8.0.0
|
|
18
|
+
with:
|
|
19
|
+
python-version: ${{ matrix.python-version }}
|
|
20
|
+
- name: Install dependencies
|
|
21
|
+
run: |
|
|
22
|
+
uv sync
|
|
23
|
+
- name: Run flake8
|
|
24
|
+
run: uv run flake8 simplug
|
|
25
|
+
- name: Test with pytest
|
|
26
|
+
run: uv run pytest tests/ --junitxml=junit/test-results-${{ matrix.python-version }}.xml
|
|
27
|
+
- name: Upload pytest test results
|
|
28
|
+
uses: actions/upload-artifact@v7
|
|
29
|
+
with:
|
|
30
|
+
name: pytest-results-${{ matrix.python-version }}
|
|
31
|
+
path: junit/test-results-${{ matrix.python-version }}.xml
|
|
32
|
+
# Use always() to always run this step to publish test results when there are test failures
|
|
33
|
+
if: ${{ always() }}
|
|
34
|
+
- name: Run codacy-coverage-reporter
|
|
35
|
+
uses: codacy/codacy-coverage-reporter-action@master
|
|
36
|
+
if: matrix.python-version == 3.12
|
|
37
|
+
with:
|
|
38
|
+
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
|
|
39
|
+
coverage-reports: .coverage.xml
|
|
40
|
+
|
|
41
|
+
deploy:
|
|
42
|
+
needs: build
|
|
43
|
+
runs-on: ubuntu-latest
|
|
44
|
+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
|
|
45
|
+
strategy:
|
|
46
|
+
matrix:
|
|
47
|
+
python-version: ["3.12"]
|
|
48
|
+
steps:
|
|
49
|
+
- uses: actions/checkout@v6
|
|
50
|
+
- name: Install uv
|
|
51
|
+
uses: astral-sh/setup-uv@v8.0.0
|
|
52
|
+
with:
|
|
53
|
+
python-version: ${{ matrix.python-version }}
|
|
54
|
+
- name: Publish to PyPI
|
|
55
|
+
run: uv build && uv publish --username ${{ secrets.PYPI_USER }} --password ${{ secrets.PYPI_PASSWORD }}
|
|
56
|
+
if: success()
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
name: Build Docs
|
|
2
|
+
|
|
3
|
+
on: [push]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
docs:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
if: "! contains(github.event.head_commit.message, 'wip')"
|
|
9
|
+
strategy:
|
|
10
|
+
matrix:
|
|
11
|
+
python-version: ["3.12"]
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v6
|
|
14
|
+
- name: Install uv
|
|
15
|
+
uses: astral-sh/setup-uv@v8.0.0
|
|
16
|
+
with:
|
|
17
|
+
python-version: ${{ matrix.python-version }}
|
|
18
|
+
- name: Install dependencies
|
|
19
|
+
run: uv sync --group docs
|
|
20
|
+
- name: Update docs
|
|
21
|
+
run: |
|
|
22
|
+
uv pip install mkdocs
|
|
23
|
+
uv pip install -r docs/requirements.txt
|
|
24
|
+
|
|
25
|
+
cd docs
|
|
26
|
+
cp ../README.md index.md
|
|
27
|
+
cd ..
|
|
28
|
+
uv run mkdocs gh-deploy --clean --force
|
|
29
|
+
if: success()
|
simplug-0.5.8/.gitignore
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
|
|
2
|
+
# Created by https://www.gitignore.io/api/python
|
|
3
|
+
# Edit at https://www.gitignore.io/?templates=python
|
|
4
|
+
|
|
5
|
+
### Python ###
|
|
6
|
+
# Byte-compiled / optimized / DLL files
|
|
7
|
+
__pycache__/
|
|
8
|
+
*.py[cod]
|
|
9
|
+
*$py.class
|
|
10
|
+
|
|
11
|
+
# C extensions
|
|
12
|
+
*.so
|
|
13
|
+
|
|
14
|
+
# Distribution / packaging
|
|
15
|
+
.Python
|
|
16
|
+
build/
|
|
17
|
+
develop-eggs/
|
|
18
|
+
dist/
|
|
19
|
+
downloads/
|
|
20
|
+
eggs/
|
|
21
|
+
.eggs/
|
|
22
|
+
lib/
|
|
23
|
+
lib64/
|
|
24
|
+
parts/
|
|
25
|
+
sdist/
|
|
26
|
+
var/
|
|
27
|
+
wheels/
|
|
28
|
+
pip-wheel-metadata/
|
|
29
|
+
share/python-wheels/
|
|
30
|
+
*.egg-info/
|
|
31
|
+
.installed.cfg
|
|
32
|
+
*.egg
|
|
33
|
+
MANIFEST
|
|
34
|
+
|
|
35
|
+
# PyInstaller
|
|
36
|
+
# Usually these files are written by a python script from a template
|
|
37
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
38
|
+
*.manifest
|
|
39
|
+
*.spec
|
|
40
|
+
|
|
41
|
+
# Installer logs
|
|
42
|
+
pip-log.txt
|
|
43
|
+
pip-delete-this-directory.txt
|
|
44
|
+
|
|
45
|
+
# Unit test / coverage reports
|
|
46
|
+
htmlcov/
|
|
47
|
+
.tox/
|
|
48
|
+
.nox/
|
|
49
|
+
.coverage
|
|
50
|
+
.coverage.*
|
|
51
|
+
.cache
|
|
52
|
+
nosetests.xml
|
|
53
|
+
coverage.xml
|
|
54
|
+
*.cover
|
|
55
|
+
.hypothesis/
|
|
56
|
+
.pytest_cache/
|
|
57
|
+
|
|
58
|
+
# Translations
|
|
59
|
+
*.mo
|
|
60
|
+
*.pot
|
|
61
|
+
|
|
62
|
+
# Django stuff:
|
|
63
|
+
*.log
|
|
64
|
+
local_settings.py
|
|
65
|
+
db.sqlite3
|
|
66
|
+
|
|
67
|
+
# Flask stuff:
|
|
68
|
+
instance/
|
|
69
|
+
.webassets-cache
|
|
70
|
+
|
|
71
|
+
# Scrapy stuff:
|
|
72
|
+
.scrapy
|
|
73
|
+
|
|
74
|
+
# Sphinx documentation
|
|
75
|
+
docs/_build/
|
|
76
|
+
|
|
77
|
+
# PyBuilder
|
|
78
|
+
target/
|
|
79
|
+
|
|
80
|
+
# Jupyter Notebook
|
|
81
|
+
.ipynb_checkpoints
|
|
82
|
+
|
|
83
|
+
# IPython
|
|
84
|
+
profile_default/
|
|
85
|
+
ipython_config.py
|
|
86
|
+
|
|
87
|
+
# pyenv
|
|
88
|
+
.python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don’t work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# celery beat schedule file
|
|
98
|
+
celerybeat-schedule
|
|
99
|
+
|
|
100
|
+
# SageMath parsed files
|
|
101
|
+
*.sage.py
|
|
102
|
+
|
|
103
|
+
# Environments
|
|
104
|
+
.env
|
|
105
|
+
.venv
|
|
106
|
+
env/
|
|
107
|
+
venv/
|
|
108
|
+
ENV/
|
|
109
|
+
env.bak/
|
|
110
|
+
venv.bak/
|
|
111
|
+
|
|
112
|
+
# Spyder project settings
|
|
113
|
+
.spyderproject
|
|
114
|
+
.spyproject
|
|
115
|
+
|
|
116
|
+
# Rope project settings
|
|
117
|
+
.ropeproject
|
|
118
|
+
|
|
119
|
+
# mkdocs documentation
|
|
120
|
+
/site
|
|
121
|
+
|
|
122
|
+
# mypy
|
|
123
|
+
.mypy_cache/
|
|
124
|
+
.dmypy.json
|
|
125
|
+
dmypy.json
|
|
126
|
+
|
|
127
|
+
# Pyre type checker
|
|
128
|
+
.pyre/
|
|
129
|
+
|
|
130
|
+
.vscode/
|
|
131
|
+
|
|
132
|
+
*.bak
|
|
133
|
+
|
|
134
|
+
# data for Examples
|
|
135
|
+
examples/data/
|
|
136
|
+
|
|
137
|
+
# logs
|
|
138
|
+
plkit_logs/
|
|
139
|
+
hparams.yaml
|
|
140
|
+
|
|
141
|
+
workdir/
|
|
142
|
+
|
|
143
|
+
**/t-*.ipynb
|
|
144
|
+
.in.fish
|
|
145
|
+
.out.fish
|
|
146
|
+
|
|
147
|
+
# local history for vscode
|
|
148
|
+
.history/
|
|
149
|
+
|
|
150
|
+
# lightning_logs, plkit_logs
|
|
151
|
+
*_logs/
|
|
152
|
+
|
|
153
|
+
docs/api/
|
|
154
|
+
|
|
155
|
+
# End of https://www.gitignore.io/api/python
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# See https://pre-commit.com for more information
|
|
2
|
+
# See https://pre-commit.com/hooks.html for more hooks
|
|
3
|
+
fail_fast: false
|
|
4
|
+
exclude: '^tests/|^setup\.py$|^examples/'
|
|
5
|
+
repos:
|
|
6
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
7
|
+
rev: 5df1a4bf6f04a1ed3a643167b38d502575e29aef
|
|
8
|
+
hooks:
|
|
9
|
+
- id: trailing-whitespace
|
|
10
|
+
- id: end-of-file-fixer
|
|
11
|
+
- id: check-yaml
|
|
12
|
+
- id: check-added-large-files
|
|
13
|
+
- repo: local
|
|
14
|
+
hooks:
|
|
15
|
+
- id: flake8
|
|
16
|
+
name: Run flake8
|
|
17
|
+
files: ^simplug\.py$
|
|
18
|
+
pass_filenames: true
|
|
19
|
+
entry: flake8
|
|
20
|
+
types: [python]
|
|
21
|
+
language: system
|
|
22
|
+
- id: mypy
|
|
23
|
+
name: Run mypy
|
|
24
|
+
files: ^simplug\.py$
|
|
25
|
+
pass_filenames: true
|
|
26
|
+
entry: mypy
|
|
27
|
+
types: [python]
|
|
28
|
+
language: system
|
|
29
|
+
- id: versionchecker
|
|
30
|
+
name: Check version agreement in pyproject and __version__
|
|
31
|
+
entry: bash -c
|
|
32
|
+
language: system
|
|
33
|
+
args:
|
|
34
|
+
- get_ver() { echo $(egrep "^__version|^version" $1 | cut -d= -f2 | sed 's/\"\| //g'); };
|
|
35
|
+
v1=`get_ver pyproject.toml`;
|
|
36
|
+
v2=`get_ver simplug.py`;
|
|
37
|
+
if [[ $v1 == $v2 ]]; then exit 0; else exit 1; fi
|
|
38
|
+
pass_filenames: false
|
|
39
|
+
files: ^pyproject\.toml|simplug\.py$
|
|
40
|
+
- id: pytest
|
|
41
|
+
name: Run pytest
|
|
42
|
+
entry: pytest
|
|
43
|
+
language: system
|
|
44
|
+
args: [tests/]
|
|
45
|
+
pass_filenames: false
|
|
46
|
+
files: ^tests/.+$|^simplug\.py$
|
simplug-0.5.8/AGENTS.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# PROJECT KNOWLEDGE BASE
|
|
2
|
+
|
|
3
|
+
**Generated:** 2026-01-14
|
|
4
|
+
**Commit:** N/A
|
|
5
|
+
**Branch:** main
|
|
6
|
+
|
|
7
|
+
## OVERVIEW
|
|
8
|
+
Simple plugin system for Python with async hooks. Single-file architecture (~1,300 lines) with comprehensive test coverage. Uses decorator-based API with 18 result collection strategies.
|
|
9
|
+
|
|
10
|
+
## STRUCTURE
|
|
11
|
+
```
|
|
12
|
+
simplug/
|
|
13
|
+
├── simplug.py # Single-file module (entire library)
|
|
14
|
+
├── tests/ # Test suite
|
|
15
|
+
│ └── test_simplug.py # 2,072 lines, 68 tests
|
|
16
|
+
├── examples/ # Usage examples
|
|
17
|
+
│ ├── toy.py # Minimal 30-line demo
|
|
18
|
+
│ └── complete/ # Full example with entrypoints
|
|
19
|
+
├── docs/ # Documentation (mkdocs)
|
|
20
|
+
└── pyproject.toml # Poetry configuration
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## WHERE TO LOOK
|
|
24
|
+
| Task | Location | Notes |
|
|
25
|
+
|------|----------|-------|
|
|
26
|
+
| Main API | simplug.py lines 1002-1293 | Simplug class - plugin manager |
|
|
27
|
+
| Hook execution | simplug.py lines 379-802 | SimplugHook/SimplugHookAsync |
|
|
28
|
+
| Result collection | simplug.py lines 141-182 | SimplugResult enum (18 strategies) |
|
|
29
|
+
| Plugin registry | simplug.py lines 804-930 | SimplugHooks class |
|
|
30
|
+
| Decorators | simplug.py lines 19-69 | SimplugImpl wrapper |
|
|
31
|
+
| Tests | tests/test_simplug.py | Comprehensive coverage of all features |
|
|
32
|
+
| Entrypoints | examples/complete/plugin/ | Setuptools entrypoint demo |
|
|
33
|
+
|
|
34
|
+
## CONVENTIONS
|
|
35
|
+
- **Decorator-based API**: `@simplug.spec` defines hooks, `@simplug.impl` implements them
|
|
36
|
+
- **Single-file architecture**: All library code in simplug.py (intentional, not legacy)
|
|
37
|
+
- **Priority system**: Plugins sorted by `(priority_attr, batch_index)`, negative = higher priority
|
|
38
|
+
- **Async/sync bridge**: `makecall()` handles both, warnings for sync impl on async spec
|
|
39
|
+
- **OrderedDiot**: Uses diot package for plugin registry (preserves insertion order)
|
|
40
|
+
- **Signature validation**: Spec and impl parameter names must match (excluding defaults)
|
|
41
|
+
|
|
42
|
+
## ANTI-PATTERNS (THIS PROJECT)
|
|
43
|
+
- **Modifying spec parameters**: Examples explicitly warn "don't touch them!" (ingredients)
|
|
44
|
+
- **Version mismatch**: Pre-commit hook enforces version sync between pyproject.toml and simplug.py
|
|
45
|
+
|
|
46
|
+
## UNIQUE STYLES
|
|
47
|
+
- **Singleton per project**: `Simplug('project_name')` returns same instance each time
|
|
48
|
+
- **Plugin name resolution**: Priority: `_name` → `name` → `__name__.lower()` → `__class__.__name__.lower()`
|
|
49
|
+
- **Auto-instantiation**: Classes auto-instantiated if no required constructor params
|
|
50
|
+
- **Bit-flag result types**: SimplugResult uses `0b TRY ALL AVAIL ID` pattern
|
|
51
|
+
|
|
52
|
+
## COMMANDS
|
|
53
|
+
```bash
|
|
54
|
+
# Install
|
|
55
|
+
poetry install
|
|
56
|
+
|
|
57
|
+
# Test
|
|
58
|
+
poetry run pytest tests/
|
|
59
|
+
|
|
60
|
+
# Run examples
|
|
61
|
+
poetry run python examples/toy.py
|
|
62
|
+
poetry run python -m examples.complete
|
|
63
|
+
|
|
64
|
+
# Lint
|
|
65
|
+
flake8 simplug.py
|
|
66
|
+
mypy simplug.py
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## NOTES
|
|
70
|
+
- **No src/ directory**: Poetry single-file layout with `generate-setup-file = true`
|
|
71
|
+
- **Minimal dependencies**: Only external dependency is `diot >= 0.3`
|
|
72
|
+
- **Python 3.9+**: Supports 3.9, 3.10, 3.11, 3.12
|
|
73
|
+
- **Entrypoint testing**: Tests and examples include setup.py files for setuptools entrypoints
|
|
74
|
+
- **CI**: GitHub Actions with matrix testing, pre-commit hooks for quality
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: simplug
|
|
3
|
-
Version: 0.5.
|
|
3
|
+
Version: 0.5.8
|
|
4
4
|
Summary: A simple plugin system for python with async hooks supported
|
|
5
|
-
Author-email: pwwang <pwwang@pwwang.com>
|
|
6
|
-
License: MIT
|
|
7
5
|
Project-URL: Homepage, https://github.com/pwwang/simplug
|
|
8
6
|
Project-URL: Repository, https://github.com/pwwang/simplug
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
Author-email: pwwang <pwwang@pwwang.com>
|
|
8
|
+
License: MIT
|
|
11
9
|
License-File: LICENSE
|
|
10
|
+
Requires-Python: >=3.9
|
|
12
11
|
Requires-Dist: diot>=0.3
|
|
13
|
-
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
14
13
|
|
|
15
14
|
# simplug
|
|
16
15
|
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
# Examples
|
|
2
|
+
|
|
3
|
+
Collection of simplug examples demonstrating various use cases.
|
|
4
|
+
|
|
5
|
+
## Included Examples
|
|
6
|
+
|
|
7
|
+
### Toy Example
|
|
8
|
+
|
|
9
|
+
A minimal 30-line demonstration of basic plugin system usage.
|
|
10
|
+
|
|
11
|
+
**Location:** `examples/toy.py`
|
|
12
|
+
|
|
13
|
+
**Features:**
|
|
14
|
+
- Simple hook specification
|
|
15
|
+
- Multiple plugin implementations
|
|
16
|
+
- Basic result collection
|
|
17
|
+
|
|
18
|
+
**Run:**
|
|
19
|
+
```bash
|
|
20
|
+
python examples/toy.py
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**View source:** [examples/toy.py](https://github.com/pwwang/simplug/blob/main/examples/toy.py)
|
|
24
|
+
|
|
25
|
+
### Complete Example
|
|
26
|
+
|
|
27
|
+
Full example with setuptools entry points integration.
|
|
28
|
+
|
|
29
|
+
**Location:** `examples/complete/`
|
|
30
|
+
|
|
31
|
+
**Features:**
|
|
32
|
+
- Complete application structure
|
|
33
|
+
- Setuptools entry points for plugins
|
|
34
|
+
- Dynamic plugin loading
|
|
35
|
+
|
|
36
|
+
**Run:**
|
|
37
|
+
```bash
|
|
38
|
+
python -m examples.complete
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**Install plugin:**
|
|
42
|
+
```bash
|
|
43
|
+
pip install --editable examples.complete.plugin
|
|
44
|
+
python -m examples.complete
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**View source:**
|
|
48
|
+
- [examples/complete/__main__.py](https://github.com/pwwang/simplug/blob/main/examples/complete/__main__.py)
|
|
49
|
+
- [examples/complete/plugin/](https://github.com/pwwang/simplug/tree/main/examples/complete/plugin)
|
|
50
|
+
|
|
51
|
+
## Common Patterns
|
|
52
|
+
|
|
53
|
+
### Data Pipeline
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from simplug import Simplug, SimplugResult
|
|
57
|
+
|
|
58
|
+
simplug = Simplug('pipeline')
|
|
59
|
+
|
|
60
|
+
# Define pipeline stages
|
|
61
|
+
@simplug.spec(result=SimplugResult.ALL_AVAILS)
|
|
62
|
+
def validate(data):
|
|
63
|
+
pass
|
|
64
|
+
|
|
65
|
+
@simplug.spec(result=SimplugResult.ALL_AVAILS)
|
|
66
|
+
def transform(data):
|
|
67
|
+
pass
|
|
68
|
+
|
|
69
|
+
@simplug.spec(result=SimplugResult.ALL_AVAILS)
|
|
70
|
+
def enrich(data):
|
|
71
|
+
pass
|
|
72
|
+
|
|
73
|
+
# Plugins for each stage
|
|
74
|
+
class SchemaValidator:
|
|
75
|
+
@simplug.impl
|
|
76
|
+
def validate(self, data):
|
|
77
|
+
if not valid_schema(data):
|
|
78
|
+
raise ValueError("Invalid schema")
|
|
79
|
+
return True
|
|
80
|
+
|
|
81
|
+
class Normalizer:
|
|
82
|
+
@simplug.impl
|
|
83
|
+
def transform(self, data):
|
|
84
|
+
return normalize(data)
|
|
85
|
+
|
|
86
|
+
class TimestampEnricher:
|
|
87
|
+
@simplug.impl
|
|
88
|
+
def enrich(self, data):
|
|
89
|
+
data['timestamp'] = datetime.now().isoformat()
|
|
90
|
+
return data
|
|
91
|
+
|
|
92
|
+
# Execute pipeline
|
|
93
|
+
validated = simplug.hooks.validate(raw_data)
|
|
94
|
+
normalized = simplug.hooks.transform(validated)
|
|
95
|
+
enriched = simplug.hooks.enrich(normalized)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Configuration Management
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from simplug import Simplug, SimplugResult
|
|
102
|
+
|
|
103
|
+
simplug = Simplug('configapp')
|
|
104
|
+
|
|
105
|
+
@simplug.spec(result=SimplugResult.FIRST_AVAIL)
|
|
106
|
+
def get_config(key):
|
|
107
|
+
"""Get config from first available source."""
|
|
108
|
+
|
|
109
|
+
# Priority order: env vars -> file -> defaults
|
|
110
|
+
class EnvConfig:
|
|
111
|
+
priority = -1
|
|
112
|
+
|
|
113
|
+
@simplug.impl
|
|
114
|
+
def get_config(self, key):
|
|
115
|
+
return os.environ.get(key)
|
|
116
|
+
|
|
117
|
+
class FileConfig:
|
|
118
|
+
priority = 0
|
|
119
|
+
|
|
120
|
+
@simplug.impl
|
|
121
|
+
def get_config(self, key):
|
|
122
|
+
return read_config_file(key)
|
|
123
|
+
|
|
124
|
+
class DefaultConfig:
|
|
125
|
+
priority = 1
|
|
126
|
+
|
|
127
|
+
@simplug.impl
|
|
128
|
+
def get_config(self, key):
|
|
129
|
+
return DEFAULTS.get(key)
|
|
130
|
+
|
|
131
|
+
# Use with fallback
|
|
132
|
+
timeout = simplug.hooks.get_config('timeout')
|
|
133
|
+
# Returns env var if set, else file, else default
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Event System
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
from simplug import Simplug, SimplugResult
|
|
140
|
+
|
|
141
|
+
simplug = Simplug('events')
|
|
142
|
+
|
|
143
|
+
@simplug.spec(result=SimplugResult.ALL)
|
|
144
|
+
def on_event(event):
|
|
145
|
+
pass
|
|
146
|
+
|
|
147
|
+
# Event subscribers
|
|
148
|
+
class LoggingPlugin:
|
|
149
|
+
@simplug.impl
|
|
150
|
+
def on_event(self, event):
|
|
151
|
+
logger.info(f"Event: {event}")
|
|
152
|
+
|
|
153
|
+
class MetricsPlugin:
|
|
154
|
+
@simplug.impl
|
|
155
|
+
def on_event(self, event):
|
|
156
|
+
metrics.increment(event.type)
|
|
157
|
+
|
|
158
|
+
class WebhookPlugin:
|
|
159
|
+
@simplug.impl
|
|
160
|
+
def on_event(self, event):
|
|
161
|
+
send_webhook(event)
|
|
162
|
+
|
|
163
|
+
# Publish event
|
|
164
|
+
simplug.hooks.on_event({'type': 'user_login', 'user_id': 123})
|
|
165
|
+
# All plugins execute
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Middleware Chain
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
from simplug import Simplug, SimplugResult
|
|
172
|
+
|
|
173
|
+
simplug = Simplug('webapp')
|
|
174
|
+
|
|
175
|
+
@simplug.spec(result=SimplugResult.ALL)
|
|
176
|
+
def handle_request(request):
|
|
177
|
+
pass
|
|
178
|
+
|
|
179
|
+
@simplug.spec(result=SimplugResult.ALL_AVAILS)
|
|
180
|
+
def get_response(request):
|
|
181
|
+
pass
|
|
182
|
+
|
|
183
|
+
class AuthPlugin:
|
|
184
|
+
@simplug.impl
|
|
185
|
+
def handle_request(self, request):
|
|
186
|
+
if not authenticated(request):
|
|
187
|
+
raise UnauthorizedError()
|
|
188
|
+
return request
|
|
189
|
+
|
|
190
|
+
class RateLimitPlugin:
|
|
191
|
+
@simplug.impl
|
|
192
|
+
def handle_request(self, request):
|
|
193
|
+
check_rate_limit(request.user)
|
|
194
|
+
return request
|
|
195
|
+
|
|
196
|
+
class CachingPlugin:
|
|
197
|
+
@simplug.impl
|
|
198
|
+
def get_response(self, request):
|
|
199
|
+
return cache.get(request.url)
|
|
200
|
+
|
|
201
|
+
class ProcessingPlugin:
|
|
202
|
+
@simplug.impl
|
|
203
|
+
def get_response(self, request):
|
|
204
|
+
return process_request(request)
|
|
205
|
+
|
|
206
|
+
# Execute middleware chain
|
|
207
|
+
# All handle_request hooks execute
|
|
208
|
+
responses = simplug.hooks.get_response(request)
|
|
209
|
+
# First non-None response returned
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Async Plugin System
|
|
213
|
+
|
|
214
|
+
```python
|
|
215
|
+
import asyncio
|
|
216
|
+
from simplug import Simplug, SimplugResult
|
|
217
|
+
|
|
218
|
+
simplug = Simplug('asyncapp')
|
|
219
|
+
|
|
220
|
+
@simplug.spec
|
|
221
|
+
async def fetch_data(url):
|
|
222
|
+
pass
|
|
223
|
+
|
|
224
|
+
@simplug.spec(result=SimplugResult.ALL_AVAILS)
|
|
225
|
+
async def process_data(data):
|
|
226
|
+
pass
|
|
227
|
+
|
|
228
|
+
class HttpPlugin:
|
|
229
|
+
@simplug.impl
|
|
230
|
+
async def fetch_data(self, url):
|
|
231
|
+
import aiohttp
|
|
232
|
+
async with aiohttp.ClientSession() as session:
|
|
233
|
+
async with session.get(url) as resp:
|
|
234
|
+
return await resp.text()
|
|
235
|
+
|
|
236
|
+
class TransformPlugin:
|
|
237
|
+
@simplug.impl
|
|
238
|
+
async def process_data(self, data):
|
|
239
|
+
await asyncio.sleep(0.1) # Simulate work
|
|
240
|
+
return data.upper()
|
|
241
|
+
|
|
242
|
+
async def main():
|
|
243
|
+
data = await simplug.hooks.fetch_data('https://api.example.com/data')
|
|
244
|
+
results = await simplug.hooks.process_data(data)
|
|
245
|
+
print(results)
|
|
246
|
+
|
|
247
|
+
asyncio.run(main())
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## Real-World Use Cases
|
|
251
|
+
|
|
252
|
+
### Web Framework Integration
|
|
253
|
+
|
|
254
|
+
Define hooks for request/response processing, authentication, and middleware.
|
|
255
|
+
|
|
256
|
+
### CLI Application
|
|
257
|
+
|
|
258
|
+
Create extensible commands with hooks for argument processing, validation, and output formatting.
|
|
259
|
+
|
|
260
|
+
### Data Processing
|
|
261
|
+
|
|
262
|
+
Build pipelines with validation, transformation, and enrichment stages.
|
|
263
|
+
|
|
264
|
+
### Monitoring and Logging
|
|
265
|
+
|
|
266
|
+
Allow plugins to subscribe to events and implement custom handlers.
|
|
267
|
+
|
|
268
|
+
### Configuration
|
|
269
|
+
|
|
270
|
+
Support multiple configuration sources with priority-based fallback.
|
|
271
|
+
|
|
272
|
+
## Learning Resources
|
|
273
|
+
|
|
274
|
+
- **[Getting Started](getting-started.md)** - Basic concepts
|
|
275
|
+
- **[Guides](guide/defining-hooks.md)** - Feature-specific documentation
|
|
276
|
+
- **[API Reference](api/simplug.md)** - Complete API documentation
|
|
277
|
+
- **[Source Code](https://github.com/pwwang/simplug)** - Browse implementation
|
|
278
|
+
|
|
279
|
+
## Creating Your Own Examples
|
|
280
|
+
|
|
281
|
+
When creating examples:
|
|
282
|
+
|
|
283
|
+
1. **Keep them simple** - Focus on one feature per example
|
|
284
|
+
2. **Add comments** - Explain the key concepts demonstrated
|
|
285
|
+
3. **Make them runnable** - Users should be able to execute immediately
|
|
286
|
+
4. **Include expected output** - Show what to expect
|
|
287
|
+
5. **Document dependencies** - List required packages
|
|
288
|
+
|
|
289
|
+
## Contributing Examples
|
|
290
|
+
|
|
291
|
+
Have an example that would help others? Consider contributing!
|
|
292
|
+
|
|
293
|
+
- Fork the repository
|
|
294
|
+
- Add your example to `examples/`
|
|
295
|
+
- Update this documentation
|
|
296
|
+
- Submit a pull request
|