strata-python 0.2.0__py3-none-any.whl → 0.2.2__py3-none-any.whl

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.
Files changed (36) hide show
  1. strata_python/__init__.py +2 -1
  2. strata_python/cli.py +99 -20
  3. strata_python/clients/__init__.py +2 -0
  4. strata_python/clients/models.py +780 -0
  5. strata_python/db/__init__.py +116 -0
  6. strata_python/db/__main__.py +5 -0
  7. strata_python/db/_alembic/env.py +24 -0
  8. strata_python/db/_alembic/script.py.mako +28 -0
  9. strata_python/db/cli.py +199 -0
  10. strata_python/db/config.py +85 -0
  11. strata_python/db/engine.py +53 -0
  12. strata_python/db/guard.py +159 -0
  13. strata_python/db/migrations.py +243 -0
  14. strata_python/db/models.py +62 -0
  15. strata_python/db/project.py +98 -0
  16. strata_python/db/pytest_plugin.py +205 -0
  17. strata_python/db/scratch.py +143 -0
  18. strata_python/db/settings.py +63 -0
  19. strata_python/db/testing.py +43 -0
  20. strata_python/db/testserver.py +205 -0
  21. strata_python/oslib.py +44 -5
  22. strata_python/pages/__init__.py +21 -0
  23. strata_python/pages/model.py +241 -0
  24. strata_python/pages/render.py +273 -0
  25. strata_python/public_check.py +54 -8
  26. strata_python/pytest_plugin.py +70 -1
  27. strata_python/settings.py +243 -42
  28. strata_python/testing.py +257 -0
  29. strata_python/web.py +15 -6
  30. strata_python-0.2.2.dist-info/METADATA +334 -0
  31. strata_python-0.2.2.dist-info/RECORD +38 -0
  32. {strata_python-0.2.0.dist-info → strata_python-0.2.2.dist-info}/entry_points.txt +2 -0
  33. strata_python-0.2.0.dist-info/METADATA +0 -82
  34. strata_python-0.2.0.dist-info/RECORD +0 -16
  35. {strata_python-0.2.0.dist-info → strata_python-0.2.2.dist-info}/WHEEL +0 -0
  36. {strata_python-0.2.0.dist-info → strata_python-0.2.2.dist-info}/licenses/LICENSE +0 -0
strata_python/__init__.py CHANGED
@@ -6,7 +6,8 @@ web reverse-proxy-safe page serving, health and settings routes, static
6
6
  cli argparse commands every app has: doctor, set, serve
7
7
  http_server a real scripted HTTP server on loopback for tests of outbound requests
8
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
9
+ testing the fakes for HTTP, subprocesses and sleep, and CLI runners, shared by every app's tests
10
+ pytest plugin (automatic once installed): hermetic environment, no-network guard, `http_server`, `live_app` and the fake_* fixtures
10
11
  check `strata-check`, the one gate: lockfile, ruff, ruff format, pyright, ty, unit tests with coverage
11
12
  public_check `strata-public-check`: no email addresses, private addresses, home paths or owner-listed names in what is published
12
13
  """
strata_python/cli.py CHANGED
@@ -1,7 +1,8 @@
1
1
  """The CLI commands every app has, added to the app's own argparse subparsers.
2
2
 
3
3
  sub = parser.add_subparsers(dest="cmd", required=True)
4
- add_doctor(sub, "myapp", __version__, ("fastapi", "uvicorn"))
4
+ add_doctor(sub, "myapp", __version__, ("fastapi", ("opencv", "cv2"), Module("ultralytics", optional=True)),
5
+ checks=[Check("ffmpeg", program("ffmpeg"))])
5
6
  add_set(sub, SETTINGS)
6
7
  add_serve(sub, lambda host, port: web.serve(create_app(), ["--host", host, "--port", str(port)]), default_port=8360)
7
8
  args = parser.parse_args(argv); return args.fn(args)
