Show More
@@ -804,7 +804,7 b' class _BaseEntry(object):' | |||
|
804 | 804 | real_wrapwidth = wrapwidth - flength + specialchars_count |
|
805 | 805 | if wrapwidth > 0 and len(field) > real_wrapwidth: |
|
806 | 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 | 808 | escaped_field, |
|
809 | 809 | wrapwidth - 2, # 2 for quotes "" |
|
810 | 810 | drop_whitespace=False, |
@@ -879,7 +879,7 b' class POEntry(_BaseEntry):' | |||
|
879 | 879 | if val: |
|
880 | 880 | for comment in val.split('\n'): |
|
881 | 881 | if wrapwidth > 0 and len(comment) + len(c[1]) > wrapwidth: |
|
882 | ret += wrap( | |
|
882 | ret += textwrap.wrap( | |
|
883 | 883 | comment, |
|
884 | 884 | wrapwidth, |
|
885 | 885 | initial_indent=c[1], |
@@ -903,7 +903,7 b' class POEntry(_BaseEntry):' | |||
|
903 | 903 | # what we want for filenames, so the dirty hack is to |
|
904 | 904 | # temporally replace hyphens with a char that a file cannot |
|
905 | 905 | # contain, like "*" |
|
906 | ret += [l.replace('*', '-') for l in wrap( | |
|
906 | ret += [l.replace('*', '-') for l in textwrap.wrap( | |
|
907 | 907 | filestr.replace('-', '*'), |
|
908 | 908 | wrapwidth, |
|
909 | 909 | initial_indent='#: ', |
@@ -1552,97 +1552,3 b' class _MOFileParser(object):' | |||
|
1552 | 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 | #}}} |
General Comments 0
You need to be logged in to leave comments.
Login now