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

Fix #774: Allow object methods to be used as extraction-keywords #1136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 38 additions & 5 deletions babel/messages/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
from tokenize import COMMENT, NAME, OP, STRING, generate_tokens
from typing import TYPE_CHECKING, Any

from more_itertools import peekable

from babel.messages._compat import find_entrypoints
from babel.util import parse_encoding, parse_future_flags, pathmatch

Expand Down Expand Up @@ -514,12 +516,35 @@ def extract_python(
future_flags = parse_future_flags(fileobj, encoding)
next_line = lambda: fileobj.readline().decode(encoding)

tokens = generate_tokens(next_line)
tokens = peekable(generate_tokens(next_line))

# Current prefix of a Python 3.12 (PEP 701) f-string, or None if we're not
# currently parsing one.
current_fstring_start = None

def peek_dotted_name(name) -> str:
"""Used to peek ahead in the token stream to get a dotted name, if possible"""

nonlocal tokens

prev_tok = NAME

while True:
tok, value, *_ = tokens.peek()

if prev_tok == NAME and tok == OP and value == '.':
name += '.'
next(tokens)
elif tok == NAME:
name += value
next(tokens)
else:
break

prev_tok = tok

return name

for tok, value, (lineno, _), _, _ in tokens:
if call_stack == -1 and tok == NAME and value in ('def', 'class'):
in_def = True
Expand Down Expand Up @@ -552,7 +577,12 @@ def extract_python(
translator_comments.append((lineno, value))
break
elif funcname and call_stack == 0:
nested = (tok == NAME and value in keywords)
if tok == NAME:
maybe_funcname: str | None = peek_dotted_name(value)
else:
maybe_funcname = None

nested = (maybe_funcname and maybe_funcname in keywords)
if (tok == OP and value == ')') or nested:
if buf:
messages.append(''.join(buf))
Expand All @@ -576,7 +606,7 @@ def extract_python(
translator_comments = []
in_translator_comments = False
if nested:
funcname = value
funcname = maybe_funcname
elif tok == STRING:
val = _parse_python_string(value, encoding, future_flags)
if val is not None:
Expand Down Expand Up @@ -612,8 +642,11 @@ def extract_python(
call_stack -= 1
elif funcname and call_stack == -1:
funcname = None
elif tok == NAME and value in keywords:
funcname = value
elif tok == NAME:
peeked_name = peek_dotted_name(value)

if peeked_name in keywords:
funcname = peeked_name

if (current_fstring_start is not None
and tok not in {FSTRING_START, FSTRING_MIDDLE}
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def run(self):
# higher.
# Python 3.9 and later include zoneinfo which replaces pytz
'pytz>=2015.7; python_version<"3.9"',
'more-itertools'
],
extras_require={
'dev': [
Expand Down
13 changes: 13 additions & 0 deletions tests/messages/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,19 @@ def test_nested_messages(self):
assert messages[7][2] == 'Armin'
assert messages[7][3] == []

def test_dotted_keywords(self):
buf = BytesIO(b"""\
msg0 = self.gettext('Hello there')
msg1 = self.gettext('Thanks {user}', name=self.gettext('User'))
msg3 = self.gettext('Can use {both}', both=gettext('gettexts'))
""")
messages = list(extract.extract_python(buf, ('self.gettext', 'gettext'), [], {}))
assert messages[0] == (1, 'self.gettext', 'Hello there', [])
assert messages[1] == (2, 'self.gettext', ('Thanks {user}', None), [])
assert messages[2] == (2, 'self.gettext', 'User', [])
assert messages[3] == (3, 'self.gettext', ('Can use {both}', None), [])
assert messages[4] == (3, 'gettext', 'gettexts', [])


class ExtractTestCase(unittest.TestCase):

Expand Down