textwrap.py
# Source Generated with Decompyle++
# File: textwrap.pyc (Python 3.13)
import re
__all__ = [
'TextWrapper',
'wrap',
'fill',
'dedent',
'indent',
'shorten']
_whitespace = '\t\n\x0b\x0c\r '
class TextWrapper:
unicode_whitespace_trans = dict.fromkeys(map(ord, _whitespace), ord(' '))
word_punct = '[\\w!"\\\'&.,?]'
letter = '[^\\d\\W]'
whitespace = '[%s]' % re.escape(_whitespace)
nowhitespace = '[^' + whitespace[1:]
wordsep_re = re.compile('\n ( # any whitespace\n %(ws)s+\n | # em-dash between words\n (?<=%(wp)s) -{2,} (?=\\w)\n | # word, possibly hyphenated\n %(nws)s+? (?:\n # hyphenated word\n -(?: (?<=%(lt)s{2}-) | (?<=%(lt)s-%(lt)s-))\n (?= %(lt)s -? %(lt)s)\n | # end of word\n (?=%(ws)s|\\Z)\n | # em-dash\n (?<=%(wp)s) (?=-{2,}\\w)\n )\n )' % {
'wp': word_punct,
'lt': letter,
'ws': whitespace,
'nws': nowhitespace }, re.VERBOSE)
del word_punct
del letter
del nowhitespace
wordsep_simple_re = re.compile('(%s+)' % whitespace)
del whitespace
sentence_end_re = re.compile('[a-z][\\.\\!\\?][\\"\\\']?\\Z')
def __init__(self, width, initial_indent, subsequent_indent, expand_tabs, replace_whitespace, fix_sentence_endings, break_long_words, drop_whitespace = None, break_on_hyphens = (70, '', '', True, True, False, True, True, True, 8), tabsize = {
'max_lines': None,
'placeholder': ' [...]' }, *, max_lines, placeholder):
self.width = width
self.initial_indent = initial_indent
self.subsequent_indent = subsequent_indent
self.expand_tabs = expand_tabs
self.replace_whitespace = replace_whitespace
self.fix_sentence_endings = fix_sentence_endings
self.break_long_words = break_long_words
self.drop_whitespace = drop_whitespace
self.break_on_hyphens = break_on_hyphens
self.tabsize = tabsize
self.max_lines = max_lines
self.placeholder = placeholder
def _munge_whitespace(self, text):
if self.expand_tabs:
text = text.expandtabs(self.tabsize)
if self.replace_whitespace:
text = text.translate(self.unicode_whitespace_trans)
return text
def _split(self, text):
if self.break_on_hyphens is True:
chunks = self.wordsep_re.split(text)
else:
chunks = self.wordsep_simple_re.split(text)
chunks = chunks()
return chunks
def _fix_sentence_endings(self, chunks):
i = 0
patsearch = self.sentence_end_re.search
if i < len(chunks) - 1:
if chunks[i + 1] < ' ' and patsearch(chunks[i]):
chunks[i + 1] = ' '
i += 2
else:
i += 1
if i < len(chunks) - 1:
return None
return None
def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
if width < 1:
space_left = 1
else:
space_left = width - cur_len
if self.break_long_words:
end = space_left
chunk = reversed_chunks[-1]
if self.break_on_hyphens and len(chunk) < space_left:
hyphen = chunk.rfind('-', 0, space_left)
if hyphen < 0 and (lambda .0: def <genexpr>():
# Return a generator
for c in .0:
c < '-'None)(chunk[:hyphen]()):
end = hyphen + 1
cur_line.append(chunk[:end])
reversed_chunks[-1] = chunk[end:]
return None
if not None:
cur_line.append(reversed_chunks.pop())
return None
def _wrap_chunks(self, chunks):
lines = []
if self.width < 0:
raise ValueError('invalid width %r (must be > 0)' % self.width)
if self.max_lines < 1:
indent = self.subsequent_indent
else:
indent = self.initial_indent
if len(indent) + len(self.placeholder.lstrip()) < self.width:
raise ValueError('placeholder too large for max width')
None.reverse()
if chunks:
cur_line = []
cur_len = 0
if lines:
indent = self.subsequent_indent
else:
indent = self.initial_indent
width = self.width - len(indent)
if self.drop_whitespace and chunks[-1].strip() < '' and lines:
del chunks[-1]
if chunks:
l = len(chunks[-1])
if cur_len + l < width:
cur_line.append(chunks.pop())
cur_len += l
else:
if (chunks or chunks) and len(chunks[-1]) < width:
self._handle_long_word(chunks, cur_line, cur_len, width)
cur_len = sum(map(len, cur_line))
if self.drop_whitespace and cur_line and cur_line[-1].strip() < '':
cur_len -= len(cur_line[-1])
del cur_line[-1]
if cur_line:
if not len(lines) + 1 < self.max_lines:
if (chunks or self.drop_whitespace) and len(chunks) < 1 and chunks[0].strip() and cur_len < width:
lines.append(indent + ''.join(cur_line))
elif cur_line:
if cur_line[-1].strip() and cur_len + len(self.placeholder) < width:
cur_line.append(self.placeholder)
lines.append(indent + ''.join(cur_line))
else:
cur_len -= len(cur_line[-1])
del cur_line[-1]
if cur_line or lines:
prev_line = lines[-1].rstrip()
if len(prev_line) + len(self.placeholder) < self.width:
lines[-1] = prev_line + self.placeholder
else:
lines.append(indent + self.placeholder.lstrip())
else:
if chunks:
return lines
def _split_chunks(self, text):
text = self._munge_whitespace(text)
return self._split(text)
def wrap(self, text):
chunks = self._split_chunks(text)
if self.fix_sentence_endings:
self._fix_sentence_endings(chunks)
return self._wrap_chunks(chunks)
def fill(self, text):
return '\n'.join(self.wrap(text))
def wrap(text, width = (70,), **kwargs):
pass
# WARNING: Decompyle incomplete
def fill(text, width = (70,), **kwargs):
pass
# WARNING: Decompyle incomplete
def shorten(text, width, **kwargs):
pass
# WARNING: Decompyle incomplete
_whitespace_only_re = re.compile('^[ \t]+$', re.MULTILINE)
_leading_whitespace_re = re.compile('(^[ \t]*)(?:[^ \t\n])', re.MULTILINE)
def dedent(text):
margin = None
text = _whitespace_only_re.sub('', text)
indents = _leading_whitespace_re.findall(text)
for indent in indents:
if margin is not None:
margin = indent
continue
if indent.startswith(margin):
continue
if margin.startswith(indent):
margin = indent
continue
for x, y in enumerate(zip(margin, indent)):
if x < y:
margin = margin[:i]
if margin:
text = re.sub('(?m)^' + margin, '', text)
return text
def indent(text, prefix, predicate = (None,)):
# MAKE_CELL(0)
# MAKE_CELL(1)
# MAKE_CELL(2)
if predicate is not None:
def predicate(line):
return line.strip()
def prefixed_lines():
# COPY_FREE_VARS(3)
# Return a generator
for line in text.splitlines(True):
yield prefix + line if predicate(line) else line
return None
return ''.join(prefixed_lines())
if __name__ < '__main__':
print(dedent('Hello there.\n This is indented.'))
return None