Skip to content

Commit

Permalink
po: improve rewrapping when adding unit
Browse files Browse the repository at this point in the history
New instance can have wrapper None, so this case should be handled as
needed rewrapping.
  • Loading branch information
nijel committed Apr 24, 2024
1 parent 7c121d6 commit 5d449aa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
28 changes: 27 additions & 1 deletion tests/translate/storage/test_pypo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from copy import copy
from io import BytesIO

from pytest import mark, raises
Expand Down Expand Up @@ -670,6 +671,31 @@ def test_wrap_custom(self):

outstore = self.StoreClass()
outstore.wrapper.width = -1
outstore.addunit(store.units[0])
unit = copy(store.units[0])
outstore.addunit(unit)

assert max(len(line) for line in bytes(outstore).decode().splitlines()) > 77

outstore = self.StoreClass()
outstore.wrapper.width = -1
unit = copy(store.units[0])
unit.wrapper = None
outstore.addunit(unit)

assert max(len(line) for line in bytes(outstore).decode().splitlines()) > 77

outstore = self.StoreClass()
outstore.wrapper = None
unit = copy(store.units[0])
unit.wrapper = None
outstore.addunit(unit)

assert max(len(line) for line in bytes(outstore).decode().splitlines()) <= 77

outstore = self.StoreClass()
outstore.wrapper = None
unit = copy(store.units[0])
unit.wrapper.width = -1
outstore.addunit(unit)

assert max(len(line) for line in bytes(outstore).decode().splitlines()) <= 77
9 changes: 6 additions & 3 deletions translate/storage/pypo.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ def __init__(self, width=77):
break_long_words=True,
)

def __eq__(self, other):
if not isinstance(other, PoWrapper):
return False
return self.width == other.width

def _handle_long_word(
self, reversed_chunks: list[str], cur_line: list[str], cur_len: int, width: int
):
Expand Down Expand Up @@ -1034,9 +1039,7 @@ def unit_iter(self):
yield unit

def addunit(self, unit):
needs_update = (
unit.wrapper and self.wrapper and (unit.wrapper.width != self.wrapper.width)
)
needs_update = unit.wrapper != self.wrapper
unit.wrapper = self.wrapper
super().addunit(unit)
if needs_update:
Expand Down

0 comments on commit 5d449aa

Please sign in to comment.