@@ -10,32 +11,110 @@ Each command sets `fn` on its parser; the handler returns the exit code."""
10
11
 
11
12
  import argparse
12
13
  import importlib
14
+ import shutil
13
15
  import sys
14
16
  from collections.abc import Callable, Iterable
15
- from typing import Any
17
+ from dataclasses import dataclass
18
+ from typing import Any, Literal
16
19
 
17
20
  from strata_python.settings import Settings
18
21
 
19
22
  Sub = Any # argparse's subparsers object is a private generic type; only add_parser is used
20
23
 
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)
24
+ Status = Literal["ok", "missing", "slow", "broken"]
25
+ """A doctor row: `ok`; `missing` (not installed or not found); `slow` (works, but not as it should: a CPU fallback,
26
+ cuDNN off), which never fails the run; `broken` (there, but does not work)."""
27
+
28
+
29
+ @dataclass(frozen=True)
30
+ class Module:
31
+ """A module `doctor` imports: `label` is what a person recognises ("rawpy (DNG decoding)"), `module` the import
32
+ name (default: the label). An `optional` one that is missing is shown with the hint but does not fail the run."""
33
+
34
+ label: str
35
+ module: str = ""
36
+ optional: bool = False
37
+
38
+ @property
39
+ def name(self) -> str:
40
+ return self.module or self.label
41
+
42
+
43
+ @dataclass(frozen=True)
44
+ class Check:
45
+ """What an import cannot tell (a program on the PATH, whether the GPU runs a kernel): `fn` returns a status and a
46
+ line of text. `missing` or `broken` fails the run unless the check is `optional`; an exception counts as
47
+ `broken`, with the exception as its text."""
48
+
49
+ label: str
50
+ fn: Callable[[], tuple[Status, str]]
51
+ optional: bool = False
52
+
53
+
54
+ def program(name: str, hint: str = "") -> Callable[[], tuple[Status, str]]:
55
+ """A check function for a program on the PATH (`ffmpeg`, `exiftool`): `ok` with its path, else `missing`."""
56
+
57
+ def check() -> tuple[Status, str]:
58
+ found = shutil.which(name)
59
+ return ("ok", found) if found else ("missing", hint or "not on the PATH")
60
+
61
+ return check
62
+
63
+
64
+ def _row(status: Status, label: str, text: str, fails: bool) -> str:
65
+ word = status.upper() if fails else status # failing rows shout, so they are found in a long list
66
+ return f" {word:<9}{label}{': ' if text and status != 'ok' else ' '}{text}".rstrip()
67
+
68
+
69
+ def doctor(
70
+ name: str,
71
+ version: str,
72
+ imports: Iterable[str | tuple[str, str] | Module],
73
+ *,
74
+ checks: Iterable[Check] = (),
75
+ hint: str = "uv sync",
76
+ ) -> int:
77
+ """Print the app and Python versions, then one row per module and per check, in the same columns for every app.
78
+ Returns 1 when a required module is missing or a required check is missing or broken, else 0."""
79
+ ok = True
80
+ print(f"{name} {version}, python {sys.version.split()[0]}")
81
+ for item in imports:
82
+ mod = Module(item) if isinstance(item, str) else Module(*item) if isinstance(item, tuple) else item
83
+ try:
84
+ m = importlib.import_module(mod.name)
85
+ print(_row("ok", mod.label, str(getattr(m, "__version__", "")), False))
86
+ except ImportError:
87
+ ok = ok and mod.optional
88
+ print(_row("missing", mod.label, hint, not mod.optional))
89
+ for check in checks:
90
+ try:
91
+ status, text = check.fn()
92
+ except Exception as e: # a check that crashes is a broken row, not a crashed doctor
93
+ status, text = "broken", f"{type(e).__name__}: {e}"
94
+ fails = status in ("missing", "broken") and not check.optional
95
+ ok = ok and not fails
96
+ print(_row(status, check.label, text, fails))
97
+ return 0 if ok else 1
98
+
99
+
100
+ def add_doctor(
101
+ sub: Sub,
102
+ name: str,
103
+ version: str,
104
+ imports: Iterable[str | tuple[str, str] | Module],
105
+ *,
106
+ checks: Iterable[Check] = (),
107
+ hint: str = "uv sync",
108
+ ) -> None:
109
+ """`doctor`: the app and Python versions, whether each module in `imports` can be imported (a name, a
110
+ `(label, module)` pair or a `Module`), then each of the app's `checks`; `hint` is what a missing module says to
111
+ run. Exit 1 if a required module or check fails (see `doctor`)."""
112
+ mods, chks = tuple(imports), tuple(checks)
113
+
114
+ def cmd_doctor(_args: argparse.Namespace) -> int:
115
+ return doctor(name, version, mods, checks=chks, hint=hint)
116
+
117
+ sub.add_parser("doctor", help="check the environment").set_defaults(fn=cmd_doctor)
39
118
 
40
119
 
41
120
  def add_set(sub: Sub, settings: Settings) -> None:
@@ -0,0 +1,2 @@
1
+ """Typed clients for the strata services, standard library only, so every app gets them with strata-python and no
2
+ extra install. One module per service: `models` (strata-models)."""