Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

l10n-compare: Conflate anonymous sections together #30

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions moz/l10n/resource/l10n_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def l10n_sections(resource: Resource[Any, Any]) -> _L10nData[_L10nData[Any]]:
if any(isinstance(entry, Entry) for entry in section.entries)
]
ls.sort()
nonempty_idx = next(
(idx for idx, (id, comment, meta, _) in enumerate(ls) if id or comment or meta),
len(ls),
)
if nonempty_idx > 1:
# Anonymous sections are considered equivalent
first = ls[0]
for sd in ls[1:nonempty_idx]:
first[3].extend(sd[3])
del ls[1:nonempty_idx]
return ls


Expand Down
13 changes: 13 additions & 0 deletions tests/test_l10n_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,16 @@ def test_reorder_meta(self):
a = Resource(None, [Section((), [Entry(("foo",), "Foo", meta=am)])])
b = Resource(None, [Section((), [Entry(("foo",), "Foo", meta=bm)])])
assert l10n_equal(a, b)

def test_empty_sections(self):
a = Resource(
None, [Section((), [Entry(("foo",), "Foo"), Entry(("bar",), "Bar")])]
)
b = Resource(
None,
[
Section((), [Entry(("bar",), "Bar")]),
Section((), [Entry(("foo",), "Foo")]),
],
)
assert l10n_equal(a, b)