home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / textwrap.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  5.0 KB  |  171 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. __revision__ = '$Id: textwrap.py 74912 2009-09-18 16:19:56Z georg.brandl $'
  5. import string
  6. import re
  7. __all__ = [
  8.     'TextWrapper',
  9.     'wrap',
  10.     'fill',
  11.     'dedent']
  12. _whitespace = '\t\n\x0b\x0c\r '
  13.  
  14. class TextWrapper:
  15.     whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
  16.     unicode_whitespace_trans = { }
  17.     uspace = ord(u' ')
  18.     for x in map(ord, _whitespace):
  19.         unicode_whitespace_trans[x] = uspace
  20.     
  21.     wordsep_re = re.compile('(\\s+|[^\\s\\w]*\\w+[^0-9\\W]-(?=\\w+[^0-9\\W])|(?<=[\\w\\!\\"\\\'\\&\\.\\,\\?])-{2,}(?=\\w))')
  22.     wordsep_simple_re = re.compile('(\\s+)')
  23.     sentence_end_re = re.compile('[%s][\\.\\!\\?][\\"\\\']?\\Z' % string.lowercase)
  24.     
  25.     def __init__(self, width = 70, initial_indent = '', subsequent_indent = '', expand_tabs = True, replace_whitespace = True, fix_sentence_endings = False, break_long_words = True, drop_whitespace = True, break_on_hyphens = True):
  26.         self.width = width
  27.         self.initial_indent = initial_indent
  28.         self.subsequent_indent = subsequent_indent
  29.         self.expand_tabs = expand_tabs
  30.         self.replace_whitespace = replace_whitespace
  31.         self.fix_sentence_endings = fix_sentence_endings
  32.         self.break_long_words = break_long_words
  33.         self.drop_whitespace = drop_whitespace
  34.         self.break_on_hyphens = break_on_hyphens
  35.         self.wordsep_re_uni = re.compile(self.wordsep_re.pattern, re.U)
  36.         self.wordsep_simple_re_uni = re.compile(self.wordsep_simple_re.pattern, re.U)
  37.  
  38.     
  39.     def _munge_whitespace(self, text):
  40.         if self.expand_tabs:
  41.             text = text.expandtabs()
  42.         if self.replace_whitespace:
  43.             if isinstance(text, str):
  44.                 text = text.translate(self.whitespace_trans)
  45.             elif isinstance(text, unicode):
  46.                 text = text.translate(self.unicode_whitespace_trans)
  47.             
  48.         return text
  49.  
  50.     
  51.     def _split(self, text):
  52.         if isinstance(text, unicode):
  53.             if self.break_on_hyphens:
  54.                 pat = self.wordsep_re_uni
  55.             else:
  56.                 pat = self.wordsep_simple_re_uni
  57.         elif self.break_on_hyphens:
  58.             pat = self.wordsep_re
  59.         else:
  60.             pat = self.wordsep_simple_re
  61.         chunks = pat.split(text)
  62.         chunks = filter(None, chunks)
  63.         return chunks
  64.  
  65.     
  66.     def _fix_sentence_endings(self, chunks):
  67.         i = 0
  68.         patsearch = self.sentence_end_re.search
  69.         while i < len(chunks) - 1:
  70.             if chunks[i + 1] == ' ' and patsearch(chunks[i]):
  71.                 chunks[i + 1] = '  '
  72.                 i += 2
  73.                 continue
  74.             i += 1
  75.  
  76.     
  77.     def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
  78.         if width < 1:
  79.             space_left = 1
  80.         else:
  81.             space_left = width - cur_len
  82.         if self.break_long_words:
  83.             cur_line.append(reversed_chunks[-1][:space_left])
  84.             reversed_chunks[-1] = reversed_chunks[-1][space_left:]
  85.         elif not cur_line:
  86.             cur_line.append(reversed_chunks.pop())
  87.  
  88.     
  89.     def _wrap_chunks(self, chunks):
  90.         lines = []
  91.         if self.width <= 0:
  92.             raise ValueError('invalid width %r (must be > 0)' % self.width)
  93.         chunks.reverse()
  94.         while chunks:
  95.             cur_line = []
  96.             cur_len = 0
  97.             if lines:
  98.                 indent = self.subsequent_indent
  99.             else:
  100.                 indent = self.initial_indent
  101.             width = self.width - len(indent)
  102.             if self.drop_whitespace and chunks[-1].strip() == '' and lines:
  103.                 del chunks[-1]
  104.             while chunks:
  105.                 l = len(chunks[-1])
  106.                 if cur_len + l <= width:
  107.                     cur_line.append(chunks.pop())
  108.                     cur_len += l
  109.                     continue
  110.                 break
  111.             if chunks and len(chunks[-1]) > width:
  112.                 self._handle_long_word(chunks, cur_line, cur_len, width)
  113.             if self.drop_whitespace and cur_line and cur_line[-1].strip() == '':
  114.                 del cur_line[-1]
  115.             if cur_line:
  116.                 lines.append(indent + ''.join(cur_line))
  117.                 continue
  118.             return lines
  119.  
  120.     
  121.     def wrap(self, text):
  122.         text = self._munge_whitespace(text)
  123.         chunks = self._split(text)
  124.         if self.fix_sentence_endings:
  125.             self._fix_sentence_endings(chunks)
  126.         return self._wrap_chunks(chunks)
  127.  
  128.     
  129.     def fill(self, text):
  130.         return '\n'.join(self.wrap(text))
  131.  
  132.  
  133.  
  134. def wrap(text, width = 70, **kwargs):
  135.     w = TextWrapper(width = width, **kwargs)
  136.     return w.wrap(text)
  137.  
  138.  
  139. def fill(text, width = 70, **kwargs):
  140.     w = TextWrapper(width = width, **kwargs)
  141.     return w.fill(text)
  142.  
  143. _whitespace_only_re = re.compile('^[ \t]+$', re.MULTILINE)
  144. _leading_whitespace_re = re.compile('(^[ \t]*)(?:[^ \t\n])', re.MULTILINE)
  145.  
  146. def dedent(text):
  147.     margin = None
  148.     text = _whitespace_only_re.sub('', text)
  149.     indents = _leading_whitespace_re.findall(text)
  150.     for indent in indents:
  151.         if margin is None:
  152.             margin = indent
  153.             continue
  154.         if indent.startswith(margin):
  155.             continue
  156.         if margin.startswith(indent):
  157.             margin = indent
  158.             continue
  159.         margin = ''
  160.     
  161.     if 0 and margin:
  162.         for line in text.split('\n'):
  163.             pass
  164.         
  165.     if margin:
  166.         text = re.sub('(?m)^' + margin, '', text)
  167.     return text
  168.  
  169. if __name__ == '__main__':
  170.     print dedent('Hello there.\n  This is indented.')
  171.