Linter for inkscape extension
This commit is contained in:
1 parent
00e9ba3434
commit
247ecc3f5a
17 files changed
+1002
-249
No files matched your search
@@ -1,23 +1,26 @@
|
||||
#!/usr/bin/env python3
|
||||
# coding=utf-8
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
# Copyright (C) 2026 Alex
|
||||
"""
|
||||
Chaine de traduction de l'extension, sans dependance a gettext.
|
||||
Translation toolchain of the extension, without depending on gettext tools.
|
||||
|
||||
python i18n.py # extract + update + compile
|
||||
python i18n.py extract # po/<domaine>.pot depuis le .inx et les .py
|
||||
python i18n.py update # reporte le modele dans po/<langue>.po
|
||||
python i18n.py compile # po/<langue>.po -> locale/<langue>/LC_MESSAGES/*.mo
|
||||
python i18n.py extract # po/<domain>.pot from the .inx and the .py files
|
||||
python i18n.py update # merge the template into po/<language>.po
|
||||
python i18n.py compile # po/<language>.po -> locale/<language>/LC_MESSAGES/*.mo
|
||||
|
||||
Le domaine est lu dans l'attribut translationdomain du .inx ; les sources sont
|
||||
le .inx et tous les .py du dossier (hors i18n.py et tests).
|
||||
The domain is read from the translationdomain attribute of the .inx; the
|
||||
sources are the .inx and every .py file of the folder (except i18n.py and
|
||||
tests).
|
||||
|
||||
Les textes source (.inx et appels _() des .py) sont en anglais : c'est ce
|
||||
qu'Inkscape affiche pour une langue sans catalogue. Le catalogue anglais est
|
||||
rempli automatiquement avec les textes source ; seul fr.po se traduit a la main.
|
||||
Source texts (.inx and _() calls in the .py files) are in English: this is
|
||||
what Inkscape shows for a language without a catalog. The English catalog is
|
||||
filled automatically with the source texts; only fr.po is translated by hand.
|
||||
|
||||
Les chaines retenues dans le .inx suivent les regles d'Inkscape (inx.its) :
|
||||
<name>, attributs gui-text et gui-description, contenu de <option>, <item> et
|
||||
<label>, sauf translatable="no". Les espaces sont normalises, sauf sous
|
||||
Strings taken from the .inx follow Inkscape's rules (inx.its): <name>,
|
||||
gui-text and gui-description attributes, content of <option>, <item> and
|
||||
<label>, unless translatable="no". Whitespace is normalized, except under
|
||||
xml:space="preserve".
|
||||
"""
|
||||
|
||||
@@ -41,7 +44,7 @@ INX_NS = "{http://www.inkscape.org/namespace/inkscape/extension}"
|
||||
def _find_inx():
|
||||
found = sorted(name for name in os.listdir(HERE) if name.endswith(".inx"))
|
||||
if len(found) != 1:
|
||||
sys.exit("i18n.py attend un seul fichier .inx dans {} (trouve : {})".format(
|
||||
sys.exit("i18n.py expects exactly one .inx file in {} (found: {})".format(
|
||||
HERE, found))
|
||||
return found[0]
|
||||
|
||||
@@ -50,7 +53,7 @@ INX = _find_inx()
|
||||
_INX_ROOT = ET.parse(os.path.join(HERE, INX)).getroot()
|
||||
DOMAIN = _INX_ROOT.get("translationdomain")
|
||||
if not DOMAIN:
|
||||
sys.exit("Attribut translationdomain absent de " + INX)
|
||||
sys.exit("translationdomain attribute missing from " + INX)
|
||||
EXTENSION_NAME = (_INX_ROOT.findtext(INX_NS + "name") or DOMAIN).strip()
|
||||
SOURCES = [INX] + sorted(
|
||||
name for name in os.listdir(HERE)
|
||||
@@ -70,7 +73,7 @@ def _normalize(text, preserve):
|
||||
|
||||
|
||||
def extract_inx(path):
|
||||
"""Chaines traduisibles d'un fichier .inx, dans l'ordre du document."""
|
||||
"""Translatable strings of a .inx file, in document order."""
|
||||
messages = []
|
||||
|
||||
def walk(elem, preserve):
|
||||
@@ -89,7 +92,7 @@ def extract_inx(path):
|
||||
|
||||
|
||||
def extract_py(path):
|
||||
"""Arguments litteraux des appels _("...") d'un script Python."""
|
||||
"""Literal arguments of the _("...") calls of a Python script."""
|
||||
with open(path, encoding="utf-8") as handle:
|
||||
tree = ast.parse(handle.read(), path)
|
||||
calls = [node for node in ast.walk(tree)
|
||||
@@ -102,7 +105,7 @@ def extract_py(path):
|
||||
|
||||
|
||||
def extract():
|
||||
"""Liste ordonnee et sans doublon de (msgid, [fichiers sources])."""
|
||||
"""Ordered list of unique (msgid, [source files])."""
|
||||
found = {}
|
||||
for name in SOURCES:
|
||||
path = os.path.join(HERE, name)
|
||||
@@ -115,7 +118,7 @@ def extract():
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Lecture / ecriture des fichiers .po
|
||||
# Reading / writing .po files
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
_ESCAPES = {"n": "\n", "t": "\t", '"': '"', "\\": "\\"}
|
||||
@@ -138,7 +141,7 @@ def _quote(text):
|
||||
|
||||
|
||||
def read_po(path):
|
||||
"""Dictionnaire msgid -> (msgstr, fuzzy). L'en-tete a pour cle ""."""
|
||||
"""Dictionary msgid -> (msgstr, fuzzy). The header has the key ""."""
|
||||
entries = {}
|
||||
if not os.path.exists(path):
|
||||
return entries
|
||||
@@ -190,10 +193,10 @@ def _header(language):
|
||||
|
||||
|
||||
def write_po(path, language, messages, translations):
|
||||
title = ("Modele de traduction" if language is None
|
||||
else "Traduction ({})".format(language))
|
||||
out = ["# {} de l'extension Inkscape « {} ».".format(title, EXTENSION_NAME),
|
||||
"# Genere par i18n.py ; ne modifier que les msgstr.",
|
||||
title = ("Translation template" if language is None
|
||||
else "Translation ({})".format(language))
|
||||
out = ["# {} of the Inkscape extension \"{}\".".format(title, EXTENSION_NAME),
|
||||
"# Generated by i18n.py; only edit the msgstr entries.",
|
||||
"msgid \"\"",
|
||||
"msgstr " + _quote(_header(language)),
|
||||
""]
|
||||
@@ -210,11 +213,11 @@ def write_po(path, language, messages, translations):
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Compilation .mo (format GNU gettext)
|
||||
# .mo compilation (GNU gettext format)
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
def write_mo(path, catalog):
|
||||
"""Ecrit un catalogue {msgid: msgstr} au format .mo, cles triees."""
|
||||
"""Write a {msgid: msgstr} catalog in .mo format, keys sorted."""
|
||||
keys = sorted(catalog)
|
||||
ids = [key.encode("utf-8") for key in keys]
|
||||
strs = [catalog[key].encode("utf-8") for key in keys]
|
||||
@@ -244,7 +247,7 @@ def write_mo(path, catalog):
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Commandes
|
||||
# Commands
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
def po_path(language):
|
||||
@@ -259,7 +262,7 @@ def cmd_extract():
|
||||
os.makedirs(PO_DIR, exist_ok=True)
|
||||
messages = extract()
|
||||
write_po(os.path.join(PO_DIR, DOMAIN + ".pot"), None, messages, {})
|
||||
print("{} chaines -> po/{}.pot".format(len(messages), DOMAIN))
|
||||
print("{} strings -> po/{}.pot".format(len(messages), DOMAIN))
|
||||
|
||||
|
||||
def cmd_update():
|
||||
@@ -270,7 +273,7 @@ def cmd_update():
|
||||
translations = {msgid: (msgid, False) for msgid, _refs in messages}
|
||||
write_po(po_path(language), language, messages, translations)
|
||||
missing = [m for m, _r in messages if not translations.get(m, ("", False))[0]]
|
||||
print("po/{}.po : {} a traduire".format(language, len(missing)))
|
||||
print("po/{}.po: {} to translate".format(language, len(missing)))
|
||||
for msgid in missing:
|
||||
print(" " + msgid.splitlines()[0])
|
||||
|
||||
@@ -282,7 +285,7 @@ def cmd_compile():
|
||||
if msgid and msgstr and not fuzzy}
|
||||
catalog[""] = _header(language)
|
||||
write_mo(mo_path(language), catalog)
|
||||
print("{} -> {} ({} chaines)".format(
|
||||
print("{} -> {} ({} strings)".format(
|
||||
os.path.relpath(po_path(language), HERE),
|
||||
os.path.relpath(mo_path(language), HERE), len(catalog) - 1))
|
||||
|
||||
|
||||
Reference in new issue
Block a user