diff --git a/scripts/core/text.py b/scripts/core/text.py
new file mode 100644
index 000000000..454855a74
--- /dev/null
+++ b/scripts/core/text.py
@@ -0,0 +1,19 @@
+"""Counts and lists said in English, the same way on every page and report."""
+
+from __future__ import annotations
+
+from collections.abc import Sequence
+
+
+def count_text(number: int, noun: str, plural: str = "") -> str:
+ """A count and its noun, singular for one: "1 hash", "1,234 hashes"."""
+ plural = plural or noun + ("es" if noun.endswith(("sh", "ch", "s", "x")) else "s")
+ return f"{number:,} {noun if number == 1 else plural}"
+
+
+def series(items: Sequence[str], last: str = "and") -> str:
+ """"a", "a and b", "a, b and c", with *last* before the final item and no
+ comma before it. Empty for no item."""
+ if len(items) < 2:
+ return "".join(items)
+ return ", ".join(items[:-1]) + f" {last} {items[-1]}"
diff --git a/scripts/exporter/native.py b/scripts/exporter/native.py
index 6bf464c0e..ac90d89b8 100644
--- a/scripts/exporter/native.py
+++ b/scripts/exporter/native.py
@@ -12,6 +12,7 @@ from pathlib import Path
import yaml
from core.artifacts import write_bytes_atomic, write_if_changed
+from core.text import count_text
from core.ziptools import build_zip_contents_index
from .base_exporter import BaseExporter
from .doubled import doubled_destinations
@@ -144,12 +145,6 @@ def doubled_in(scraped: dict | None, platforms_dir: str) -> list[dict]:
)
-def _count(number: int, noun: str, plural: str = "") -> str:
- """"1 hash", "2 hashes": a count with its noun in the right number."""
- plural = plural or noun + ("es" if noun.endswith(("sh", "ch", "s", "x")) else "s")
- return f"{number} {noun if number == 1 else plural}"
-
-
def export_platform(
platform: str,
exporter_class: type,
@@ -314,7 +309,7 @@ def _landed_summary(report: Report, changes: LandedChanges, landed: int, lost: i
(changes.lists, "md5 list", "", "written, any listed value passes"),
)
parts += [
- f"{_count(number, noun, plural)} {verb}"
+ f"{count_text(number, noun, plural)} {verb}"
for number, noun, plural, verb in landed_counts
if number
]
@@ -323,7 +318,7 @@ def _landed_summary(report: Report, changes: LandedChanges, landed: int, lost: i
def _unwritten(refused: int, withheld: list[str], report: Report) -> list[str]:
return [
- f"{_count(number, noun, plural)} {what}"
+ f"{count_text(number, noun, plural)} {what}"
for number, noun, plural, what in (
(refused, "file", "", "the format or its checker cannot carry"),
(len(withheld), "update", "", "withheld"),
@@ -351,13 +346,13 @@ def _export_messages(
if candidates:
open_question = sum(c["verdict"] == "undetermined" for c in candidates)
messages.append(
- f"{_count(len(candidates), 'entry', 'entries')} no examined core is known to open"
+ f"{count_text(len(candidates), 'entry', 'entries')} no examined core is known to open"
+ (f" ({open_question} undetermined)" if open_question else "")
+ ", listed in the report"
)
if doubled:
messages.append(
- f"{_count(len(doubled), 'destination')} declared twice for two files"
+ f"{count_text(len(doubled), 'destination')} declared twice for two files"
", listed in the report"
)
messages.extend(f"withheld: {reason}" for reason in withheld)
diff --git a/scripts/sitegen/diagrams.py b/scripts/sitegen/diagrams.py
index 9d41b2375..970382a2e 100644
--- a/scripts/sitegen/diagrams.py
+++ b/scripts/sitegen/diagrams.py
@@ -15,6 +15,7 @@ from xml.sax.saxutils import escape
from model.registry import load_platform_registry
from core.paths import REPO_ROOT
+from core.text import count_text
from model import siteinputs
from sitegen import markup
@@ -518,7 +519,7 @@ def _mode_boxes() -> list[Box]:
boxes = []
for mode, (title, text) in MODE_TEXT.items():
count = len(_modes().get(mode, []))
- boxes.append(Box(title, [text, f"{count} platform" + ("" if count == 1 else "s")]))
+ boxes.append(Box(title, [text, count_text(count, "platform")]))
return boxes
diff --git a/scripts/sitegen/exports.py b/scripts/sitegen/exports.py
index 86a7bb234..b69bdefae 100644
--- a/scripts/sitegen/exports.py
+++ b/scripts/sitegen/exports.py
@@ -11,6 +11,7 @@ from __future__ import annotations
import json
from pathlib import Path
+from core.text import count_text
from sitegen import markup
EXPORTS_DIR = "exports"
@@ -91,10 +92,6 @@ def _lookup_only(block: list[str]) -> list[str]:
return ["", markup.block_open("section", markup.search_exclude()), "", *block, "", "", ""]
-def _plural(count: int, noun: str, plural: str = "") -> str:
- return f"{count:,} {noun if count == 1 else plural or noun + 's'}"
-
-
def change_text(report: dict) -> str:
"""What the updated file changes, from the report's numbers.
@@ -105,7 +102,7 @@ def change_text(report: dict) -> str:
counts = report.get("counts")
if not counts:
return report["summary"]
- parts = [f"{_plural(counts['kept'], 'entry', 'entries')} kept"]
+ parts = [f"{count_text(counts['kept'], 'entry', 'entries')} kept"]
extra = [
(counts["added"], "file", "files", "added"),
(len(report.get("hashes_updated") or []), "hash", "hashes", "updated"),
@@ -114,7 +111,7 @@ def change_text(report: dict) -> str:
(len(report.get("hashes_filled") or []), "hash", "hashes", "added"),
(len(report.get("sizes_updated") or []), "size", "sizes", "updated"),
]
- parts += [f"{_plural(n, one, many)} {verb}" for n, one, many, verb in extra if n]
+ parts += [f"{count_text(n, one, many)} {verb}" for n, one, many, verb in extra if n]
return ", ".join(parts)
@@ -131,7 +128,7 @@ def candidate_lines(candidates: list[dict]) -> list[str]:
if not candidates:
return []
open_question = sum(c.get("verdict") == "undetermined" for c in candidates)
- count = _plural(len(candidates), "entry", "entries")
+ count = count_text(len(candidates), "entry", "entries")
sentence = (
f"{count} of the original {'is' if len(candidates) == 1 else 'are'} not "
"named, by name, path or hash, in the profile of any emulator this "
@@ -187,7 +184,7 @@ def doubled_lines(doubled: list[dict]) -> list[str]:
"""
if not doubled:
return []
- count = _plural(len(doubled), "destination")
+ count = count_text(len(doubled), "destination")
lines = [
f"{count} of the original {'is' if len(doubled) == 1 else 'are'} declared "
"twice, for two different files. One path holds one file, so one of the "
@@ -299,14 +296,14 @@ def generate_exports_page(reports: list[dict], names: dict[str, str]) -> str:
counts = report.get("counts") or {}
if counts.get("not_added"):
lines.extend([
- f"{_plural(counts['not_added'], 'other file')} the emulators read "
+ f"{count_text(counts['not_added'], 'other file')} the emulators read "
f"{'is' if counts['not_added'] == 1 else 'are'} not added, for the "
"reasons above.",
"",
])
if counts.get("not_kept"):
lines.extend([
- f"{_plural(counts['not_kept'], 'entry', 'entries')} of the original "
+ f"{count_text(counts['not_kept'], 'entry', 'entries')} of the original "
"cannot be written in this format and should be checked by hand.",
"",
])
diff --git a/scripts/sitegen/platformpage.py b/scripts/sitegen/platformpage.py
index effabe384..c58facaba 100644
--- a/scripts/sitegen/platformpage.py
+++ b/scripts/sitegen/platformpage.py
@@ -27,7 +27,7 @@ from sitegen.platformfiles import (
render_platform_systems,
system_emulators,
)
-from sitegen.render import escape_tags, percent_text
+from sitegen.render import count_text, escape_tags, percent_text
# A hash a platform's list writes, in words.
@@ -230,7 +230,7 @@ def generate_platform_page(
else:
status = (
f''
- f'{non_ok} issue{"s" if non_ok > 1 else ""}'
+ f'{count_text(non_ok, "issue")}'
)
sys_emus = system_emulators(sys_id, emulator_files, by_system_declared.get(sys_id))
anchor = sys_id.replace(" ", "-")
diff --git a/scripts/sitegen/render.py b/scripts/sitegen/render.py
index bdb3aea75..90385d3c6 100644
--- a/scripts/sitegen/render.py
+++ b/scripts/sitegen/render.py
@@ -11,6 +11,7 @@ from model import siteinputs
from sitegen import markup
from core.memo import DerivedView
from core.paths import REPO_ROOT
+from core.text import count_text as count_text
from model.collection import PROGRAM_TOPS, card_moves, manufacturer_slug
from model.systemnames import (
SYSTEM_PARENTS,
@@ -59,11 +60,6 @@ def card_anchor(sha1: str) -> str:
return f"sha1-{sha1[:SHORT_HASH]}"
-def count_text(number: int, noun: str, plural: str = "") -> str:
- """A count and its noun, singular for one: the site printed "1 files" 234 times."""
- return f"{number:,} {noun if number == 1 else plural or noun + 's'}"
-
-
def escape_tags(text: str) -> str:
"""Show a placeholder such as ```` as it was written.
diff --git a/tests/test_text.py b/tests/test_text.py
new file mode 100644
index 000000000..3b5212c8a
--- /dev/null
+++ b/tests/test_text.py
@@ -0,0 +1,24 @@
+"""Counts and lists said one way across the site, the README and reports."""
+
+from __future__ import annotations
+
+import tests
+
+import unittest
+
+from core.text import count_text
+
+
+class CountsTakeTheirNumber(unittest.TestCase):
+ def test_one_is_singular_and_thousands_are_grouped(self):
+ self.assertEqual(count_text(1, "file"), "1 file")
+ self.assertEqual(count_text(0, "file"), "0 files")
+ self.assertEqual(count_text(1234, "file"), "1,234 files")
+
+ def test_a_sibilant_noun_takes_es(self):
+ self.assertEqual(count_text(2, "hash"), "2 hashes")
+ self.assertEqual(count_text(2, "entry", "entries"), "2 entries")
+
+
+if __name__ == "__main__":
+ unittest.main()