strata-python 0.2.3__tar.gz → 0.2.4__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.
Files changed (72) hide show
  1. {strata_python-0.2.3 → strata_python-0.2.4}/CHANGELOG.md +7 -0
  2. {strata_python-0.2.3 → strata_python-0.2.4}/PKG-INFO +1 -1
  3. {strata_python-0.2.3 → strata_python-0.2.4}/pyproject.toml +1 -1
  4. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/oslib.py +9 -3
  5. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/pages/render.py +10 -2
  6. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_oslib.py +43 -1
  7. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_pages.py +22 -0
  8. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_tasks.py +4 -1
  9. {strata_python-0.2.3 → strata_python-0.2.4}/.gitignore +0 -0
  10. {strata_python-0.2.3 → strata_python-0.2.4}/LICENSE +0 -0
  11. {strata_python-0.2.3 → strata_python-0.2.4}/README.md +0 -0
  12. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/__init__.py +0 -0
  13. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/check.py +0 -0
  14. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/cli.py +0 -0
  15. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/client.py +0 -0
  16. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/clients/__init__.py +0 -0
  17. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/clients/models.py +0 -0
  18. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/contracts.py +0 -0
  19. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/__init__.py +0 -0
  20. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/__main__.py +0 -0
  21. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/_alembic/env.py +0 -0
  22. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/_alembic/script.py.mako +0 -0
  23. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/cli.py +0 -0
  24. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/config.py +0 -0
  25. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/engine.py +0 -0
  26. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/guard.py +0 -0
  27. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/migrations.py +0 -0
  28. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/models.py +0 -0
  29. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/project.py +0 -0
  30. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/pytest_plugin.py +0 -0
  31. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/scratch.py +0 -0
  32. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/settings.py +0 -0
  33. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/testing.py +0 -0
  34. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/db/testserver.py +0 -0
  35. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/http_server.py +0 -0
  36. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/live.py +0 -0
  37. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/pages/__init__.py +0 -0
  38. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/pages/model.py +0 -0
  39. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/public_check.py +0 -0
  40. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/py.typed +0 -0
  41. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/pytest_plugin.py +0 -0
  42. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/settings.py +0 -0
  43. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/tasks/__init__.py +0 -0
  44. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/tasks/child.py +0 -0
  45. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/tasks/flight.py +0 -0
  46. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/tasks/progress.py +0 -0
  47. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/tasks/queue.py +0 -0
  48. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/tasks/retry.py +0 -0
  49. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/tasks/status.py +0 -0
  50. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/tasks/ticker.py +0 -0
  51. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/testing.py +0 -0
  52. {strata_python-0.2.3 → strata_python-0.2.4}/src/strata_python/web.py +0 -0
  53. {strata_python-0.2.3 → strata_python-0.2.4}/tests/conftest.py +0 -0
  54. {strata_python-0.2.3 → strata_python-0.2.4}/tests/integration/test_db_cli.py +0 -0
  55. {strata_python-0.2.3 → strata_python-0.2.4}/tests/integration/test_serve.py +0 -0
  56. {strata_python-0.2.3 → strata_python-0.2.4}/tests/integration/test_strata_check.py +0 -0
  57. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/job_handlers.py +0 -0
  58. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_check.py +0 -0
  59. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_cli.py +0 -0
  60. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_client.py +0 -0
  61. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_clients_models.py +0 -0
  62. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_contracts.py +0 -0
  63. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_db.py +0 -0
  64. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_http_server.py +0 -0
  65. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_live.py +0 -0
  66. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_package.py +0 -0
  67. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_plugin.py +0 -0
  68. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_public_check.py +0 -0
  69. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_queue.py +0 -0
  70. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_settings.py +0 -0
  71. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_testing.py +0 -0
  72. {strata_python-0.2.3 → strata_python-0.2.4}/tests/unit/test_web.py +0 -0
@@ -4,6 +4,13 @@ What each release adds or changes for an app that depends on strata-python, newe
4
4
 
5
5
  ## Unreleased
6
6
 
7
+ ## 0.2.4 (2026-11-23)
8
+
9
+ ### Fixed
10
+
11
+ - `oslib.lower_priority(nice)` sets the nice value to `nice` (never lower than the current one) instead of adding it, so `lower_priority(19)` gives 19 from a process that starts below 0. An app that worked around it with its own `setpriority` can drop that.
12
+ - `pages.render.rasterise(pdf, dpi=page.dpi)` comes back exactly the scan's size: the page's points times `dpi / 72`, rounded. pdfium rounded up, so some sizes came back a pixel wider or taller. An app that padded or cropped the raster to the scan's size can drop that.
13
+
7
14
  ## 0.2.3 (2026-11-14)
8
15
 
9
16
  ## 0.2.2 (2026-11-14)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: strata-python
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Summary: Common code for Python web server apps: OS helpers, settings, FastAPI serving, CLI commands, test fixtures and the strata-check gate
5
5
  Project-URL: Homepage, https://github.com/jburnhams/strata-python
