strata-python 0.2.0__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.
- strata_python-0.2.0/.gitignore +220 -0
- strata_python-0.2.0/LICENSE +21 -0
- strata_python-0.2.0/PKG-INFO +82 -0
- strata_python-0.2.0/README.md +54 -0
- strata_python-0.2.0/pyproject.toml +101 -0
- strata_python-0.2.0/src/strata_python/__init__.py +19 -0
- strata_python-0.2.0/src/strata_python/check.py +68 -0
- strata_python-0.2.0/src/strata_python/cli.py +71 -0
- strata_python-0.2.0/src/strata_python/http_server.py +119 -0
- strata_python-0.2.0/src/strata_python/live.py +30 -0
- strata_python-0.2.0/src/strata_python/oslib.py +117 -0
- strata_python-0.2.0/src/strata_python/public_check.py +133 -0
- strata_python-0.2.0/src/strata_python/py.typed +0 -0
- strata_python-0.2.0/src/strata_python/pytest_plugin.py +80 -0
- strata_python-0.2.0/src/strata_python/settings.py +132 -0
- strata_python-0.2.0/src/strata_python/web.py +98 -0
- strata_python-0.2.0/tests/conftest.py +37 -0
- strata_python-0.2.0/tests/integration/test_serve.py +42 -0
- strata_python-0.2.0/tests/integration/test_strata_check.py +39 -0
- strata_python-0.2.0/tests/unit/test_check.py +48 -0
- strata_python-0.2.0/tests/unit/test_cli.py +64 -0
- strata_python-0.2.0/tests/unit/test_http_server.py +55 -0
- strata_python-0.2.0/tests/unit/test_live.py +22 -0
- strata_python-0.2.0/tests/unit/test_oslib.py +80 -0
- strata_python-0.2.0/tests/unit/test_package.py +29 -0
- strata_python-0.2.0/tests/unit/test_plugin.py +93 -0
- strata_python-0.2.0/tests/unit/test_public_check.py +92 -0
- strata_python-0.2.0/tests/unit/test_settings.py +125 -0
- strata_python-0.2.0/tests/unit/test_web.py +126 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
*.lcov
|
|
51
|
+
.hypothesis/
|
|
52
|
+
.pytest_cache/
|
|
53
|
+
cover/
|
|
54
|
+
|
|
55
|
+
# Translations
|
|
56
|
+
*.mo
|
|
57
|
+
*.pot
|
|
58
|
+
|
|
59
|
+
# Django stuff:
|
|
60
|
+
*.log
|
|
61
|
+
local_settings.py
|
|
62
|
+
db.sqlite3
|
|
63
|
+
db.sqlite3-journal
|
|
64
|
+
|
|
65
|
+
# Flask stuff:
|
|
66
|
+
instance/
|
|
67
|
+
.webassets-cache
|
|
68
|
+
|
|
69
|
+
# Scrapy stuff:
|
|
70
|
+
.scrapy
|
|
71
|
+
|
|
72
|
+
# Sphinx documentation
|
|
73
|
+
docs/_build/
|
|
74
|
+
|
|
75
|
+
# PyBuilder
|
|
76
|
+
.pybuilder/
|
|
77
|
+
target/
|
|
78
|
+
|
|
79
|
+
# Jupyter Notebook
|
|
80
|
+
.ipynb_checkpoints
|
|
81
|
+
|
|
82
|
+
# IPython
|
|
83
|
+
profile_default/
|
|
84
|
+
ipython_config.py
|
|
85
|
+
|
|
86
|
+
# pyenv
|
|
87
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
88
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
89
|
+
# .python-version
|
|
90
|
+
|
|
91
|
+
# pipenv
|
|
92
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
93
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
94
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
95
|
+
# install all needed dependencies.
|
|
96
|
+
# Pipfile.lock
|
|
97
|
+
|
|
98
|
+
# uv
|
|
99
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
100
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
101
|
+
# commonly ignored for libraries.
|
|
102
|
+
# uv.lock
|
|
103
|
+
|
|
104
|
+
# poetry
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
106
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
107
|
+
# commonly ignored for libraries.
|
|
108
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
109
|
+
# poetry.lock
|
|
110
|
+
# poetry.toml
|
|
111
|
+
|
|
112
|
+
# pdm
|
|
113
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
114
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
115
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
116
|
+
# pdm.lock
|
|
117
|
+
# pdm.toml
|
|
118
|
+
.pdm-python
|
|
119
|
+
.pdm-build/
|
|
120
|
+
|
|
121
|
+
# pixi
|
|
122
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
123
|
+
# pixi.lock
|
|
124
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
125
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
126
|
+
.pixi/*
|
|
127
|
+
!.pixi/config.toml
|
|
128
|
+
|
|
129
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
130
|
+
__pypackages__/
|
|
131
|
+
|
|
132
|
+
# Celery stuff
|
|
133
|
+
celerybeat-schedule*
|
|
134
|
+
celerybeat.pid
|
|
135
|
+
|
|
136
|
+
# Redis
|
|
137
|
+
*.rdb
|
|
138
|
+
*.aof
|
|
139
|
+
*.pid
|
|
140
|
+
|
|
141
|
+
# RabbitMQ
|
|
142
|
+
mnesia/
|
|
143
|
+
rabbitmq/
|
|
144
|
+
rabbitmq-data/
|
|
145
|
+
|
|
146
|
+
# ActiveMQ
|
|
147
|
+
activemq-data/
|
|
148
|
+
|
|
149
|
+
# SageMath parsed files
|
|
150
|
+
*.sage.py
|
|
151
|
+
|
|
152
|
+
# Environments
|
|
153
|
+
.env
|
|
154
|
+
.envrc
|
|
155
|
+
.venv
|
|
156
|
+
env/
|
|
157
|
+
venv/
|
|
158
|
+
ENV/
|
|
159
|
+
env.bak/
|
|
160
|
+
venv.bak/
|
|
161
|
+
|
|
162
|
+
# Spyder project settings
|
|
163
|
+
.spyderproject
|
|
164
|
+
.spyproject
|
|
165
|
+
|
|
166
|
+
# Rope project settings
|
|
167
|
+
.ropeproject
|
|
168
|
+
|
|
169
|
+
# mkdocs/Zensical documentation
|
|
170
|
+
/site
|
|
171
|
+
|
|
172
|
+
# mypy
|
|
173
|
+
.mypy_cache/
|
|
174
|
+
.dmypy.json
|
|
175
|
+
dmypy.json
|
|
176
|
+
|
|
177
|
+
# Pyre type checker
|
|
178
|
+
.pyre/
|
|
179
|
+
|
|
180
|
+
# pytype static type analyzer
|
|
181
|
+
.pytype/
|
|
182
|
+
|
|
183
|
+
# Cython debug symbols
|
|
184
|
+
cython_debug/
|
|
185
|
+
|
|
186
|
+
# PyCharm
|
|
187
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
188
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
189
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
190
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
191
|
+
# .idea/
|
|
192
|
+
|
|
193
|
+
# Abstra
|
|
194
|
+
# Abstra is an AI-powered process automation framework.
|
|
195
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
196
|
+
# Learn more at https://abstra.io/docs
|
|
197
|
+
.abstra/
|
|
198
|
+
|
|
199
|
+
# Visual Studio Code
|
|
200
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore that
|
|
201
|
+
# can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
202
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer, you
|
|
203
|
+
# could uncomment the following to ignore the entire vscode folder
|
|
204
|
+
# .vscode/
|
|
205
|
+
# Temporary file for partial code execution
|
|
206
|
+
tempCodeRunnerFile.py
|
|
207
|
+
|
|
208
|
+
# Ruff stuff:
|
|
209
|
+
.ruff_cache/
|
|
210
|
+
|
|
211
|
+
# PyPI configuration file
|
|
212
|
+
.pypirc
|
|
213
|
+
|
|
214
|
+
# Marimo
|
|
215
|
+
marimo/_static/
|
|
216
|
+
marimo/_lsp/
|
|
217
|
+
__marimo__/
|
|
218
|
+
|
|
219
|
+
# Streamlit
|
|
220
|
+
.streamlit/secrets.toml
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 jburnhams
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: strata-python
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Common code for Python web server apps: OS helpers, settings, FastAPI serving, CLI commands, test fixtures and the strata-check gate
|
|
5
|
+
Project-URL: Homepage, https://github.com/jburnhams/strata-python
|
|
6
|
+
Project-URL: Source, https://github.com/jburnhams/strata-python
|
|
7
|
+
Project-URL: Changelog, https://github.com/jburnhams/strata-python/blob/main/CHANGELOG.md
|
|
8
|
+
Author: jburnhams
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: cli,fastapi,pytest,settings
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Framework :: FastAPI
|
|
14
|
+
Classifier: Framework :: Pytest
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.11
|
|
22
|
+
Requires-Dist: fastapi>=0.115
|
|
23
|
+
Requires-Dist: pydantic>=2.9
|
|
24
|
+
Requires-Dist: pytest>=8
|
|
25
|
+
Requires-Dist: tzdata; sys_platform == 'win32'
|
|
26
|
+
Requires-Dist: uvicorn>=0.32
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# strata-python
|
|
30
|
+
|
|
31
|
+
Common code for the strata Python web server apps (FastAPI + CLI), the Python counterpart of [strata-react](https://github.com/jburnhams/strata-react). Every app made from [python-app-template](https://github.com/jburnhams/python-app-template) used to carry its own drifting copy of these modules; they live here once. It is opinionated and built for those apps rather than as a general-purpose library, but it is public, MIT-licensed and on PyPI.
|
|
32
|
+
|
|
33
|
+
| Module | What it gives an app |
|
|
34
|
+
|---|---|
|
|
35
|
+
| `strata_python.oslib` | Cross-platform file locks, process liveness (`pid_alive`), `kill_tree`, `lower_priority`, `venv_python`, `cli_command(module)` |
|
|
36
|
+
| `strata_python.settings` | `Settings` / `Setting`: one JSON file in `~/.<app>` with `<APP>_*` environment overrides, validation (`absolute_path`, `non_empty`), atomic saves |
|
|
37
|
+
| `strata_python.web` | `base_app` (health + settings routes), `add_web` (built web app or stand-in page at every non-API path), `with_base` (reverse-proxy-safe `<base href>`), `static_file` (path-safe), `serve` |
|
|
38
|
+
| `strata_python.cli` | `add_doctor`, `add_set`, `add_serve` for the app's argparse subcommands |
|
|
39
|
+
| `strata_python.http_server` / `live` | A real scripted HTTP server and a real uvicorn server on loopback, for tests that make actual requests |
|
|
40
|
+
| pytest plugin | Loaded automatically: hermetic home and data folders, a no-network guard, `http_server` and `live_app` fixtures |
|
|
41
|
+
| `strata-check` | The one gate: lockfile, ruff, `ruff format`, pyright strict, ty, unit tests with coverage |
|
|
42
|
+
| `strata-public-check` | Fails on email addresses, private IPv4 addresses, home-folder paths and the regexes in `STRATA_PUBLIC_DENY` in a tree, sdist or wheel; run before anything public is published |
|
|
43
|
+
|
|
44
|
+
## Use it in an app
|
|
45
|
+
|
|
46
|
+
```toml
|
|
47
|
+
# pyproject.toml
|
|
48
|
+
[project]
|
|
49
|
+
dependencies = ["strata-python>=0.2,<0.3"] # from PyPI; while 0.x a minor release may break, so cap below the next one
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
strata_env_prefix = "MYAPP" # MYAPP_HOME and MYAPP_DATA point into a tmp dir for every test
|
|
53
|
+
strata_tuning_vars = ["MYAPP_GREETING"] # removed from the environment for every test
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from strata_python import cli, web
|
|
58
|
+
from strata_python.settings import Setting, Settings, absolute_path, non_empty
|
|
59
|
+
|
|
60
|
+
SETTINGS = Settings(
|
|
61
|
+
"myapp",
|
|
62
|
+
[
|
|
63
|
+
Setting("data_dir", default=lambda: "/srv/myapp", normalise=absolute_path),
|
|
64
|
+
Setting("greeting", default="Hello", normalise=non_empty),
|
|
65
|
+
],
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def create_app():
|
|
70
|
+
app = web.base_app("myapp", __version__, SETTINGS) # /api/health, GET/PUT /api/settings
|
|
71
|
+
# ... the app's own /api routes ...
|
|
72
|
+
web.add_web(app, STATIC_DIR, LANDING_HTML) # last: it registers the catch-all page route
|
|
73
|
+
return app
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
To try an unreleased commit, add `[tool.uv.sources] strata-python = { git = "https://github.com/jburnhams/strata-python", rev = "<sha>" }` for the moment and remove it once the release is out.
|
|
77
|
+
|
|
78
|
+
`scripts/check` in the app is `exec uv run --locked strata-check "$@"`; git hooks and CI call it. Optional `[tool.strata-check]` in `pyproject.toml` sets `paths` (default `src`, `tests`, `scripts`) and `tests` (default `tests/unit`).
|
|
79
|
+
|
|
80
|
+
## Develop
|
|
81
|
+
|
|
82
|
+
`uv sync`, then `scripts/check` (add `--fast` to skip the tests, `--fix` for safe ruff fixes). Integration tests (`uv run pytest tests/integration`) run the gate on a tiny project and `serve` in a separate process. Read [AGENTS.md](AGENTS.md) before changing anything; every change gets a [CHANGELOG.md](CHANGELOG.md) entry. Merging to main releases: `release.yml` bumps the version through its own PR, tags it and creates the GitHub release, and `pypi-publish.yml` uploads it to PyPI.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# strata-python
|
|
2
|
+
|
|
3
|
+
Common code for the strata Python web server apps (FastAPI + CLI), the Python counterpart of [strata-react](https://github.com/jburnhams/strata-react). Every app made from [python-app-template](https://github.com/jburnhams/python-app-template) used to carry its own drifting copy of these modules; they live here once. It is opinionated and built for those apps rather than as a general-purpose library, but it is public, MIT-licensed and on PyPI.
|
|
4
|
+
|
|
5
|
+
| Module | What it gives an app |
|
|
6
|
+
|---|---|
|
|
7
|
+
| `strata_python.oslib` | Cross-platform file locks, process liveness (`pid_alive`), `kill_tree`, `lower_priority`, `venv_python`, `cli_command(module)` |
|
|
8
|
+
| `strata_python.settings` | `Settings` / `Setting`: one JSON file in `~/.<app>` with `<APP>_*` environment overrides, validation (`absolute_path`, `non_empty`), atomic saves |
|
|
9
|
+
| `strata_python.web` | `base_app` (health + settings routes), `add_web` (built web app or stand-in page at every non-API path), `with_base` (reverse-proxy-safe `<base href>`), `static_file` (path-safe), `serve` |
|
|
10
|
+
| `strata_python.cli` | `add_doctor`, `add_set`, `add_serve` for the app's argparse subcommands |
|
|
11
|
+
| `strata_python.http_server` / `live` | A real scripted HTTP server and a real uvicorn server on loopback, for tests that make actual requests |
|
|
12
|
+
| pytest plugin | Loaded automatically: hermetic home and data folders, a no-network guard, `http_server` and `live_app` fixtures |
|
|
13
|
+
| `strata-check` | The one gate: lockfile, ruff, `ruff format`, pyright strict, ty, unit tests with coverage |
|
|
14
|
+
| `strata-public-check` | Fails on email addresses, private IPv4 addresses, home-folder paths and the regexes in `STRATA_PUBLIC_DENY` in a tree, sdist or wheel; run before anything public is published |
|
|
15
|
+
|
|
16
|
+
## Use it in an app
|
|
17
|
+
|
|
18
|
+
```toml
|
|
19
|
+
# pyproject.toml
|
|
20
|
+
[project]
|
|
21
|
+
dependencies = ["strata-python>=0.2,<0.3"] # from PyPI; while 0.x a minor release may break, so cap below the next one
|
|
22
|
+
|
|
23
|
+
[tool.pytest.ini_options]
|
|
24
|
+
strata_env_prefix = "MYAPP" # MYAPP_HOME and MYAPP_DATA point into a tmp dir for every test
|
|
25
|
+
strata_tuning_vars = ["MYAPP_GREETING"] # removed from the environment for every test
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from strata_python import cli, web
|
|
30
|
+
from strata_python.settings import Setting, Settings, absolute_path, non_empty
|
|
31
|
+
|
|
32
|
+
SETTINGS = Settings(
|
|
33
|
+
"myapp",
|
|
34
|
+
[
|
|
35
|
+
Setting("data_dir", default=lambda: "/srv/myapp", normalise=absolute_path),
|
|
36
|
+
Setting("greeting", default="Hello", normalise=non_empty),
|
|
37
|
+
],
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def create_app():
|
|
42
|
+
app = web.base_app("myapp", __version__, SETTINGS) # /api/health, GET/PUT /api/settings
|
|
43
|
+
# ... the app's own /api routes ...
|
|
44
|
+
web.add_web(app, STATIC_DIR, LANDING_HTML) # last: it registers the catch-all page route
|
|
45
|
+
return app
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
To try an unreleased commit, add `[tool.uv.sources] strata-python = { git = "https://github.com/jburnhams/strata-python", rev = "<sha>" }` for the moment and remove it once the release is out.
|
|
49
|
+
|
|
50
|
+
`scripts/check` in the app is `exec uv run --locked strata-check "$@"`; git hooks and CI call it. Optional `[tool.strata-check]` in `pyproject.toml` sets `paths` (default `src`, `tests`, `scripts`) and `tests` (default `tests/unit`).
|
|
51
|
+
|
|
52
|
+
## Develop
|
|
53
|
+
|
|
54
|
+
`uv sync`, then `scripts/check` (add `--fast` to skip the tests, `--fix` for safe ruff fixes). Integration tests (`uv run pytest tests/integration`) run the gate on a tiny project and `serve` in a separate process. Read [AGENTS.md](AGENTS.md) before changing anything; every change gets a [CHANGELOG.md](CHANGELOG.md) entry. Merging to main releases: `release.yml` bumps the version through its own PR, tags it and creates the GitHub release, and `pypi-publish.yml` uploads it to PyPI.
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27"] # SPDX `license` and `license-files`
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "strata-python"
|
|
7
|
+
version = "0.2.0" # the single source of truth: `strata_python.__version__` reads it back with importlib.metadata
|
|
8
|
+
description = "Common code for Python web server apps: OS helpers, settings, FastAPI serving, CLI commands, test fixtures and the strata-check gate"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "jburnhams" }] # a handle, not an email: this is shown on PyPI
|
|
14
|
+
keywords = ["fastapi", "pytest", "settings", "cli"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.11",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Operating System :: OS Independent",
|
|
22
|
+
"Framework :: FastAPI",
|
|
23
|
+
"Framework :: Pytest",
|
|
24
|
+
"Typing :: Typed",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"fastapi>=0.115",
|
|
28
|
+
"uvicorn>=0.32",
|
|
29
|
+
"pydantic>=2.9",
|
|
30
|
+
"pytest>=8", # the plugin and fixtures ship with the library; apps have pytest anyway
|
|
31
|
+
"tzdata; sys_platform == 'win32'", # IANA timezone database, needed on Windows for zoneinfo
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Homepage = "https://github.com/jburnhams/strata-python"
|
|
36
|
+
Source = "https://github.com/jburnhams/strata-python"
|
|
37
|
+
Changelog = "https://github.com/jburnhams/strata-python/blob/main/CHANGELOG.md"
|
|
38
|
+
|
|
39
|
+
[project.scripts]
|
|
40
|
+
strata-check = "strata_python.check:main"
|
|
41
|
+
strata-public-check = "strata_python.public_check:main"
|
|
42
|
+
|
|
43
|
+
[project.entry-points.pytest11]
|
|
44
|
+
strata_python = "strata_python.pytest_plugin"
|
|
45
|
+
|
|
46
|
+
[dependency-groups]
|
|
47
|
+
dev = [
|
|
48
|
+
"httpx>=0.27",
|
|
49
|
+
"pytest-cov>=5",
|
|
50
|
+
"pytest-randomly",
|
|
51
|
+
"pytest-timeout",
|
|
52
|
+
"pytest-xdist",
|
|
53
|
+
"ruff",
|
|
54
|
+
"pyright",
|
|
55
|
+
"ty",
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
[tool.hatch.build.targets.wheel]
|
|
59
|
+
packages = ["src/strata_python"]
|
|
60
|
+
|
|
61
|
+
# Hatch puts every tracked file in the sdist by default (agent notes, CI, the lockfile); a PyPI upload is permanent,
|
|
62
|
+
# so the sdist carries only what builds and tests the package.
|
|
63
|
+
[tool.hatch.build.targets.sdist]
|
|
64
|
+
include = ["src", "tests", "README.md", "LICENSE", "pyproject.toml"]
|
|
65
|
+
|
|
66
|
+
[tool.pytest.ini_options]
|
|
67
|
+
testpaths = ["tests/unit"]
|
|
68
|
+
# The plugin is loaded by tests/conftest.py, not its entry point: pytest imports entry-point plugins before coverage starts, which would hide their lines from the report
|
|
69
|
+
addopts = "-ra --timeout=300 --strict-markers --strict-config -p pytester -p no:strata_python"
|
|
70
|
+
strata_env_prefix = "DEMO"
|
|
71
|
+
strata_tuning_vars = ["DEMO_TUNE"]
|
|
72
|
+
markers = ["integration: runs a real server or subprocess end-to-end (tests/integration)"]
|
|
73
|
+
|
|
74
|
+
[tool.coverage.run]
|
|
75
|
+
source = ["src/strata_python"]
|
|
76
|
+
branch = true
|
|
77
|
+
|
|
78
|
+
[tool.coverage.report]
|
|
79
|
+
skip_empty = true
|
|
80
|
+
# The Windows branches of oslib run only on the Windows CI leg, so they do not count towards the number measured elsewhere
|
|
81
|
+
exclude_also = ["if __name__ == .__main__.:", "if WIN:"]
|
|
82
|
+
fail_under = 95
|
|
83
|
+
|
|
84
|
+
[tool.ruff]
|
|
85
|
+
line-length = 120
|
|
86
|
+
target-version = "py311"
|
|
87
|
+
|
|
88
|
+
[tool.ruff.lint]
|
|
89
|
+
select = ["E", "F", "I", "B", "UP", "SIM", "RUF", "C4", "PT", "S", "TID"]
|
|
90
|
+
ignore = ["E501"] # the formatter wraps code; long comments and docstrings stay (the house style comments the why)
|
|
91
|
+
|
|
92
|
+
[tool.ruff.lint.flake8-tidy-imports.banned-api]
|
|
93
|
+
"pytest.importorskip".msg = "Do not skip when a dependency is missing: install it (locally and in CI) and let the test fail if it is not there. A deliberate skip is a marker with a reason, see AGENTS.md (Testing)."
|
|
94
|
+
|
|
95
|
+
[tool.ruff.lint.per-file-ignores]
|
|
96
|
+
"tests/**" = ["S101", "S310", "S104", "S105", "S106", "S108", "S603", "S607"] # asserts, urlopen of our own loopback servers, fake secrets, tmp paths, subprocess of our own code
|
|
97
|
+
|
|
98
|
+
[tool.pyright]
|
|
99
|
+
include = ["src"]
|
|
100
|
+
pythonVersion = "3.11"
|
|
101
|
+
typeCheckingMode = "strict"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Common code for the strata Python web server apps (FastAPI + CLI): the parts every app was copying.
|
|
2
|
+
|
|
3
|
+
oslib file locks, process liveness and termination, priority, how to start a CLI (POSIX and Windows)
|
|
4
|
+
settings one JSON settings file per app with environment overrides and validation
|
|
5
|
+
web reverse-proxy-safe page serving, health and settings routes, static files, the uvicorn runner
|
|
6
|
+
cli argparse commands every app has: doctor, set, serve
|
|
7
|
+
http_server a real scripted HTTP server on loopback for tests of outbound requests
|
|
8
|
+
live run a FastAPI app on a real loopback port for tests
|
|
9
|
+
pytest plugin (automatic once installed): hermetic environment, no-network guard, `http_server` and `live_app` fixtures
|
|
10
|
+
check `strata-check`, the one gate: lockfile, ruff, ruff format, pyright, ty, unit tests with coverage
|
|
11
|
+
public_check `strata-public-check`: no email addresses, private addresses, home paths or owner-listed names in what is published
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
__version__ = version("strata-python")
|
|
18
|
+
except PackageNotFoundError: # running from a source tree that was never installed
|
|
19
|
+
__version__ = "0+unknown"
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""`strata-check`: the one gate for an app, run before every commit by hand, by the git hooks and by CI (so they cannot disagree).
|
|
2
|
+
|
|
3
|
+
uv run strata-check # lockfile, ruff lint, ruff format, pyright, ty, then the unit tests with coverage
|
|
4
|
+
uv run strata-check --fast # skip the tests (for a commit hook)
|
|
5
|
+
uv run strata-check --fix # apply safe ruff fixes and formatting first
|
|
6
|
+
|
|
7
|
+
Two type checkers gate: pyright (strict) and ty; when ty is stable and as strict as pyright's strict mode, drop the pyright step. `--fix` never uses ruff's unsafe fixes: they
|
|
8
|
+
rewrite code in ways tests have to catch (see the README). Run it from the project root. Optional configuration in pyproject.toml:
|
|
9
|
+
|
|
10
|
+
[tool.strata-check]
|
|
11
|
+
paths = ["src", "tests", "scripts"] # what ruff checks (only folders that exist are passed); ty checks the first one
|
|
12
|
+
tests = "tests/unit" # what the coverage run covers
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import os
|
|
16
|
+
import subprocess
|
|
17
|
+
import sys
|
|
18
|
+
import tomllib
|
|
19
|
+
from collections.abc import Sequence
|
|
20
|
+
from typing import Any
|
|
21
|
+
|
|
22
|
+
DEFAULT_PATHS = ("src", "tests", "scripts")
|
|
23
|
+
DEFAULT_TESTS = "tests/unit"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def config(root: str = ".") -> dict[str, Any]:
|
|
27
|
+
try:
|
|
28
|
+
with open(os.path.join(root, "pyproject.toml"), "rb") as f:
|
|
29
|
+
return tomllib.load(f).get("tool", {}).get("strata-check", {})
|
|
30
|
+
except FileNotFoundError:
|
|
31
|
+
return {}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def steps(root: str = ".", *, tests: bool = True) -> list[tuple[str, list[str]]]:
|
|
35
|
+
cfg = config(root)
|
|
36
|
+
paths = [p for p in cfg.get("paths", DEFAULT_PATHS) if os.path.isdir(os.path.join(root, p))]
|
|
37
|
+
out: list[tuple[str, list[str]]] = []
|
|
38
|
+
if os.path.exists(os.path.join(root, "uv.lock")):
|
|
39
|
+
out.append(("lockfile is current", ["uv", "lock", "--check"]))
|
|
40
|
+
out += [
|
|
41
|
+
("ruff lint", ["ruff", "check", *paths]),
|
|
42
|
+
("ruff format", ["ruff", "format", "--check", *paths]),
|
|
43
|
+
("pyright (strict)", ["pyright"]),
|
|
44
|
+
("ty", ["ty", "check", paths[0] if paths else "."]),
|
|
45
|
+
]
|
|
46
|
+
if tests:
|
|
47
|
+
out.append(("unit tests + coverage", ["pytest", cfg.get("tests", DEFAULT_TESTS), "--cov", "-q"]))
|
|
48
|
+
return out
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def run(label: str, cmd: Sequence[str]) -> bool:
|
|
52
|
+
print(f"\n==> {label}: {' '.join(cmd)}", flush=True)
|
|
53
|
+
return subprocess.run(cmd).returncode == 0 # noqa: S603 (fixed commands from this file)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
57
|
+
args = list(sys.argv[1:] if argv is None else argv)
|
|
58
|
+
if "--fix" in args:
|
|
59
|
+
paths = [p for p in config().get("paths", DEFAULT_PATHS) if os.path.isdir(p)]
|
|
60
|
+
run("ruff fix (safe fixes only)", ["ruff", "check", "--fix", *paths])
|
|
61
|
+
run("ruff format", ["ruff", "format", *paths])
|
|
62
|
+
failed = [label for label, cmd in steps(tests="--fast" not in args) if not run(label, cmd)]
|
|
63
|
+
print("\n" + ("FAILED: " + ", ".join(failed) if failed else "all checks passed"))
|
|
64
|
+
return 1 if failed else 0
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
if __name__ == "__main__":
|
|
68
|
+
sys.exit(main())
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""The CLI commands every app has, added to the app's own argparse subparsers.
|
|
2
|
+
|
|
3
|
+
sub = parser.add_subparsers(dest="cmd", required=True)
|
|
4
|
+
add_doctor(sub, "myapp", __version__, ("fastapi", "uvicorn"))
|
|
5
|
+
add_set(sub, SETTINGS)
|
|
6
|
+
add_serve(sub, lambda host, port: web.serve(create_app(), ["--host", host, "--port", str(port)]), default_port=8360)
|
|
7
|
+
args = parser.parse_args(argv); return args.fn(args)
|
|
8
|
+
|
|
9
|
+
Each command sets `fn` on its parser; the handler returns the exit code."""
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import importlib
|
|
13
|
+
import sys
|
|
14
|
+
from collections.abc import Callable, Iterable
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
from strata_python.settings import Settings
|
|
18
|
+
|
|
19
|
+
Sub = Any # argparse's subparsers object is a private generic type; only add_parser is used
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def add_doctor(sub: Sub, name: str, version: str, imports: Iterable[str]) -> None:
|
|
23
|
+
"""`doctor`: print the app and Python versions and whether each module in `imports` can be imported (exit 1 if any cannot)."""
|
|
24
|
+
mods = tuple(imports)
|
|
25
|
+
|
|
26
|
+
def doctor(_args: argparse.Namespace) -> int:
|
|
27
|
+
ok = True
|
|
28
|
+
print(f"{name} {version}, python {sys.version.split()[0]}")
|
|
29
|
+
for mod in mods:
|
|
30
|
+
try:
|
|
31
|
+
m = importlib.import_module(mod)
|
|
32
|
+
print(f" ok {mod} {getattr(m, '__version__', '')}".rstrip())
|
|
33
|
+
except ImportError:
|
|
34
|
+
ok = False
|
|
35
|
+
print(f" MISSING {mod}: uv sync")
|
|
36
|
+
return 0 if ok else 1
|
|
37
|
+
|
|
38
|
+
sub.add_parser("doctor", help="check the environment").set_defaults(fn=doctor)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def add_set(sub: Sub, settings: Settings) -> None:
|
|
42
|
+
"""`set KEY [VALUE]`: show a setting, change it, or reset it with the value `default`. A bad key or value exits with the message."""
|
|
43
|
+
|
|
44
|
+
def cmd_set(args: argparse.Namespace) -> int:
|
|
45
|
+
if args.key not in settings.keys:
|
|
46
|
+
sys.exit(f"unknown setting {args.key!r} (known: {', '.join(settings.keys)})")
|
|
47
|
+
if args.value is not None:
|
|
48
|
+
try:
|
|
49
|
+
settings.save({args.key: None if args.value == "default" else args.value})
|
|
50
|
+
except ValueError as e:
|
|
51
|
+
sys.exit(str(e))
|
|
52
|
+
print(f"{args.key} = {settings.get(args.key)}")
|
|
53
|
+
return 0
|
|
54
|
+
|
|
55
|
+
p = sub.add_parser("set", help=f'show or change a setting ({", ".join(settings.keys)}); value "default" resets it')
|
|
56
|
+
p.add_argument("key")
|
|
57
|
+
p.add_argument("value", nargs="?")
|
|
58
|
+
p.set_defaults(fn=cmd_set)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def add_serve(sub: Sub, run: Callable[[str, int], None], *, default_port: int) -> None:
|
|
62
|
+
"""`serve [--host] [--port]`: call `run(host, port)`."""
|
|
63
|
+
|
|
64
|
+
def cmd_serve(args: argparse.Namespace) -> int:
|
|
65
|
+
run(args.host, args.port)
|
|
66
|
+
return 0
|
|
67
|
+
|
|
68
|
+
p = sub.add_parser("serve", help="start the web server")
|
|
69
|
+
p.add_argument("--host", default="127.0.0.1")
|
|
70
|
+
p.add_argument("--port", type=int, default=default_port)
|
|
71
|
+
p.set_defaults(fn=cmd_serve)
|