| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
|
| 29 |
"""Report the state of every Apprise translation catalog. |
| 30 |
|
| 31 |
Run it from the repository root: |
| 32 |
|
| 33 |
python packaging/i18n_status.py |
| 34 |
|
| 35 |
Every language Apprise ships is listed with the number of strings it still |
| 36 |
needs. Each of those strings is then printed as a bullet so anyone can pick |
| 37 |
a language up and start filling the gaps in. |
| 38 |
""" |
| 39 |
|
| 40 |
from __future__ import annotations |
| 41 |
|
| 42 |
import argparse |
| 43 |
from os.path import abspath, basename, dirname, isfile, join |
| 44 |
import re |
| 45 |
import sys |
| 46 |
from typing import Optional |
| 47 |
|
| 48 |
# The package our translatable strings are extracted from |
| 49 |
PACKAGE_DIR = abspath(join(dirname(__file__), "..", "apprise")) |
| 50 |
|
| 51 |
# Where our translations live |
| 52 |
I18N_DIR = join(PACKAGE_DIR, "i18n") |
| 53 |
|
| 54 |
# Our translation template. It is a build artifact rather than something the |
| 55 |
# repository keeps, so it is only a fallback for reading our strings. |
| 56 |
POT_FILE = join(I18N_DIR, "apprise.pot") |
| 57 |
|
| 58 |
# The catalog filename found inside each language directory |
| 59 |
CATALOG = join("LC_MESSAGES", "apprise.po") |
| 60 |
|
| 61 |
# Matches the start of a msgid or msgstr line, and a quoted continuation of |
| 62 |
# either; a PO file is free to wrap long strings over several lines |
| 63 |
RE_ENTRY = re.compile(r'^(?P<key>msgid|msgstr)\s+"(?P<value>.*)"$') |
| 64 |
RE_CONTINUATION = re.compile(r'^"(?P<value>.*)"$') |
| 65 |
|
| 66 |
# What a backslash stands for inside a PO string. Anything else following a |
| 67 |
# backslash is simply the character itself. |
| 68 |
PO_ESCAPES = { |
| 69 |
"\\": "\\", |
| 70 |
'"': '"', |
| 71 |
"n": "\n", |
| 72 |
"r": "\r", |
| 73 |
"t": "\t", |
| 74 |
} |
| 75 |
|
| 76 |
# A backslash and whatever it escapes |
| 77 |
RE_ESCAPE = re.compile(r"\\(.)") |
| 78 |
|
| 79 |
# A plural entry, which Apprise has none of. Reading one as an ordinary |
| 80 |
# entry would report it wrongly, so it is stepped over. |
| 81 |
RE_UNSUPPORTED = re.compile(r"^(msgid_plural|msgstr\[)") |
| 82 |
|
| 83 |
|
| 84 |
class Catalog: |
| 85 |
"""One language and the state of each string in it.""" |
| 86 |
|
| 87 |
__slots__ = ("fuzzy", "language", "missing", "translated") |
| 88 |
|
| 89 |
def __init__(self, language: str) -> None: |
| 90 |
"""Prepare an empty report for ``language``.""" |
| 91 |
# The 2 letter language code, which is also its directory name |
| 92 |
self.language = language |
| 93 |
|
| 94 |
# Strings that have a translation we can use |
| 95 |
self.translated = [] |
| 96 |
|
| 97 |
# Strings with no translation at all |
| 98 |
self.missing = [] |
| 99 |
|
| 100 |
# Strings a tool guessed at; they need a human to confirm them |
| 101 |
self.fuzzy = [] |
| 102 |
|
| 103 |
@property |
| 104 |
def complete(self) -> bool: |
| 105 |
"""True when nothing is left to translate or confirm.""" |
| 106 |
# A catalog is only done once both lists are empty. |
| 107 |
return not self.missing and not self.fuzzy |
| 108 |
|
| 109 |
|
| 110 |
def unescape(value: str) -> str: |
| 111 |
"""Return ``value`` with its PO escaping removed.""" |
| 112 |
# One left to right pass, so an escaped backslash is not mistaken for the |
| 113 |
# start of the escape that follows it. |
| 114 |
return RE_ESCAPE.sub( |
| 115 |
lambda result: PO_ESCAPES.get(result.group(1), result.group(1)), |
| 116 |
value, |
| 117 |
) |
| 118 |
|
| 119 |
|
| 120 |
class Entry: |
| 121 |
"""One entry as it is read out of a catalog.""" |
| 122 |
|
| 123 |
__slots__ = ("fuzzy", "msgid", "msgstr", "skip") |
| 124 |
|
| 125 |
def __init__(self) -> None: |
| 126 |
"""Start an empty entry.""" |
| 127 |
# The English source text and the translation given for it |
| 128 |
self.msgid = "" |
| 129 |
self.msgstr = "" |
| 130 |
|
| 131 |
# Whether a tool guessed at this entry rather than a person writing it |
| 132 |
self.fuzzy = False |
| 133 |
|
| 134 |
# Whether this is an entry shape we do not report on at all |
| 135 |
self.skip = False |
| 136 |
|
| 137 |
|
| 138 |
def read_catalog(path: str) -> tuple[dict[str, str], set[str]]: |
| 139 |
"""Return the translations in ``path`` and the fuzzy msgids among them.""" |
| 140 |
translations = {} |
| 141 |
fuzzy = set() |
| 142 |
|
| 143 |
# The entry being read, and which of its two halves we are collecting |
| 144 |
entry = Entry() |
| 145 |
key = None |
| 146 |
|
| 147 |
# Flags read ahead of the next msgid; they belong to that entry, not to |
| 148 |
# the one we may still be holding on to |
| 149 |
pending_fuzzy = False |
| 150 |
pending_skip = False |
| 151 |
|
| 152 |
def store() -> None: |
| 153 |
"""Keep the entry we just finished reading.""" |
| 154 |
nonlocal entry, key |
| 155 |
if entry.msgid and not entry.skip: |
| 156 |
translations[entry.msgid] = entry.msgstr |
| 157 |
if entry.fuzzy: |
| 158 |
fuzzy.add(entry.msgid) |
| 159 |
|
| 160 |
entry = Entry() |
| 161 |
key = None |
| 162 |
|
| 163 |
with open(path, encoding="utf-8") as fp: |
| 164 |
for line in fp: |
| 165 |
line = line.strip() |
| 166 |
if line.startswith("#~"): |
| 167 |
# An obsolete entry; it is no longer in the template |
| 168 |
continue |
| 169 |
|
| 170 |
if line.startswith("#,") and "fuzzy" in line: |
| 171 |
# The entry below this needs a human to look at it |
| 172 |
pending_fuzzy = True |
| 173 |
continue |
| 174 |
|
| 175 |
if line.startswith("#"): |
| 176 |
# A comment, such as the source file the string came from |
| 177 |
continue |
| 178 |
|
| 179 |
if line.startswith("msgctxt"): |
| 180 |
# A context belongs to the entry below this one |
| 181 |
pending_skip = True |
| 182 |
continue |
| 183 |
|
| 184 |
if RE_UNSUPPORTED.match(line): |
| 185 |
# A plural entry; Apprise has none, and reading one as an |
| 186 |
# ordinary entry would report it wrongly |
| 187 |
entry.skip = True |
| 188 |
continue |
| 189 |
|
| 190 |
result = RE_ENTRY.match(line) |
| 191 |
if result: |
| 192 |
if result.group("key") == "msgid": |
| 193 |
# A new entry begins here, and it owns whatever we read |
| 194 |
# ahead of it |
| 195 |
store() |
| 196 |
entry.fuzzy = pending_fuzzy |
| 197 |
entry.skip = pending_skip |
| 198 |
pending_fuzzy = pending_skip = False |
| 199 |
|
| 200 |
key = result.group("key") |
| 201 |
setattr(entry, key, unescape(result.group("value"))) |
| 202 |
continue |
| 203 |
|
| 204 |
result = RE_CONTINUATION.match(line) if key else None |
| 205 |
if result: |
| 206 |
# A long string wrapped onto another line |
| 207 |
setattr( |
| 208 |
entry, |
| 209 |
key, |
| 210 |
getattr(entry, key) + unescape(result.group("value")), |
| 211 |
) |
| 212 |
continue |
| 213 |
|
| 214 |
if not line: |
| 215 |
# A blank line closes the entry |
| 216 |
store() |
| 217 |
|
| 218 |
store() |
| 219 |
return (translations, fuzzy) |
| 220 |
|
| 221 |
|
| 222 |
def languages() -> list[str]: |
| 223 |
"""Return every language Apprise ships a catalog for.""" |
| 224 |
# Each language is a directory holding an LC_MESSAGES/apprise.po file. |
| 225 |
from glob import glob |
| 226 |
|
| 227 |
return sorted( |
| 228 |
basename(dirname(dirname(path))) |
| 229 |
for path in glob(join(I18N_DIR, "*", CATALOG)) |
| 230 |
) |
| 231 |
|
| 232 |
|
| 233 |
def extract() -> Optional[list[str]]: |
| 234 |
"""Return every translatable string found in the Apprise source. |
| 235 |
|
| 236 |
None comes back when Babel is not installed. This is the only list that |
| 237 |
can not go stale, so it is preferred over anything on disk. |
| 238 |
""" |
| 239 |
try: |
| 240 |
from babel.messages.extract import extract_from_dir |
| 241 |
|
| 242 |
except ImportError: |
| 243 |
# Babel ships with our development requirements, but the report is |
| 244 |
# still useful without it |
| 245 |
return None |
| 246 |
|
| 247 |
msgids = [] |
| 248 |
seen = set() |
| 249 |
for _filename, _lineno, message, _comments, _context in extract_from_dir( |
| 250 |
PACKAGE_DIR, method_map=[("**.py", "python")] |
| 251 |
): |
| 252 |
# A message is a string on its own, or a tuple when it has plural |
| 253 |
# forms; only the singular is what Apprise asks us to translate |
| 254 |
msgid = message[0] if isinstance(message, tuple) else message |
| 255 |
if msgid and msgid not in seen: |
| 256 |
seen.add(msgid) |
| 257 |
msgids.append(msgid) |
| 258 |
|
| 259 |
return msgids |
| 260 |
|
| 261 |
|
| 262 |
def reference() -> tuple[list[str], str]: |
| 263 |
"""Return every string Apprise can translate, and where it came from. |
| 264 |
|
| 265 |
The source is read directly when Babel is available. The template is |
| 266 |
the fallback; it is built by ``tox -e i18n`` and is not kept in the |
| 267 |
repository, so it can be absent or out of date. |
| 268 |
""" |
| 269 |
msgids = extract() |
| 270 |
if msgids is not None: |
| 271 |
return (msgids, "source") |
| 272 |
|
| 273 |
if isfile(POT_FILE): |
| 274 |
template, _ = read_catalog(POT_FILE) |
| 275 |
return (list(template), "template") |
| 276 |
|
| 277 |
return ([], "") |
| 278 |
|
| 279 |
|
| 280 |
def inspect(language: str, msgids: list[str]) -> Catalog: |
| 281 |
"""Measure ``language`` against the strings in ``msgids``.""" |
| 282 |
catalog = Catalog(language) |
| 283 |
translations, fuzzy = read_catalog(join(I18N_DIR, language, CATALOG)) |
| 284 |
for msgid in msgids: |
| 285 |
if msgid in fuzzy or not translations.get(msgid): |
| 286 |
# Nothing usable; a fuzzy entry is a guess, not a translation |
| 287 |
target = catalog.fuzzy if msgid in fuzzy else catalog.missing |
| 288 |
target.append(msgid) |
| 289 |
continue |
| 290 |
|
| 291 |
catalog.translated.append(msgid) |
| 292 |
|
| 293 |
return catalog |
| 294 |
|
| 295 |
|
| 296 |
def main() -> int: |
| 297 |
"""Print our report and return the exit code to finish with.""" |
| 298 |
parser = argparse.ArgumentParser( |
| 299 |
description=( |
| 300 |
"Report which Apprise translations are complete and list the" |
| 301 |
" strings the rest are still waiting on." |
| 302 |
), |
| 303 |
) |
| 304 |
parser.add_argument( |
| 305 |
"-l", |
| 306 |
"--language", |
| 307 |
action="append", |
| 308 |
metavar="CODE", |
| 309 |
help=("only report on this language; may be used more than once"), |
| 310 |
) |
| 311 |
parser.add_argument( |
| 312 |
"-q", |
| 313 |
"--quiet", |
| 314 |
action="store_true", |
| 315 |
help="print the summary only, leaving the missing strings out", |
| 316 |
) |
| 317 |
parser.add_argument( |
| 318 |
"-s", |
| 319 |
"--strict", |
| 320 |
action="store_true", |
| 321 |
help="exit with an error when anything is left to translate", |
| 322 |
) |
| 323 |
args = parser.parse_args() |
| 324 |
|
| 325 |
# Work out which languages we were asked about |
| 326 |
available = languages() |
| 327 |
requested = args.language if args.language else available |
| 328 |
unknown = [ |
| 329 |
language |
| 330 |
for language in requested |
| 331 |
if not isfile(join(I18N_DIR, language, CATALOG)) |
| 332 |
] |
| 333 |
if unknown: |
| 334 |
print( |
| 335 |
"No translation exists for: " + ", ".join(unknown), |
| 336 |
file=sys.stderr, |
| 337 |
) |
| 338 |
return 1 |
| 339 |
|
| 340 |
if not available: |
| 341 |
print( |
| 342 |
f"No translations were found under {I18N_DIR}.", |
| 343 |
file=sys.stderr, |
| 344 |
) |
| 345 |
return 1 |
| 346 |
|
| 347 |
# Every string Apprise can translate, read from the source where we can |
| 348 |
msgids, origin = reference() |
| 349 |
if not msgids: |
| 350 |
print( |
| 351 |
"No translatable strings were found; install Babel, or run" |
| 352 |
" `tox -e i18n` to build the translation template.", |
| 353 |
file=sys.stderr, |
| 354 |
) |
| 355 |
return 1 |
| 356 |
|
| 357 |
if origin != "source" and args.strict: |
| 358 |
# Nothing on disk can certify that a catalog is complete; only the |
| 359 |
# source can, and reading it needs Babel |
| 360 |
print( |
| 361 |
"Babel is required to check completeness against the source;" |
| 362 |
" install it, or drop --strict to report on" |
| 363 |
" apprise/i18n/apprise.pot instead.", |
| 364 |
file=sys.stderr, |
| 365 |
) |
| 366 |
return 1 |
| 367 |
|
| 368 |
print(f"Apprise translations ({len(msgids)} strings per language)") |
| 369 |
if origin == "template": |
| 370 |
# The template is a file on disk and may predate the source |
| 371 |
print( |
| 372 |
"Measured against apprise/i18n/apprise.pot; install Babel, or" |
| 373 |
" run `tox -e i18n`, to measure against the source itself." |
| 374 |
) |
| 375 |
|
| 376 |
print("") |
| 377 |
|
| 378 |
catalogs = [inspect(language, msgids) for language in requested] |
| 379 |
|
| 380 |
# The summary first, so the overall state is visible at a glance |
| 381 |
for catalog in catalogs: |
| 382 |
done = len(catalog.translated) |
| 383 |
percent = 100.0 * done / len(msgids) if msgids else 100.0 |
| 384 |
state = "complete" if catalog.complete else "needs help" |
| 385 |
print( |
| 386 |
f" {catalog.language:<6} {done:>4}/{len(msgids)}" |
| 387 |
f" ({percent:5.1f}%) {state}" |
| 388 |
) |
| 389 |
|
| 390 |
if not args.quiet: |
| 391 |
# Then the strings themselves, so they can be worked through |
| 392 |
for catalog in catalogs: |
| 393 |
if catalog.complete: |
| 394 |
continue |
| 395 |
|
| 396 |
path = join("apprise", "i18n", catalog.language, CATALOG) |
| 397 |
print("") |
| 398 |
print(f"{catalog.language} -- edit {path}") |
| 399 |
for msgid in catalog.missing: |
| 400 |
print(f" - {msgid}") |
| 401 |
|
| 402 |
for msgid in catalog.fuzzy: |
| 403 |
print(f" - {msgid} (marked fuzzy; please confirm it)") |
| 404 |
|
| 405 |
incomplete = [c.language for c in catalogs if not c.complete] |
| 406 |
print("") |
| 407 |
if not incomplete: |
| 408 |
print("Every language is fully translated. Thank you!") |
| 409 |
|
| 410 |
else: |
| 411 |
print( |
| 412 |
"To help, edit the file listed above for your language and fill" |
| 413 |
" in each empty msgstr." |
| 414 |
) |
| 415 |
print("Then run these from the repository root:") |
| 416 |
print("") |
| 417 |
print(" tox -e i18n # refresh the .pot and every .po file") |
| 418 |
print(" tox -e compile # build the .mo files Apprise loads") |
| 419 |
print(" python packaging/i18n_status.py") |
| 420 |
|
| 421 |
return 1 if (args.strict and incomplete) else 0 |
| 422 |
|
| 423 |
|
| 424 |
if __name__ == "__main__": |
| 425 |
sys.exit(main()) |