Debian Codegraph @

tests/benchmarks/helpers.py

raw
1 from __future__ import annotations
2
3 import datetime
4 import io
5
6 from babel.messages import Catalog
7 from babel.messages.mofile import write_mo
8 from babel.messages.pofile import write_po
9
10
11 def build_catalog(message_count: int) -> Catalog:
12 # Fixed dates keep the serialized headers (and thus PO/MO bytes) stable.
13 catalog = Catalog(
14 locale="fi",
15 creation_date=datetime.datetime(2025, 10, 15, 12, 0, 0),
16 revision_date=datetime.datetime(2025, 10, 16, 12, 0, 0),
17 )
18 for i in range(message_count):
19 locations = [(f"module_{i % 7}.py", i * 3 + 1)]
20 if i % 5 == 0:
21 catalog.add(
22 (f"{i} apple", f"{i} apples"),
23 (f"{i} omena", f"{i} omenaa"),
24 locations=locations,
25 auto_comments=[f"Auto comment for message {i}"],
26 )
27 else:
28 catalog.add(
29 f"Message number {i}",
30 f"Viesti numero {i}",
31 locations=locations,
32 flags=["fuzzy"] if i % 11 == 0 else (),
33 user_comments=[f"Translator note {i}"] if i % 3 == 0 else (),
34 )
35 return catalog
36
37
38 def dump_po(catalog: Catalog) -> str:
39 buf = io.BytesIO()
40 write_po(buf, catalog)
41 return buf.getvalue().decode("utf-8")
42
43
44 def dump_mo(catalog: Catalog) -> bytes:
45 buf = io.BytesIO()
46 write_mo(buf, catalog)
47 return buf.getvalue()