strata-python 0.2.1__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.
- strata_python/cli.py +99 -20
- strata_python/clients/__init__.py +2 -0
- strata_python/clients/models.py +780 -0
- strata_python/db/__init__.py +6 -3
- strata_python/db/models.py +15 -3
- strata_python/db/project.py +29 -16
- strata_python/db/pytest_plugin.py +63 -9
- strata_python/db/settings.py +63 -0
- strata_python/oslib.py +44 -5
- strata_python/pages/__init__.py +21 -0
- strata_python/pages/model.py +241 -0
- strata_python/pages/render.py +273 -0
- strata_python/public_check.py +54 -8
- strata_python/settings.py +243 -42
- strata_python/web.py +15 -6
- {strata_python-0.2.1.dist-info → strata_python-0.2.2.dist-info}/METADATA +78 -18
- {strata_python-0.2.1.dist-info → strata_python-0.2.2.dist-info}/RECORD +20 -14
- {strata_python-0.2.1.dist-info → strata_python-0.2.2.dist-info}/WHEEL +0 -0
- {strata_python-0.2.1.dist-info → strata_python-0.2.2.dist-info}/entry_points.txt +0 -0
- {strata_python-0.2.1.dist-info → strata_python-0.2.2.dist-info}/licenses/LICENSE +0 -0
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", "
|
|
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
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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:
|