6
6
  Project-URL: Source, https://github.com/jburnhams/strata-python
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "strata-python"
7
- version = "0.2.3" # the single source of truth: `strata_python.__version__` reads it back with importlib.metadata
7
+ version = "0.2.4" # the single source of truth: `strata_python.__version__` reads it back with importlib.metadata
8
8
  description = "Common code for Python web server apps: OS helpers, settings, FastAPI serving, CLI commands, test fixtures and the strata-check gate"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
@@ -101,8 +101,12 @@ MAC = sys.platform == "darwin"
101
101
 
102
102
 
103
103
  def lower_priority(nice: int = 19, io: bool = False) -> None:
104
- """Run this process at lower CPU priority (nice value on POSIX, below-normal/idle class on Windows). Children
105
- inherit it. Best effort: a refusal is skipped.
104
+ """Run this process at lower CPU priority (nice value `nice` on POSIX, below-normal/idle class on Windows).
105
+ Children inherit it. Best effort: a refusal is skipped.
106
+
107
+ `nice` is the value the process ends at, not an increment: it is set absolutely and never lowers the current
108
+ one, so `lower_priority(19)` gives 19 even from a process that starts below 0 (issue #28; `os.nice` adds, so a
109
+ CI runner at -10 ended at 9).
106
110
 
107
111
  `io=True` also lowers its disk priority, for batch jobs that read many large files (hashing an archive, exiftool
108
112
  over a folder) and should not slow interactive use of the same disk: the idle I/O class on Linux (children
@@ -118,7 +122,9 @@ def lower_priority(nice: int = 19, io: bool = False) -> None:
118
122
  k32.SetPriorityClass(k32.GetCurrentProcess(), 0x00100000) # PROCESS_MODE_BACKGROUND_BEGIN
119
123
  return
120
124
  with contextlib.suppress(OSError, AttributeError):
121
- os.nice(nice)
125
+ current = os.getpriority(os.PRIO_PROCESS, 0)
126
+ if nice > current:
127
+ os.setpriority(os.PRIO_PROCESS, 0, nice)
122
128
  if io:
123
129
  _lower_io_priority()
124
130
 
@@ -244,14 +244,22 @@ def page_count(pdf: bytes) -> int:
244
244
 
245
245
 
246
246
  def rasterise(pdf: bytes, page: int = 0, dpi: float = 300.0) -> Image.Image:
247
- """One page of a PDF as a greyscale picture at `dpi` (the scan's dpi gives the scan's size in pixels), white paper."""
247
+ """One page of a PDF as a greyscale picture at `dpi` (the scan's dpi gives the scan's size in pixels), white paper.
248
+
249
+ The picture is the page's size in points times `dpi / 72`, rounded to whole pixels. pdfium rounds up, so a scan
250
+ page 2660 px wide at 500 dpi (383.04 pt, stored a hair over) came back 2661 px (issue #27) and an ink comparison
251
+ against the scan had to pad or crop; the sliver of a pixel past the rounded edge is cut off here instead."""
248
252
  doc = _document(pdf)
249
253
  try:
250
254
  if not 0 <= page < len(doc):
251
255
  raise PageError(f"no page {page} in a PDF of {len(doc)}")
252
256
  p = doc[page]
253
- img = p.render(scale=dpi / 72.0).to_pil().convert("L")
257
+ scale = dpi / 72.0
258
+ img = p.render(scale=scale).to_pil().convert("L")
259
+ w, h = round(p.get_width() * scale), round(p.get_height() * scale) # pdfium's sizes already follow /Rotate
254
260
  p.close()
261
+ if img.size != (w, h): # pdfium's ceil is at most one pixel over round, so this only ever trims
262
+ img = img.crop((0, 0, min(img.width, w), min(img.height, h)))
255
263
  return img
256
264
  finally:
257
265
  doc.close()
@@ -117,7 +117,49 @@ def test_lower_priority_io_in_a_child_reaches_its_children():
117
117
 
118
118
 
119
119
  def test_lower_priority_in_this_process_with_no_change():
120
- oslib.lower_priority(0) # nice(0) is a no-op, so this process keeps its priority
120
+ # -20 is never above the current value, so this process keeps its priority. Not 0: a macOS runner starts at -10,
121
+ # and raising this process to 0 kept the below-zero test's child from starting at -10 (PR #32's CI).
122
+ oslib.lower_priority(-20)
123
+
124
+
125
+ @pytest.mark.skipif(oslib.WIN, reason="nice values are POSIX; Windows sets a priority class")
126
+ @pytest.mark.parametrize(("start", "nice", "expected"), [(5, 10, 10), (15, 10, 15), (0, 19, 19)])
127
+ def test_lower_priority_sets_the_nice_value_absolutely(start, nice, expected):
128
+ """Issue #28: `nice` is where the process ends, not an increment (os.nice would give 15 for the first case), and
129
+ it never lowers a process that is already further down. In a child, so this test process keeps its priority."""
130
+ code = (
131
+ "import os, sys\n"
132
+ "from strata_python import oslib\n"
133
+ "os.setpriority(os.PRIO_PROCESS, 0, int(sys.argv[1]))\n"
134
+ "oslib.lower_priority(int(sys.argv[2]))\n"
135
+ "print(os.getpriority(os.PRIO_PROCESS, 0))\n"
136
+ )
137
+ out = subprocess.run(
138
+ [sys.executable, "-c", code, str(start), str(nice)], capture_output=True, text=True, check=True
139
+ ).stdout
140
+ assert int(out) == expected
141
+
142
+
143
+ def _can_start_below_zero() -> bool:
144
+ """Root without CAP_SYS_NICE (a locked-down container) is refused too, so ask the kernel, in a child."""
145
+ if oslib.WIN:
146
+ return False
147
+ probe = "import os; os.setpriority(os.PRIO_PROCESS, 0, -10)"
148
+ return subprocess.run([sys.executable, "-c", probe], capture_output=True).returncode == 0
149
+
150
+
151
+ @pytest.mark.skipif(not _can_start_below_zero(), reason="starting below 0 needs root with CAP_SYS_NICE")
152
+ def test_lower_priority_from_below_zero_reaches_19():
153
+ """Issue #28 as seen: a macOS runner starts at -10, and os.nice(19) left the worker at 9."""
154
+ code = (
155
+ "import os\n"
156
+ "from strata_python import oslib\n"
157
+ "os.setpriority(os.PRIO_PROCESS, 0, -10)\n"
158
+ "oslib.lower_priority(19)\n"
159
+ "print(os.getpriority(os.PRIO_PROCESS, 0))\n"
160
+ )
161
+ out = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True, check=True).stdout
162
+ assert int(out) == 19
121
163
 
