Skip to content

Commit 7430293

Browse files
authored
Merge pull request #514 from moritz-gross/refactor-diffrules
refactor diff_rules()
2 parents 6cabe24 + c45c2b6 commit 7430293

1 file changed

Lines changed: 32 additions & 40 deletions

File tree

PythonScripts/audit_translations/parsers.py

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -302,24 +302,28 @@ def diff_rules(english_rule: RuleInfo, translated_rule: RuleInfo) -> list[RuleDi
302302
Compare two rules and return fine-grained differences.
303303
Ignores text content differences (T/t values) but catches structural changes.
304304
"""
305-
differences = []
306-
# Check match pattern differences
307-
en_match_raw = extract_match_pattern(english_rule.data)
308-
translated_match_raw = extract_match_pattern(translated_rule.data)
309-
en_match = normalize_xpath(en_match_raw)
310-
translated_match = normalize_xpath(translated_match_raw)
311-
if en_match != translated_match and en_match and translated_match:
305+
differences: list[RuleDifference] = []
306+
307+
def add_difference(diff_type: DiffType, description: str, english_snippet: str, translated_snippet: str) -> None:
312308
differences.append(
313309
RuleDifference(
314-
english_rule=english_rule,
315-
translated_rule=translated_rule,
316-
diff_type=DiffType.MATCH,
317-
description="Match pattern differs",
318-
english_snippet=en_match,
319-
translated_snippet=translated_match,
310+
english_rule,
311+
translated_rule,
312+
diff_type,
313+
description,
314+
english_snippet,
315+
translated_snippet,
320316
)
321317
)
322318

319+
# Check match pattern differences
320+
en_match_raw = extract_match_pattern(english_rule.data)
321+
tr_match_raw = extract_match_pattern(translated_rule.data)
322+
en_match = normalize_xpath(en_match_raw)
323+
tr_match = normalize_xpath(tr_match_raw)
324+
if en_match != tr_match and en_match and tr_match:
325+
add_difference(DiffType.MATCH, "Match pattern differs", en_match, tr_match)
326+
323327
# Check condition differences
324328
en_conditions_raw = extract_conditions(english_rule.data)
325329
tr_conditions_raw = extract_conditions(translated_rule.data)
@@ -329,15 +333,11 @@ def diff_rules(english_rule: RuleInfo, translated_rule: RuleInfo) -> list[RuleDi
329333
# Find specific differences
330334
en_set, tr_set = set(en_conditions), set(tr_conditions)
331335
if en_set != tr_set:
332-
differences.append(
333-
RuleDifference(
334-
english_rule=english_rule,
335-
translated_rule=translated_rule,
336-
diff_type=DiffType.CONDITION,
337-
description="Conditions differ",
338-
english_snippet=", ".join(dedup_list(en_conditions)) or "(none)",
339-
translated_snippet=", ".join(dedup_list(tr_conditions)) or "(none)",
340-
)
336+
add_difference(
337+
DiffType.CONDITION,
338+
"Conditions differ",
339+
", ".join(dedup_list(en_conditions)) or "(none)",
340+
", ".join(dedup_list(tr_conditions)) or "(none)",
341341
)
342342

343343
# Check variable differences
@@ -347,30 +347,22 @@ def diff_rules(english_rule: RuleInfo, translated_rule: RuleInfo) -> list[RuleDi
347347
en_var_names = {v[0] for v in en_vars}
348348
tr_var_names = {v[0] for v in tr_vars}
349349
if en_var_names != tr_var_names:
350-
differences.append(
351-
RuleDifference(
352-
english_rule=english_rule,
353-
translated_rule=translated_rule,
354-
diff_type=DiffType.VARIABLES,
355-
description="Variable definitions differ",
356-
english_snippet=", ".join(sorted(en_var_names)) or "(none)",
357-
translated_snippet=", ".join(sorted(tr_var_names)) or "(none)",
358-
)
350+
add_difference(
351+
DiffType.VARIABLES,
352+
"Variable definitions differ",
353+
", ".join(sorted(en_var_names)) or "(none)",
354+
", ".join(sorted(tr_var_names)) or "(none)",
359355
)
360356

361357
# Check structural differences (test/if/then/else blocks)
362358
en_structure = extract_structure_elements(english_rule.data)
363359
tr_structure = extract_structure_elements(translated_rule.data)
364360
if en_structure != tr_structure:
365-
differences.append(
366-
RuleDifference(
367-
english_rule=english_rule,
368-
translated_rule=translated_rule,
369-
diff_type=DiffType.STRUCTURE,
370-
description="Rule structure differs (test/if/then/else blocks)",
371-
english_snippet=" ".join(en_structure),
372-
translated_snippet=" ".join(tr_structure),
373-
)
361+
add_difference(
362+
DiffType.STRUCTURE,
363+
"Rule structure differs (test/if/then/else blocks)",
364+
" ".join(en_structure),
365+
" ".join(tr_structure),
374366
)
375367

376368
return differences

0 commit comments

Comments
 (0)