| 627 | return isinstance(node, ast.Constant) and isinstance(node.value, str) |
| 628 | |
| 629 | def write_pot_file(messages, options, fp): |
| 630 | timestamp = time.strftime('%Y-%m-%d %H:%M%z') |
| 631 | encoding = fp.encoding if fp.encoding else 'UTF-8' |
| 632 | print(pot_header % {'time': timestamp, 'version': __version__, |
| 633 | 'charset': encoding, |
| 634 | 'encoding': '8bit'}, file=fp) |
| 635 | |
| 636 | # Sort locations within each message by filename and lineno |
| 637 | sorted_keys = [ |
| 638 | (key, sorted(msg.locations)) |
| 639 | for key, msg in messages.items() |
| 640 | ] |
| 641 | # Sort messages by locations |
| 642 | # For example, a message with locations [('test.py', 1), ('test.py', 2)] will |
| 643 | # appear before a message with locations [('test.py', 1), ('test.py', 3)] |
| 644 | sorted_keys.sort(key=itemgetter(1)) |
| 645 | |
| 646 | for key, locations in sorted_keys: |
| 647 | msg = messages[key] |
| 648 | |
| 649 | for comment in msg.comments: |
| 650 | print(f'#. {comment}', file=fp) |
| 651 | |
| 652 | if options.writelocations: |
| 653 | # location comments are different b/w Solaris and GNU: |
| 654 | if options.locationstyle == options.SOLARIS: |
| 655 | for location in locations: |
| 656 | print(f'# File: {location.filename}, line: {location.lineno}', file=fp) |
| 657 | elif options.locationstyle == options.GNU: |
| 658 | # fit as many locations on one line, as long as the |
| 659 | # resulting line length doesn't exceed 'options.width' |
| 660 | locline = '#:' |
| 661 | for location in locations: |
| 662 | s = f' {location.filename}:{location.lineno}' |
| 663 | if len(locline) + len(s) <= options.width: |
| 664 | locline = locline + s |
| 665 | else: |
| 666 | print(locline, file=fp) |
| 667 | locline = f'#:{s}' |
| 668 | if len(locline) > 2: |
| 669 | print(locline, file=fp) |
| 670 | if msg.is_docstring: |
| 671 | # If the entry was gleaned out of a docstring, then add a |
| 672 | # comment stating so. This is to aid translators who may wish |
| 673 | # to skip translating some unimportant docstrings. |
| 674 | print('#, docstring', file=fp) |
| 675 | if msg.msgctxt is not None: |
| 676 | print('msgctxt', normalize(msg.msgctxt, encoding), file=fp) |
| 677 | print('msgid', normalize(msg.msgid, encoding), file=fp) |
| 678 | if msg.msgid_plural is not None: |
| 679 | print('msgid_plural', normalize(msg.msgid_plural, encoding), file=fp) |
| 680 | print('msgstr[0] ""', file=fp) |
| 681 | print('msgstr[1] ""\n', file=fp) |
| 682 | else: |
| 683 | print('msgstr ""\n', file=fp) |
| 684 | |
| 685 | |
| 686 | def main(): |