Mercurial > hg
comparison i18n/polib.py @ 32889:a7310a477966
i18n: drop a py25 conditional
I'm not sure how to test this one. `make update-pot` spews all kinds of
warnings, though it did before this change too.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Fri, 16 Jun 2017 20:55:34 -0400 |
parents | 6a98f9408a50 |
children | 19fc5a986669 |
comparison
equal
deleted
inserted
replaced
32888:04fa5520d167 | 32889:a7310a477966 |
---|---|
802 if plural_index: | 802 if plural_index: |
803 flength += len(plural_index) | 803 flength += len(plural_index) |
804 real_wrapwidth = wrapwidth - flength + specialchars_count | 804 real_wrapwidth = wrapwidth - flength + specialchars_count |
805 if wrapwidth > 0 and len(field) > real_wrapwidth: | 805 if wrapwidth > 0 and len(field) > real_wrapwidth: |
806 # Wrap the line but take field name into account | 806 # Wrap the line but take field name into account |
807 lines = [''] + [unescape(item) for item in wrap( | 807 lines = [''] + [unescape(item) for item in textwrap.wrap( |
808 escaped_field, | 808 escaped_field, |
809 wrapwidth - 2, # 2 for quotes "" | 809 wrapwidth - 2, # 2 for quotes "" |
810 drop_whitespace=False, | 810 drop_whitespace=False, |
811 break_long_words=False | 811 break_long_words=False |
812 )] | 812 )] |
877 for c in comments: | 877 for c in comments: |
878 val = getattr(self, c[0]) | 878 val = getattr(self, c[0]) |
879 if val: | 879 if val: |
880 for comment in val.split('\n'): | 880 for comment in val.split('\n'): |
881 if wrapwidth > 0 and len(comment) + len(c[1]) > wrapwidth: | 881 if wrapwidth > 0 and len(comment) + len(c[1]) > wrapwidth: |
882 ret += wrap( | 882 ret += textwrap.wrap( |
883 comment, | 883 comment, |
884 wrapwidth, | 884 wrapwidth, |
885 initial_indent=c[1], | 885 initial_indent=c[1], |
886 subsequent_indent=c[1], | 886 subsequent_indent=c[1], |
887 break_long_words=False | 887 break_long_words=False |
901 if wrapwidth > 0 and len(filestr) + 3 > wrapwidth: | 901 if wrapwidth > 0 and len(filestr) + 3 > wrapwidth: |
902 # textwrap split words that contain hyphen, this is not | 902 # textwrap split words that contain hyphen, this is not |
903 # what we want for filenames, so the dirty hack is to | 903 # what we want for filenames, so the dirty hack is to |
904 # temporally replace hyphens with a char that a file cannot | 904 # temporally replace hyphens with a char that a file cannot |
905 # contain, like "*" | 905 # contain, like "*" |
906 ret += [l.replace('*', '-') for l in wrap( | 906 ret += [l.replace('*', '-') for l in textwrap.wrap( |
907 filestr.replace('-', '*'), | 907 filestr.replace('-', '*'), |
908 wrapwidth, | 908 wrapwidth, |
909 initial_indent='#: ', | 909 initial_indent='#: ', |
910 subsequent_indent='#: ', | 910 subsequent_indent='#: ', |
911 break_long_words=False | 911 break_long_words=False |
1550 if len(tup) == 1: | 1550 if len(tup) == 1: |
1551 return tup[0] | 1551 return tup[0] |
1552 return tup | 1552 return tup |
1553 | 1553 |
1554 # }}} | 1554 # }}} |
1555 # class TextWrapper {{{ | |
1556 | |
1557 class TextWrapper(textwrap.TextWrapper): | |
1558 """ | |
1559 Subclass of textwrap.TextWrapper that backport the | |
1560 drop_whitespace option. | |
1561 """ | |
1562 def __init__(self, *args, **kwargs): | |
1563 drop_whitespace = kwargs.pop('drop_whitespace', True) | |
1564 textwrap.TextWrapper.__init__(self, *args, **kwargs) | |
1565 self.drop_whitespace = drop_whitespace | |
1566 | |
1567 def _wrap_chunks(self, chunks): | |
1568 """_wrap_chunks(chunks : [string]) -> [string] | |
1569 | |
1570 Wrap a sequence of text chunks and return a list of lines of | |
1571 length 'self.width' or less. (If 'break_long_words' is false, | |
1572 some lines may be longer than this.) Chunks correspond roughly | |
1573 to words and the whitespace between them: each chunk is | |
1574 indivisible (modulo 'break_long_words'), but a line break can | |
1575 come between any two chunks. Chunks should not have internal | |
1576 whitespace; ie. a chunk is either all whitespace or a "word". | |
1577 Whitespace chunks will be removed from the beginning and end of | |
1578 lines, but apart from that whitespace is preserved. | |
1579 """ | |
1580 lines = [] | |
1581 if self.width <= 0: | |
1582 raise ValueError("invalid width %r (must be > 0)" % self.width) | |
1583 | |
1584 # Arrange in reverse order so items can be efficiently popped | |
1585 # from a stack of chucks. | |
1586 chunks.reverse() | |
1587 | |
1588 while chunks: | |
1589 | |
1590 # Start the list of chunks that will make up the current line. | |
1591 # cur_len is just the length of all the chunks in cur_line. | |
1592 cur_line = [] | |
1593 cur_len = 0 | |
1594 | |
1595 # Figure out which static string will prefix this line. | |
1596 if lines: | |
1597 indent = self.subsequent_indent | |
1598 else: | |
1599 indent = self.initial_indent | |
1600 | |
1601 # Maximum width for this line. | |
1602 width = self.width - len(indent) | |
1603 | |
1604 # First chunk on line is whitespace -- drop it, unless this | |
1605 # is the very beginning of the text (ie. no lines started yet). | |
1606 if self.drop_whitespace and chunks[-1].strip() == '' and lines: | |
1607 del chunks[-1] | |
1608 | |
1609 while chunks: | |
1610 l = len(chunks[-1]) | |
1611 | |
1612 # Can at least squeeze this chunk onto the current line. | |
1613 if cur_len + l <= width: | |
1614 cur_line.append(chunks.pop()) | |
1615 cur_len += l | |
1616 | |
1617 # Nope, this line is full. | |
1618 else: | |
1619 break | |
1620 | |
1621 # The current line is full, and the next chunk is too big to | |
1622 # fit on *any* line (not just this one). | |
1623 if chunks and len(chunks[-1]) > width: | |
1624 self._handle_long_word(chunks, cur_line, cur_len, width) | |
1625 | |
1626 # If the last chunk on this line is all whitespace, drop it. | |
1627 if self.drop_whitespace and cur_line and cur_line[-1].strip() == '': | |
1628 del cur_line[-1] | |
1629 | |
1630 # Convert current line back to a string and store it in list | |
1631 # of all lines (return value). | |
1632 if cur_line: | |
1633 lines.append(indent + ''.join(cur_line)) | |
1634 | |
1635 return lines | |
1636 | |
1637 # }}} | |
1638 # function wrap() {{{ | |
1639 | |
1640 def wrap(text, width=70, **kwargs): | |
1641 """ | |
1642 Wrap a single paragraph of text, returning a list of wrapped lines. | |
1643 """ | |
1644 if sys.version_info < (2, 6): | |
1645 return TextWrapper(width=width, **kwargs).wrap(text) | |
1646 return textwrap.wrap(text, width=width, **kwargs) | |
1647 | |
1648 #}}} |