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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6.  
  7. try:
  8.     import curses
  9. except ImportError:
  10.     curses = None
  11.  
  12. COLOR_BLACK = 0
  13. COLOR_RED = 1
  14. COLOR_GREEN = 2
  15. COLOR_YELLOW = 3
  16. COLOR_BLUE = 4
  17. COLOR_MAGENTA = 5
  18. COLOR_CYAN = 6
  19. COLOR_WHITE = 7
  20. A_BLINK = 1
  21. A_BOLD = 2
  22. A_DIM = 4
  23. A_REVERSE = 8
  24. A_STANDOUT = 16
  25. A_UNDERLINE = 32
  26.  
  27. class Style(object):
  28.     __slots__ = ('fg', 'bg', 'attrs')
  29.     COLORNAMES = {
  30.         'black': COLOR_BLACK,
  31.         'red': COLOR_RED,
  32.         'green': COLOR_GREEN,
  33.         'yellow': COLOR_YELLOW,
  34.         'blue': COLOR_BLUE,
  35.         'magenta': COLOR_MAGENTA,
  36.         'cyan': COLOR_CYAN,
  37.         'white': COLOR_WHITE }
  38.     ATTRNAMES = {
  39.         'blink': A_BLINK,
  40.         'bold': A_BOLD,
  41.         'dim': A_DIM,
  42.         'reverse': A_REVERSE,
  43.         'standout': A_STANDOUT,
  44.         'underline': A_UNDERLINE }
  45.     
  46.     def __init__(self, fg, bg, attrs = 0):
  47.         self.fg = fg
  48.         self.bg = bg
  49.         self.attrs = attrs
  50.  
  51.     
  52.     def __call__(self, *args):
  53.         text = Text()
  54.         for arg in args:
  55.             if isinstance(arg, Text):
  56.                 text.extend(arg)
  57.                 continue
  58.             text.append((self, arg))
  59.         
  60.         return text
  61.  
  62.     
  63.     def __eq__(self, other):
  64.         if self.fg == other.fg and self.bg == other.bg:
  65.             pass
  66.         return self.attrs == other.attrs
  67.  
  68.     
  69.     def __neq__(self, other):
  70.         if not self.fg != other.fg and self.bg != other.bg:
  71.             pass
  72.         return self.attrs != other.attrs
  73.  
  74.     
  75.     def __repr__(self):
  76.         color2name = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white')
  77.         attrs2name = ('blink', 'bold', 'dim', 'reverse', 'standout', 'underline')
  78.         if not [](_[1]):
  79.             pass
  80.         return color2name[self.fg] % (color2name[self.bg], '|'.join, [], 0)
  81.  
  82.     
  83.     def fromstr(cls, value):
  84.         fg = COLOR_WHITE
  85.         bg = COLOR_BLACK
  86.         attrs = 0
  87.         parts = value.split(':')
  88.         if len(parts) > 0:
  89.             fg = cls.COLORNAMES[parts[0].lower()]
  90.             if len(parts) > 1:
  91.                 bg = cls.COLORNAMES[parts[1].lower()]
  92.                 if len(parts) > 2:
  93.                     for strattr in parts[2].split('|'):
  94.                         attrs |= cls.ATTRNAMES[strattr.lower()]
  95.                     
  96.                 
  97.             
  98.         
  99.         return cls(fg, bg, attrs)
  100.  
  101.     fromstr = classmethod(fromstr)
  102.     
  103.     def fromenv(cls, name, default):
  104.         return cls.fromstr(os.environ.get(name, default))
  105.  
  106.     fromenv = classmethod(fromenv)
  107.  
  108.  
  109. def switchstyle(s1, s2):
  110.     attrmask = A_BLINK | A_BOLD | A_UNDERLINE | A_REVERSE
  111.     a1 = s1.attrs & attrmask
  112.     a2 = s2.attrs & attrmask
  113.     args = []
  114.     if s1 != s2:
  115.         if a1 & ~a2 or s2 == style_default:
  116.             args.append('0')
  117.             s1 = style_default
  118.             a1 = 0
  119.         
  120.         if not (a1 & A_BOLD) and a2 & A_BOLD:
  121.             args.append('1')
  122.         
  123.         if not (a1 & A_UNDERLINE) and a2 & A_UNDERLINE:
  124.             args.append('4')
  125.         
  126.         if not (a1 & A_BLINK) and a2 & A_BLINK:
  127.             args.append('5')
  128.         
  129.         if not (a1 & A_REVERSE) and a2 & A_REVERSE:
  130.             args.append('7')
  131.         
  132.         if s1.fg != s2.fg:
  133.             args.append('3%d' % s2.fg)
  134.         
  135.         if s1.bg != s2.bg:
  136.             args.append('4%d' % s2.bg)
  137.         
  138.         if args:
  139.             return '\x1b[%sm' % ';'.join(args)
  140.     
  141.     return ''
  142.  
  143.  
  144. class Text(list):
  145.     
  146.     def __init__(self, *args):
  147.         list.__init__(self)
  148.         self.append(*args)
  149.  
  150.     
  151.     def __repr__(self):
  152.         return '%s.%s(%s)' % (self.__class__.__module__, self.__class__.__name__, list.__repr__(self)[1:-1])
  153.  
  154.     
  155.     def append(self, *args):
  156.         for arg in args:
  157.             if isinstance(arg, Text):
  158.                 self.extend(arg)
  159.                 continue
  160.             if isinstance(arg, tuple):
  161.                 list.append(self, arg)
  162.                 continue
  163.             if isinstance(arg, unicode):
  164.                 list.append(self, (style_default, arg))
  165.                 continue
  166.             list.append(self, (style_default, str(arg)))
  167.         
  168.  
  169.     
  170.     def insert(self, index, *args):
  171.         self[index:index] = Text(*args)
  172.  
  173.     
  174.     def __add__(self, other):
  175.         new = Text()
  176.         new.append(self)
  177.         new.append(other)
  178.         return new
  179.  
  180.     
  181.     def __iadd__(self, other):
  182.         self.append(other)
  183.         return self
  184.  
  185.     
  186.     def format(self, styled = True):
  187.         if styled:
  188.             oldstyle = style_default
  189.             for style, string in self:
  190.                 if not isinstance(style, (int, long)):
  191.                     switch = switchstyle(oldstyle, style)
  192.                     if switch:
  193.                         yield switch
  194.                     
  195.                     if string:
  196.                         yield string
  197.                     
  198.                     oldstyle = style
  199.                     continue
  200.             
  201.             switch = switchstyle(oldstyle, style_default)
  202.             if switch:
  203.                 yield switch
  204.             
  205.         else:
  206.             for style, string in self:
  207.                 if not isinstance(style, (int, long)):
  208.                     yield string
  209.                     continue
  210.             
  211.  
  212.     
  213.     def string(self, styled = True):
  214.         return ''.join(self.format(styled))
  215.  
  216.     
  217.     def __str__(self):
  218.         return self.string(False)
  219.  
  220.     
  221.     def write(self, stream, styled = True):
  222.         for part in self.format(styled):
  223.             stream.write(part)
  224.         
  225.  
  226.  
  227.  
  228. try:
  229.     import ipipe
  230. except ImportError:
  231.     pass
  232.  
  233.  
  234. def xrepr_astyle_text(self, mode = 'default'):
  235.     yield (-1, True)
  236.     for info in self:
  237.         yield info
  238.     
  239.  
  240. ipipe.xrepr.when_type(Text)(xrepr_astyle_text)
  241.  
  242. def streamstyle(stream, styled = None):
  243.     if styled is None:
  244.         
  245.         try:
  246.             styled = os.isatty(stream.fileno())
  247.         except (KeyboardInterrupt, SystemExit):
  248.             raise 
  249.         except Exception:
  250.             styled = False
  251.         except:
  252.             None<EXCEPTION MATCH>(KeyboardInterrupt, SystemExit)
  253.         
  254.  
  255.     None<EXCEPTION MATCH>(KeyboardInterrupt, SystemExit)
  256.     return styled
  257.  
  258.  
  259. def write(stream, styled, *texts):
  260.     text = Text(*texts)
  261.     text.write(stream, streamstyle(stream, styled))
  262.  
  263.  
  264. def writeln(stream, styled, *texts):
  265.     write(stream, styled, *texts)
  266.     stream.write('\n')
  267.  
  268.  
  269. class Stream(object):
  270.     
  271.     def __init__(self, stream, styled = None):
  272.         self.stream = stream
  273.         self.styled = streamstyle(stream, styled)
  274.  
  275.     
  276.     def write(self, *texts):
  277.         write(self.stream, self.styled, *texts)
  278.  
  279.     
  280.     def writeln(self, *texts):
  281.         writeln(self.stream, self.styled, *texts)
  282.  
  283.     
  284.     def __getattr__(self, name):
  285.         return getattr(self.stream, name)
  286.  
  287.  
  288.  
  289. class stdout(object):
  290.     
  291.     def write(self, *texts):
  292.         write(sys.stdout, None, *texts)
  293.  
  294.     
  295.     def writeln(self, *texts):
  296.         writeln(sys.stdout, None, *texts)
  297.  
  298.     
  299.     def __getattr__(self, name):
  300.         return getattr(sys.stdout, name)
  301.  
  302.  
  303. stdout = stdout()
  304.  
  305. class stderr(object):
  306.     
  307.     def write(self, *texts):
  308.         write(sys.stderr, None, *texts)
  309.  
  310.     
  311.     def writeln(self, *texts):
  312.         writeln(sys.stderr, None, *texts)
  313.  
  314.     
  315.     def __getattr__(self, name):
  316.         return getattr(sys.stdout, name)
  317.  
  318.  
  319. stderr = stderr()
  320. if curses is not None:
  321.     COLOR2CURSES = [
  322.         COLOR_BLACK,
  323.         COLOR_RED,
  324.         COLOR_GREEN,
  325.         COLOR_YELLOW,
  326.         COLOR_BLUE,
  327.         COLOR_MAGENTA,
  328.         COLOR_CYAN,
  329.         COLOR_WHITE]
  330.     A2CURSES = {
  331.         A_BLINK: curses.A_BLINK,
  332.         A_BOLD: curses.A_BOLD,
  333.         A_DIM: curses.A_DIM,
  334.         A_REVERSE: curses.A_REVERSE,
  335.         A_STANDOUT: curses.A_STANDOUT,
  336.         A_UNDERLINE: curses.A_UNDERLINE }
  337.  
  338. style_default = Style.fromstr('white:black')
  339. style_type_none = Style.fromstr('magenta:black')
  340. style_type_bool = Style.fromstr('magenta:black')
  341. style_type_number = Style.fromstr('yellow:black')
  342. style_type_datetime = Style.fromstr('magenta:black')
  343. style_type_type = Style.fromstr('cyan:black')
  344. style_url = Style.fromstr('green:black')
  345. style_dir = Style.fromstr('cyan:black')
  346. style_file = Style.fromstr('green:black')
  347. style_ellisis = Style.fromstr('red:black')
  348. style_error = Style.fromstr('red:black')
  349. style_nodata = Style.fromstr('red:black')
  350.