Extract translator comments. Translator comments must precede the gettext call and start with one of the comment prefixes defined by --add-comments=TAG. See the tests for examples.
(self, node)
| 547 | self._add_message(lineno, **msg_data, comments=comments) |
| 548 | |
| 549 | def _extract_comments(self, node): |
| 550 | """Extract translator comments. |
| 551 | |
| 552 | Translator comments must precede the gettext call and |
| 553 | start with one of the comment prefixes defined by |
| 554 | --add-comments=TAG. See the tests for examples. |
| 555 | """ |
| 556 | if not self.options.comment_tags: |
| 557 | return [] |
| 558 | |
| 559 | comments = [] |
| 560 | lineno = node.lineno - 1 |
| 561 | # Collect an unbroken sequence of comments starting from |
| 562 | # the line above the gettext call. |
| 563 | while lineno >= 1: |
| 564 | comment = self.comments.get(lineno) |
| 565 | if comment is None: |
| 566 | break |
| 567 | comments.append(comment) |
| 568 | lineno -= 1 |
| 569 | |
| 570 | # Find the first translator comment in the sequence and |
| 571 | # return all comments starting from that comment. |
| 572 | comments = comments[::-1] |
| 573 | first_index = next((i for i, comment in enumerate(comments) |
| 574 | if self._is_translator_comment(comment)), None) |
| 575 | if first_index is None: |
| 576 | return [] |
| 577 | return comments[first_index:] |
| 578 | |
| 579 | def _is_translator_comment(self, comment): |
| 580 | return comment.startswith(self.options.comment_tags) |
no test coverage detected