Extract a gettext call with the given spec. Return None if the gettext call was successfully extracted, otherwise return an error message.
(self, node, spec)
| 518 | print(f'\tkeyword="{unparsed}": {err}', file=sys.stderr) |
| 519 | |
| 520 | def _extract_message_with_spec(self, node, spec): |
| 521 | """Extract a gettext call with the given spec. |
| 522 | |
| 523 | Return None if the gettext call was successfully extracted, |
| 524 | otherwise return an error message. |
| 525 | """ |
| 526 | max_index = max(spec.values()) |
| 527 | has_var_positional = any(isinstance(arg, ast.Starred) for |
| 528 | arg in node.args[:max_index+1]) |
| 529 | if has_var_positional: |
| 530 | return ('Variable positional arguments are not ' |
| 531 | 'allowed in gettext calls') |
| 532 | |
| 533 | if max_index >= len(node.args): |
| 534 | return (f'Expected at least {max_index + 1} positional ' |
| 535 | f'argument(s) in gettext call, got {len(node.args)}') |
| 536 | |
| 537 | msg_data = {} |
| 538 | for arg_type, position in spec.items(): |
| 539 | arg = node.args[position] |
| 540 | if not self._is_string_const(arg): |
| 541 | return (f'Expected a string constant for argument ' |
| 542 | f'{position + 1}, got {ast.unparse(arg)}') |
| 543 | msg_data[arg_type] = arg.value |
| 544 | |
| 545 | lineno = node.lineno |
| 546 | comments = self._extract_comments(node) |
| 547 | self._add_message(lineno, **msg_data, comments=comments) |
| 548 | |
| 549 | def _extract_comments(self, node): |
| 550 | """Extract translator comments. |
no test coverage detected