Skip to content

Commit

Permalink
pypo: fixed DOS newline handling in getnotes()
Browse files Browse the repository at this point in the history
- strip actual length of the newline instead of hardcoding one character
- avoid duplicite generating of comments from different scopes
- avoid string concatenation
  • Loading branch information
nijel committed Mar 25, 2024
1 parent a8187a4 commit 4a51a2a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
5 changes: 4 additions & 1 deletion tests/translate/storage/test_pypo.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,14 @@ def test_unix_newlines(self):

def test_dos_newlines(self):
"""Checks that dos newlines are properly parsed."""
posource = b'#: File1\r\n#: File2\r\nmsgid "test me"\r\nmsgstr ""\r\n'
posource = (
b'# Note!\r\n#: File1\r\n#: File2\r\nmsgid "test me"\r\nmsgstr ""\r\n'
)
pofile = self.poparse(posource)
assert len(pofile.units) == 1
assert pofile.units[0].source == "test me"
assert pofile.units[0].getlocations() == ["File1", "File2"]
assert pofile.units[0].getnotes() == "Note!"
assert bytes(pofile) == posource

def test_mac_newlines(self):
Expand Down
31 changes: 12 additions & 19 deletions translate/storage/pypo.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import re
import textwrap
import unicodedata
from itertools import chain

from translate.misc import quote
from translate.misc.multistring import multistring
Expand Down Expand Up @@ -460,33 +461,25 @@ def getalttrans(self):
return [unit]
return []

def getnotes(self, origin=None):
def getnotes(self, origin: str | None = None) -> str:
"""
Return comments based on origin value.
:param origin: programmer, developer, source code, translator or None
"""
if origin is None:
comments = "".join(
comment[2:] or self.newline for comment in self.othercomments
)
comments += "".join(
comment[3:] or self.newline for comment in self.automaticcomments
)
elif origin == "translator":
comments = "".join(
comment[2:] or self.newline for comment in self.othercomments
)
elif origin in ["programmer", "developer", "source code"]:
comments = "".join(
comment[3:] or self.newline for comment in self.automaticcomments
)
else:
parts = []
newline = self.newline
if origin == "translator" or origin is None:
parts.append(comment[2:] or newline for comment in self.othercomments)
if origin in ["programmer", "developer", "source code", None]:
parts.append(comment[3:] or newline for comment in self.automaticcomments)
if not parts:
raise ValueError("Comment type not valid")
comments = "".join(chain.from_iterable(parts))
# Let's drop the last newline
return comments[:-1]
return comments[: -len(newline)]

def addnote(self, text, origin=None, position="append"):
def addnote(self, text: str, origin: str | None = None, position: str = "append"):
"""
This is modeled on the XLIFF method.
Expand Down

0 comments on commit 4a51a2a

Please sign in to comment.