Skip to content

Commit 3964846

Browse files
committed
Address review comments in catalog, frontend and pofile
- catalog: use _key_for(id, context) in add_conflict/get_conflicts to correctly handle messages with msgctxt; add docstrings to both methods - frontend: tighten type hints (Counter[_MessageID], dict[_MessageID, ...]); remove redundant early-return guard in _get_messages_from_compendiums; initialize self.compendium as empty list - pofile: replace .startswith() calls with slice comparisons for consistency
1 parent 97f1e08 commit 3964846

3 files changed

Lines changed: 31 additions & 12 deletions

File tree

babel/messages/catalog.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -790,17 +790,35 @@ def __setitem__(self, id: _MessageID, message: Message) -> None:
790790
)
791791
self._messages[key] = message
792792

793-
def add_conflict(self, message: Message, filename: str, project: str, version: str):
794-
key = message.id
793+
def add_conflict(self, message: Message, filename: str, project: str, version: str) -> None:
794+
"""Record a conflicting translation for a message.
795+
796+
When the same message ID has different translations across input files,
797+
the conflicting entry is stored and the message is marked as fuzzy in
798+
the output catalog.
799+
800+
:param message: the conflicting :class:`Message` object
801+
:param filename: the basename of the file where the conflict originates
802+
:param project: the project name of the conflicting file
803+
:param version: the project version of the conflicting file
804+
"""
805+
key = self._key_for(message.id, message.context)
795806
self._conflicts[key].append({
796807
'message': message,
797808
'filename': filename,
798809
'project': project,
799810
'version': version,
800811
})
801812

802-
def get_conflicts(self, id: _MessageID) -> list[ConflictInfo]:
803-
return self._conflicts.get(id, [])
813+
def get_conflicts(self, id: _MessageID, context: str | None = None) -> list[ConflictInfo]:
814+
"""Return all recorded conflicts for a message ID.
815+
816+
:param id: the message ID to look up conflicts for
817+
:param context: optional message context (msgctxt)
818+
:return: list of :class:`ConflictInfo` dicts, or an empty list if none
819+
"""
820+
key = self._key_for(id, context)
821+
return self._conflicts.get(key, [])
804822

805823
def add(
806824
self,

babel/messages/frontend.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
from collections import Counter, defaultdict
2525
from configparser import RawConfigParser
2626
from io import StringIO
27-
from typing import Any, BinaryIO, Iterable, Literal
27+
from typing import TYPE_CHECKING, Any, BinaryIO, Iterable, Literal
28+
29+
if TYPE_CHECKING:
30+
from babel.messages.catalog import _MessageID
2831

2932
from babel import Locale, localedata
3033
from babel import __version__ as VERSION
@@ -960,8 +963,8 @@ def finalize_options(self):
960963

961964
def _collect_message_info(self):
962965
templates: list[tuple[str, Catalog]] = []
963-
message_counts: Counter = Counter()
964-
message_strings: dict[object, set] = defaultdict(set)
966+
message_counts: Counter[_MessageID] = Counter()
967+
message_strings: dict[_MessageID, set[str | tuple[str, ...]]] = defaultdict(set)
965968

966969
for filename in self.input_files:
967970
with open(filename, 'r') as pofile:
@@ -1095,8 +1098,6 @@ def finalize_options(self):
10951098
self.width = int(self.width)
10961099

10971100
def _get_messages_from_compendiums(self, compendium_paths):
1098-
if not compendium_paths:
1099-
return
11001101
for file_path in compendium_paths:
11011102
with open(file_path, 'r') as pofile:
11021103
catalog = read_po(pofile)

babel/messages/pofile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,11 @@ def parse(self, fileobj: IO[AnyStr] | Iterable[AnyStr]) -> None:
351351
continue
352352
if needs_decode:
353353
line = line.decode(self.catalog.charset)
354-
if line.startswith('#'):
355-
if line[1:].startswith('-'):
354+
if line[:1] == '#':
355+
if line[1:2] == '-':
356356
self._invalid_pofile(line, lineno, 'cannot parse po file with conflicts')
357357

358-
if line[1:].startswith('~'):
358+
if line[1:2] == '~':
359359
self._process_message_line(lineno, line[2:].lstrip(), obsolete=True)
360360
else:
361361
try:

0 commit comments

Comments
 (0)