Linter for inkscape extension

This commit is contained in:
Alex committed 2026-10-24 21:38:03 +02:00
1 parent 00e9ba3434
commit 247ecc3f5a
17 files changed
+1002 -249

No files matched your search

+28 -26
View File
@@ -1,5 +1,7 @@
# coding=utf-8
"""Tests de l'extension « Dog Bandana » (pytest)."""
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2026 Alex
"""Tests of the "Dog Bandana Pattern Fill" extension (pytest)."""
import os
import re
@@ -23,7 +25,7 @@ SCALED = os.path.join(HERE, "tests", "data", "scaled.svg")
# --------------------------------------------------------------------------
# Noyau (sans inkex)
# Core (without inkex)
# --------------------------------------------------------------------------
def test_parse_color_inkscape_integer():
@@ -34,7 +36,7 @@ def test_parse_color_inkscape_integer():
def test_parse_color_hex_and_invalid():
assert parse_color("#123456") == ("#123456", 1.0)
assert parse_color("#abc") == ("#aabbcc", 1.0)
assert parse_color("pas une couleur") == ("#b3b3b3", 1.0)
assert parse_color("not a color") == ("#b3b3b3", 1.0)
def test_polylines_to_d():
@@ -43,7 +45,7 @@ def test_polylines_to_d():
def covered_segments(polylines):
"""Segments (arrondis, sens indifferent) couverts par des polylignes."""
"""Segments (rounded, direction ignored) covered by polylines."""
out = []
for line in polylines:
for p, q in zip(line, line[1:]):
@@ -53,7 +55,7 @@ def covered_segments(polylines):
def test_chain_polylines_joins_touching_lines():
# Un « Y » (4 noeuds impairs : 2 traits au minimum) et un trait isole.
# A "Y" (4 odd nodes: at least 2 strokes) and an isolated stroke.
y_shape = [[(0, 0), (0, -1)], [(0, 0), (1, 1)], [(-1, 1), (0, 0)]]
lone = [[(5, 5), (6, 5), (7, 6)]]
chained = chain_polylines(y_shape + lone)
@@ -83,7 +85,7 @@ def test_order_polylines_rotates_loops():
def test_laser_order_cuts_travel_on_a_grid():
# Quadrillage de segments unitaires dans un ordre defavorable.
# Grid of unit segments in an unfavourable order.
segments = [[(x, y), (x + 1, y)] for x in range(20) for y in range(0, 20, 2)]
segments += [[(x, y), (x, y + 1)] for y in range(20) for x in range(0, 20, 2)]
segments = segments[::2] + segments[1::2]
@@ -93,7 +95,7 @@ def test_laser_order_cuts_travel_on_a_grid():
# --------------------------------------------------------------------------
# Motif de Turing : etapes
# Turing pattern: steps
# --------------------------------------------------------------------------
def test_rasterize_square_with_hole_even_odd():
@@ -105,10 +107,10 @@ def test_rasterize_square_with_hole_even_odd():
def test_rasterize_vertex_on_scanline_counted_once():
# Sommets de ce losange exactement sur les centres de lignes de pixels.
# Vertices of this diamond exactly on pixel row centers.
diamond = [(5, 0.5), (9.5, 5.5), (5, 10.5), (0.5, 5.5)]
mask = rasterize([diamond], 0, 0, 1.0, 10, 11)
for row in mask: # chaque ligne : un seul segment
for row in mask: # each row: a single run
assert np.count_nonzero(np.diff(row.astype(int))) <= 2
@@ -126,14 +128,14 @@ def test_skeletonize_bar_to_single_line():
bar[5:10, 5:35] = True
skeleton = skeletonize(bar)
assert skeleton.any()
assert skeleton.sum(axis=0).max() == 1 # un pixel par colonne
assert skeleton.sum(axis=0).max() == 1 # one pixel per column
assert (skeleton <= bar).all()
def test_trace_skeleton_line_cross_dot_and_loop():
img = np.zeros((30, 30), dtype=bool)
img[2, 2:12] = True # segment
img[20, 5] = True # point isole
img[20, 5] = True # isolated dot
lines = trace_skeleton(img)
assert sorted(len(line) for line in lines) == [1, 10]
@@ -151,7 +153,7 @@ def test_trace_skeleton_line_cross_dot_and_loop():
def test_trace_skeleton_prunes_short_spurs():
img = np.zeros((20, 30), dtype=bool)
img[10, 2:28] = True
img[8:10, 15] = True # ergot de 2 pixels
img[8:10, 15] = True # 2-pixel spur
assert len(trace_skeleton(img)) == 3
assert len(trace_skeleton(img, min_spur=4)) == 2
@@ -174,7 +176,7 @@ def test_simplify_straight_line():
# --------------------------------------------------------------------------
# Motif de Turing : resultat
# Turing pattern: result
# --------------------------------------------------------------------------
RECT = [[(0, 0), (60, 0), (60, 40), (0, 40)]]
@@ -190,7 +192,7 @@ def test_pattern_stays_inside_with_margin(edge):
lines = turing_pattern(RECT, period, width, margin, seed=3, edge=edge)
assert lines
inset = margin + width / 2
tolerance = period / 8 # un pixel de calcul
tolerance = period / 8 # one computation pixel
for line in lines:
for x, y in line:
assert inset - tolerance <= x <= 60 - inset + tolerance
@@ -198,7 +200,7 @@ def test_pattern_stays_inside_with_margin(edge):
def test_pattern_density_matches_period():
"""Longueur des lignes ~ aire / periode : bandes espacees d'une periode."""
"""Line length ~ area / period: bands one period apart."""
period = 4.0
lines = turing_pattern(RECT, period, 2.0, seed=1)
total = sum(polyline_length(line) for line in lines)
@@ -230,7 +232,7 @@ def test_pattern_respects_holes():
def test_pattern_empty_cases():
assert turing_pattern([], 4.0, 2.0) == []
assert turing_pattern(RECT, 0, 2.0) == []
# Bande plus etroite que le trait : rien a dessiner.
# Strip narrower than the stroke: nothing to draw.
assert turing_pattern([[(0, 0), (60, 0), (60, 1.5), (0, 1.5)]], 4.0, 2.0) == []
@@ -251,7 +253,7 @@ def test_laser_order_cuts_travel_on_pattern():
# --------------------------------------------------------------------------
# Bout en bout (necessite inkex)
# End to end (requires inkex)
# --------------------------------------------------------------------------
def run_extension(tmp_path, *args, source=SHAPES):
@@ -279,7 +281,7 @@ def test_end_to_end_group_and_holes(tmp_path):
(["--margin=30"], "too narrow"),
])
def test_end_to_end_errors(tmp_path, capsys, args, message):
# Document inchange : inkex n'ecrit pas de sortie, seul le message compte.
# Unchanged document: inkex writes no output, only the message matters.
pytest.importorskip("inkex")
from dog_bandana import DogBandana
DogBandana().run(["--id=rect1", *args,
@@ -288,7 +290,7 @@ def test_end_to_end_errors(tmp_path, capsys, args, message):
def test_end_to_end_non_standard_document_scale(tmp_path):
"""Page de 400 mm pour 538,72 unites : svg.unittouu() s'y tromperait."""
"""400 mm page for 538.72 user units: svg.unittouu() would get it wrong."""
svg = run_extension(tmp_path, "--id=rect1", source=SCALED)
width = re.search(r"stroke-width:([\d.]+)", svg)
assert float(width.group(1)) == pytest.approx(2.0 * 538.72053 / 400, abs=0.001)
@@ -305,27 +307,27 @@ def test_end_to_end_color(tmp_path):
# --------------------------------------------------------------------------
# Traductions
# Translations
# --------------------------------------------------------------------------
def test_translations_up_to_date_and_complete():
"""Chaque texte du .inx et des .py a sa traduction dans chaque catalogue."""
"""Every text of the .inx and .py files is translated in every catalog."""
import gettext
import i18n
msgids = [msgid for msgid, _refs in i18n.extract()]
assert "Dog Bandana Pattern Fill" in msgids
assert "mm" not in msgids # unites marquees translatable="no"
assert "mm" not in msgids # units marked translatable="no"
for language in i18n.LANGUAGES:
entries = i18n.read_po(i18n.po_path(language))
missing = [m for m in msgids if not entries.get(m, ("", False))[0]]
assert not missing, "{}.po incomplet : {}".format(language, missing)
assert not missing, "{}.po incomplete: {}".format(language, missing)
catalog = gettext.translation(i18n.DOMAIN, i18n.LOCALE_DIR, [language])
for msgid in msgids:
assert catalog.gettext(msgid) == entries[msgid][0], \
"{} : .mo a recompiler (python i18n.py)".format(language)
"{}: .mo must be recompiled (python i18n.py)".format(language)
def test_po_roundtrip(tmp_path):
@@ -342,7 +344,7 @@ def test_po_roundtrip(tmp_path):
def test_inx_matches_arguments():
"""Chaque <param> du .inx a son add_argument, et inversement."""
"""Every <param> of the .inx has its add_argument, and vice versa."""
root = ET.parse(os.path.join(HERE, "dog_bandana.inx")).getroot()
params = {elem.get("name") for elem in root.iter()
if elem.tag.rsplit("}", 1)[-1] == "param"}
@@ -352,7 +354,7 @@ def test_inx_matches_arguments():
def test_inx_images_exist():
"""Les images de la boite de dialogue sont presentes (chemin relatif au .inx)."""
"""Dialog images exist (path relative to the .inx)."""
root = ET.parse(os.path.join(HERE, "dog_bandana.inx")).getroot()
for elem in root.iter():
if elem.tag.rsplit("}", 1)[-1] == "image":