122
164
 
123
165
  def test_detached_kwargs_start_a_new_session():
@@ -1,6 +1,7 @@
1
1
  """strata_python.pages: the page description round-trips and refuses what cannot be drawn; the renderer's PDF, drawn
2
2
  back by pdfium (no mocks), puts ink where the description says and reads back as the description's text."""
3
3
 
4
+ import io
4
5
  import json
5
6
  import os
6
7
  from pathlib import Path
@@ -249,3 +250,24 @@ def test_tracking_is_the_advance_on_the_page_even_when_scaled():
249
250
  i_left = ink(img.crop((hh[2] + 1, 0, 1200, 600)))[0] + hh[2] + 1
250
251
  gap_px = 0.5 * 10 * 300 / 72
251
252
  assert abs((i_left - hh[2]) - gap_px) <= 4
253
+
254
+
255
+ @pytest.mark.parametrize(("width", "height"), [(2660, 4140), (2660, 4056), (2481, 3509)])
256
+ def test_a_page_at_its_own_dpi_comes_back_the_scan_size(width, height):
257
+ """Issue #27: points that are not whole at 500 dpi (383.04 pt) made pdfium round the raster up a pixel."""
258
+ pdf = render(Page(width, height, 500))
259
+ assert rasterise(pdf, dpi=500).size == (width, height)
260
+ if width % 2 == 0 and height % 2 == 0: # an odd size is half a pixel at half the dpi, which either way is right
261
+ assert rasterise(pdf, dpi=250).size == (width // 2, height // 2)
262
+
263
+
264
+ def test_a_rotated_page_comes_back_turned_and_at_its_rounded_size():
265
+ """A PDF from elsewhere may carry /Rotate; the rounded size follows the turn, as pdfium's own does."""
266
+ import pypdfium2 as pdfium
267
+
268
+ doc = pdfium.PdfDocument(render(Page(2660, 4140, 500)))
269
+ doc[0].set_rotation(90)
270
+ out = io.BytesIO()
271
+ doc.save(out)
272
+ doc.close()
273
+ assert rasterise(out.getvalue(), dpi=500).size == (4140, 2660)
@@ -180,7 +180,10 @@ def test_detached_runs_a_real_process_that_reports_through_its_file(tmp_path):
180
180
  time.sleep(0.05)
181
181
  d = job.read()
182
182
  assert (d["output"], d["progress"], d["project"], d["pid"]) == ("out.mp4", {"done": 1}, "p", pid)
183
- assert "finished" in (tmp_path / "logs" / "a.log").read_text()
183
+ log = tmp_path / "logs" / "a.log"
184
+ while "finished" not in log.read_text(): # the child prints after writing "done", so wait for it (PR #32's CI)
185
+ assert time.monotonic() < deadline, log.read_text()
186
+ time.sleep(0.05)
184
187
  assert not job.stop() # not running
185
188
 
186
189
 
File without changes
File without changes
File without changes