home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / textwrap.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  5.1 KB  |  182 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. __revision__ = '$Id: textwrap.py 68135 2009-01-01 15:46:10Z 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.         
  43.         if self.replace_whitespace:
  44.             if isinstance(text, str):
  45.                 text = text.translate(self.whitespace_trans)
  46.             elif isinstance(text, unicode):
  47.                 text = text.translate(self.unicode_whitespace_trans)
  48.             
  49.         
  50.         return text
  51.  
  52.     
  53.     def _split(self, text):
  54.         if isinstance(text, unicode):
  55.             if self.break_on_hyphens:
  56.                 pat = self.wordsep_re_uni
  57.             else:
  58.                 pat = self.wordsep_simple_re_uni
  59.         elif self.break_on_hyphens:
  60.             pat = self.wordsep_re
  61.         else:
  62.             pat = self.wordsep_simple_re
  63.         chunks = pat.split(text)
  64.         chunks = filter(None, chunks)
  65.         return chunks
  66.  
  67.     
  68.     def _fix_sentence_endings(self, chunks):
  69.         i = 0
  70.         pat = self.sentence_end_re
  71.         while i < len(chunks) - 1:
  72.             if chunks[i + 1] == ' ' and pat.search(chunks[i]):
  73.                 chunks[i + 1] = '  '
  74.                 i += 2
  75.                 continue
  76.             i += 1
  77.  
  78.     
  79.     def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
  80.         if width < 1:
  81.             space_left = 1
  82.         else:
  83.             space_left = width - cur_len
  84.         if self.break_long_words:
  85.             cur_line.append(reversed_chunks[-1][:space_left])
  86.             reversed_chunks[-1] = reversed_chunks[-1][space_left:]
  87.         elif not cur_line:
  88.             cur_line.append(reversed_chunks.pop())
  89.         
  90.  
  91.     
  92.     def _wrap_chunks(self, chunks):
  93.         lines = []
  94.         if self.width <= 0:
  95.             raise ValueError('invalid width %r (must be > 0)' % self.width)
  96.         self.width <= 0
  97.         chunks.reverse()
  98.         while chunks:
  99.             cur_line = []
  100.             cur_len = 0
  101.             if lines:
  102.                 indent = self.subsequent_indent
  103.             else:
  104.                 indent = self.initial_indent
  105.             width = self.width - len(indent)
  106.             if self.drop_whitespace and chunks[-1].strip() == '' and lines:
  107.                 del chunks[-1]
  108.             
  109.             while chunks:
  110.                 l = len(chunks[-1])
  111.                 if cur_len + l <= width:
  112.                     cur_line.append(chunks.pop())
  113.                     cur_len += l
  114.                     continue
  115.                 break
  116.             if chunks and len(chunks[-1]) > width:
  117.                 self._handle_long_word(chunks, cur_line, cur_len, width)
  118.             
  119.             if self.drop_whitespace and cur_line and cur_line[-1].strip() == '':
  120.                 del cur_line[-1]
  121.             
  122.             if cur_line:
  123.                 lines.append(indent + ''.join(cur_line))
  124.                 continue
  125.         return lines
  126.  
  127.     
  128.     def wrap(self, text):
  129.         text = self._munge_whitespace(text)
  130.         chunks = self._split(text)
  131.         if self.fix_sentence_endings:
  132.             self._fix_sentence_endings(chunks)
  133.         
  134.         return self._wrap_chunks(chunks)
  135.  
  136.     
  137.     def fill(self, text):
  138.         return '\n'.join(self.wrap(text))
  139.  
  140.  
  141.  
  142. def wrap(text, width = 70, **kwargs):
  143.     w = TextWrapper(width = width, **kwargs)
  144.     return w.wrap(text)
  145.  
  146.  
  147. def fill(text, width = 70, **kwargs):
  148.     w = TextWrapper(width = width, **kwargs)
  149.     return w.fill(text)
  150.  
  151. _whitespace_only_re = re.compile('^[ \t]+$', re.MULTILINE)
  152. _leading_whitespace_re = re.compile('(^[ \t]*)(?:[^ \t\n])', re.MULTILINE)
  153.  
  154. def dedent(text):
  155.     margin = None
  156.     text = _whitespace_only_re.sub('', text)
  157.     indents = _leading_whitespace_re.findall(text)
  158.     for indent in indents:
  159.         if margin is None:
  160.             margin = indent
  161.             continue
  162.         if indent.startswith(margin):
  163.             continue
  164.         if margin.startswith(indent):
  165.             margin = indent
  166.             continue
  167.         margin = ''
  168.     
  169.     if 0 and margin:
  170.         for line in text.split('\n'):
  171.             pass
  172.         
  173.     
  174.     if margin:
  175.         text = re.sub('(?m)^' + margin, '', text)
  176.     
  177.     return text
  178.  
  179. if __name__ == '__main__':
  180.     print dedent('Hello there.\n  This is indented.')
  181.  
  182.