home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2326 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  6.1 KB  |  222 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import sys
  6. import os
  7. terminal_escape = re.compile('(\x01?\x1b\\[[0-9;]*m\x02?)')
  8. escape_parts = re.compile('\x01?\x1b\\[([0-9;]*)m\x02?')
  9.  
  10. class AnsiState(object):
  11.     
  12.     def __init__(self, bold = False, inverse = False, color = 'white', background = 'black', backgroundbold = False):
  13.         self.bold = bold
  14.         self.inverse = inverse
  15.         self.color = color
  16.         self.background = background
  17.         self.backgroundbold = backgroundbold
  18.  
  19.     trtable = {
  20.         'black': 0,
  21.         'red': 4,
  22.         'green': 2,
  23.         'yellow': 6,
  24.         'blue': 1,
  25.         'magenta': 5,
  26.         'cyan': 3,
  27.         'white': 7 }
  28.     revtable = dict(zip(trtable.values(), trtable.keys()))
  29.     
  30.     def get_winattr(self):
  31.         attr = 0
  32.         if self.bold:
  33.             attr |= 8
  34.         
  35.         if self.backgroundbold:
  36.             attr |= 128
  37.         
  38.         if self.inverse:
  39.             attr |= 16384
  40.         
  41.         attr |= self.trtable[self.color]
  42.         attr |= self.trtable[self.background] << 4
  43.         return attr
  44.  
  45.     
  46.     def set_winattr(self, attr):
  47.         self.bold = bool(attr & 8)
  48.         self.backgroundbold = bool(attr & 128)
  49.         self.inverse = bool(attr & 16384)
  50.         self.color = self.revtable[attr & 7]
  51.         self.background = self.revtable[(attr & 112) >> 4]
  52.  
  53.     winattr = property(get_winattr, set_winattr)
  54.     
  55.     def __repr__(self):
  56.         return 'AnsiState(bold=%s,inverse=%s,color=%9s,background=%9s,backgroundbold=%s)# 0x%x' % (self.bold, self.inverse, '"%s"' % self.color, '"%s"' % self.background, self.backgroundbold, self.winattr)
  57.  
  58.     
  59.     def copy(self):
  60.         x = AnsiState()
  61.         x.bold = self.bold
  62.         x.inverse = self.inverse
  63.         x.color = self.color
  64.         x.background = self.background
  65.         x.backgroundbold = self.backgroundbold
  66.         return x
  67.  
  68.  
  69. defaultstate = AnsiState(False, False, 'white')
  70. trtable = {
  71.     0: 'black',
  72.     1: 'red',
  73.     2: 'green',
  74.     3: 'yellow',
  75.     4: 'blue',
  76.     5: 'magenta',
  77.     6: 'cyan',
  78.     7: 'white' }
  79.  
  80. class AnsiWriter(object):
  81.     
  82.     def __init__(self, default = defaultstate):
  83.         if isinstance(defaultstate, AnsiState):
  84.             self.defaultstate = default
  85.         else:
  86.             self.defaultstate = AnsiState()
  87.             self.defaultstate.winattr = defaultstate
  88.  
  89.     
  90.     def write_color(self, text, attr = None):
  91.         if isinstance(attr, AnsiState):
  92.             defaultstate = attr
  93.         elif attr is None:
  94.             attr = self.defaultstate.copy()
  95.         else:
  96.             defaultstate = AnsiState()
  97.             defaultstate.winattr = attr
  98.             attr = defaultstate
  99.         chunks = terminal_escape.split(text)
  100.         n = 0
  101.         res = []
  102.         for chunk in chunks:
  103.             m = escape_parts.match(chunk)
  104.             if m:
  105.                 parts = m.group(1).split(';')
  106.                 if len(parts) == 1 and parts[0] == '0':
  107.                     attr = self.defaultstate.copy()
  108.                     continue
  109.                 
  110.                 for part in parts:
  111.                     if part == '0':
  112.                         attr = self.defaultstate.copy()
  113.                         attr.bold = False
  114.                         continue
  115.                     if part == '7':
  116.                         attr.inverse = True
  117.                         continue
  118.                     if part == '1':
  119.                         attr.bold = True
  120.                         continue
  121.                     if len(part) == 2:
  122.                         if part <= part:
  123.                             pass
  124.                         elif part <= '37':
  125.                             attr.color = trtable[int(part) - 30]
  126.                             continue
  127.                     if len(part) == 2:
  128.                         if part <= part:
  129.                             pass
  130.                         elif part <= '47':
  131.                             attr.color = trtable[int(part) - 40]
  132.                             continue
  133.                 
  134.                 continue
  135.             
  136.             n += len(chunk)
  137.             if True:
  138.                 res.append((attr.copy(), chunk))
  139.                 continue
  140.         
  141.         return (n, res)
  142.  
  143.     
  144.     def parse_color(self, text, attr = None):
  145.         (n, res) = self.write_color(text, attr)
  146.         return ([], [ attr.winattr for attr, text in res ])
  147.  
  148.  
  149.  
  150. def write_color(text, attr = None):
  151.     a = AnsiWriter(defaultstate)
  152.     return a.write_color(text, attr)
  153.  
  154.  
  155. def write_color_old(text, attr = None):
  156.     res = []
  157.     chunks = terminal_escape.split(text)
  158.     n = 0
  159.     if attr is None:
  160.         attr = 15
  161.     
  162.     for chunk in chunks:
  163.         m = escape_parts.match(chunk)
  164.         if m:
  165.             for part in m.group(1).split(';'):
  166.                 if part == '0':
  167.                     attr = 0
  168.                 elif part == '7':
  169.                     attr |= 16384
  170.                 
  171.                 if part == '1':
  172.                     attr |= 8
  173.                     continue
  174.                 if len(part) == 2:
  175.                     if part <= part:
  176.                         pass
  177.                     elif part <= '37':
  178.                         part = int(part) - 30
  179.                         attr = attr & -8 | (part & 1) << 2 | part & 2 | (part & 4) >> 2
  180.                         continue
  181.                 if len(part) == 2:
  182.                     if part <= part:
  183.                         pass
  184.                     elif part <= '47':
  185.                         part = int(part) - 40
  186.                         attr = attr & -113 | (part & 1) << 6 | (part & 2) << 4 | (part & 4) << 2
  187.                         continue
  188.             
  189.             continue
  190.         
  191.         n += len(chunk)
  192.         if chunk:
  193.             res.append(('0x%x' % attr, chunk))
  194.             continue
  195.     
  196.     return res
  197.  
  198. if __name__ == '__main__':
  199.     import pprint
  200.     pprint = pprint.pprint
  201.     s = '\x1b[0;31mred\x1b[0;32mgreen\x1b[0;33myellow\x1b[0;34mblue\x1b[0;35mmagenta\x1b[0;36mcyan\x1b[0;37mwhite\x1b[0m'
  202.     pprint(write_color(s))
  203.     pprint(write_color_old(s))
  204.     s = '\x1b[1;31mred\x1b[1;32mgreen\x1b[1;33myellow\x1b[1;34mblue\x1b[1;35mmagenta\x1b[1;36mcyan\x1b[1;37mwhite\x1b[0m'
  205.     pprint(write_color(s))
  206.     pprint(write_color_old(s))
  207.     s = '\x1b[0;7;31mred\x1b[0;7;32mgreen\x1b[0;7;33myellow\x1b[0;7;34mblue\x1b[0;7;35mmagenta\x1b[0;7;36mcyan\x1b[0;7;37mwhite\x1b[0m'
  208.     pprint(write_color(s))
  209.     pprint(write_color_old(s))
  210.     s = '\x1b[1;7;31mred\x1b[1;7;32mgreen\x1b[1;7;33myellow\x1b[1;7;34mblue\x1b[1;7;35mmagenta\x1b[1;7;36mcyan\x1b[1;7;37mwhite\x1b[0m'
  211.     pprint(write_color(s))
  212.     pprint(write_color_old(s))
  213.  
  214. if __name__ == '__main__':
  215.     import console
  216.     c = console.Console()
  217.     c.write_color('dhsjdhs')
  218.     c.write_color('\x1b[0;32mIn [\x1b[1;32m1\x1b[0;32m]:')
  219.     print 
  220.     pprint(write_color('\x1b[0;32mIn [\x1b[1;32m1\x1b[0;32m]:'))
  221.  
  222.