Helper class to find comments in a token stream. Can only find comments for gettext calls forwards. Once the comment from line 4 is found, a comment for line 1 will not return a usable value.
| 503 | |
| 504 | |
| 505 | class _CommentFinder(object): |
| 506 | """Helper class to find comments in a token stream. Can only |
| 507 | find comments for gettext calls forwards. Once the comment |
| 508 | from line 4 is found, a comment for line 1 will not return a |
| 509 | usable value. |
| 510 | """ |
| 511 | |
| 512 | def __init__(self, tokens, comment_tags): |
| 513 | self.tokens = tokens |
| 514 | self.comment_tags = comment_tags |
| 515 | self.offset = 0 |
| 516 | self.last_lineno = 0 |
| 517 | |
| 518 | def find_backwards(self, offset): |
| 519 | try: |
| 520 | for _, token_type, token_value in \ |
| 521 | reversed(self.tokens[self.offset:offset]): |
| 522 | if token_type in ('comment', 'linecomment'): |
| 523 | try: |
| 524 | prefix, comment = token_value.split(None, 1) |
| 525 | except ValueError: |
| 526 | continue |
| 527 | if prefix in self.comment_tags: |
| 528 | return [comment.rstrip()] |
| 529 | return [] |
| 530 | finally: |
| 531 | self.offset = offset |
| 532 | |
| 533 | def find_comments(self, lineno): |
| 534 | if not self.comment_tags or self.last_lineno > lineno: |
| 535 | return [] |
| 536 | for idx, (token_lineno, _, _) in enumerate(self.tokens[self.offset:]): |
| 537 | if token_lineno > lineno: |
| 538 | return self.find_backwards(self.offset + idx) |
| 539 | return self.find_backwards(len(self.tokens)) |
| 540 | |
| 541 | |
| 542 | def babel_extract(fileobj, keywords, comment_tags, options): |
no outgoing calls
no test coverage detected
searching dependent graphs…