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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import clr
  5. import sys
  6. clr.AddReferenceToFileAndPath(sys.executable)
  7. import IronPythonConsole
  8. import sys
  9. import re
  10. import os
  11. import System
  12. from event import Event
  13. from pyreadline.logger import log, log_sock
  14. from pyreadline.keysyms import make_keysym, make_keyinfo, make_KeyPress, make_KeyPress_from_keydescr
  15. from pyreadline.console.ansi import AnsiState
  16. color = System.ConsoleColor
  17. ansicolor = {
  18.     '0;30': color.Black,
  19.     '0;31': color.DarkRed,
  20.     '0;32': color.DarkGreen,
  21.     '0;33': color.DarkYellow,
  22.     '0;34': color.DarkBlue,
  23.     '0;35': color.DarkMagenta,
  24.     '0;36': color.DarkCyan,
  25.     '0;37': color.DarkGray,
  26.     '1;30': color.Gray,
  27.     '1;31': color.Red,
  28.     '1;32': color.Green,
  29.     '1;33': color.Yellow,
  30.     '1;34': color.Blue,
  31.     '1;35': color.Magenta,
  32.     '1;36': color.Cyan,
  33.     '1;37': color.White }
  34. winattr = {
  35.     'black': 0,
  36.     'darkgray': 0 + 8,
  37.     'darkred': 4,
  38.     'red': 4 + 8,
  39.     'darkgreen': 2,
  40.     'green': 2 + 8,
  41.     'darkyellow': 6,
  42.     'yellow': 6 + 8,
  43.     'darkblue': 1,
  44.     'blue': 1 + 8,
  45.     'darkmagenta': 5,
  46.     'magenta': 5 + 8,
  47.     'darkcyan': 3,
  48.     'cyan': 3 + 8,
  49.     'gray': 7,
  50.     'white': 7 + 8 }
  51.  
  52. class Console(object):
  53.     
  54.     def __init__(self, newbuffer = 0):
  55.         self.serial = 0
  56.         self.attr = System.Console.ForegroundColor
  57.         self.saveattr = winattr[str(System.Console.ForegroundColor).lower()]
  58.         self.savebg = System.Console.BackgroundColor
  59.         log('initial attr=%s' % self.attr)
  60.         log_sock('%s' % self.saveattr)
  61.  
  62.     
  63.     def _get(self):
  64.         top = System.Console.WindowTop
  65.         log_sock('WindowTop:%s' % top, 'console')
  66.         return top
  67.  
  68.     
  69.     def _set(self, value):
  70.         top = System.Console.WindowTop
  71.         log_sock('Set WindowTop:old:%s,new:%s' % (top, value), 'console')
  72.  
  73.     WindowTop = property(_get, _set)
  74.     del _get
  75.     del _set
  76.     
  77.     def __del__(self):
  78.         pass
  79.  
  80.     
  81.     def pos(self, x = None, y = None):
  82.         if x is not None:
  83.             System.Console.CursorLeft = x
  84.         else:
  85.             x = System.Console.CursorLeft
  86.         if y is not None:
  87.             System.Console.CursorTop = y
  88.         else:
  89.             y = System.Console.CursorTop
  90.         return (x, y)
  91.  
  92.     
  93.     def home(self):
  94.         self.pos(0, 0)
  95.  
  96.     terminal_escape = re.compile('(\x01?\x1b\\[[0-9;]*m\x02?)')
  97.     escape_parts = re.compile('\x01?\x1b\\[([0-9;]*)m\x02?')
  98.     motion_char_re = re.compile('([\n\r\t\x08\x07])')
  99.     
  100.     def write_scrolling(self, text, attr = None):
  101.         (x, y) = self.pos()
  102.         (w, h) = self.size()
  103.         scroll = 0
  104.         chunks = self.motion_char_re.split(text)
  105.         for chunk in chunks:
  106.             log('C:' + chunk)
  107.             n = self.write_color(chunk, attr)
  108.             if len(chunk) == 1:
  109.                 if chunk[0] == '\n':
  110.                     x = 0
  111.                     y += 1
  112.                 elif chunk[0] == '\r':
  113.                     x = 0
  114.                 elif chunk[0] == '\t':
  115.                     x = 8 * (int(x / 8) + 1)
  116.                     if x > w:
  117.                         x -= w
  118.                         y += 1
  119.                     
  120.                 elif chunk[0] == '\x07':
  121.                     pass
  122.                 elif chunk[0] == '\x08':
  123.                     x -= 1
  124.                     if x < 0:
  125.                         y -= 1
  126.                     
  127.                 else:
  128.                     x += 1
  129.                 if x == w:
  130.                     x = 0
  131.                     y += 1
  132.                 
  133.                 if y == h:
  134.                     scroll += 1
  135.                     y = h - 1
  136.                 
  137.             y == h
  138.             x += n
  139.             l = int(x / w)
  140.             x = x % w
  141.             y += l
  142.             if y >= h:
  143.                 scroll += (y - h) + 1
  144.                 y = h - 1
  145.                 continue
  146.         
  147.         return scroll
  148.  
  149.     trtable = {
  150.         0: color.Black,
  151.         4: color.DarkRed,
  152.         2: color.DarkGreen,
  153.         6: color.DarkYellow,
  154.         1: color.DarkBlue,
  155.         5: color.DarkMagenta,
  156.         3: color.DarkCyan,
  157.         7: color.Gray,
  158.         8: color.DarkGray,
  159.         12: color.Red,
  160.         10: color.Green,
  161.         14: color.Yellow,
  162.         9: color.Blue,
  163.         13: color.Magenta,
  164.         11: color.Cyan,
  165.         15: color.White }
  166.     
  167.     def write_color(self, text, attr = None):
  168.         log('write_color("%s", %s)' % (text, attr))
  169.         chunks = self.terminal_escape.split(text)
  170.         log('chunks=%s' % repr(chunks))
  171.         bg = self.savebg
  172.         n = 0
  173.         if attr is None:
  174.             attr = self.attr
  175.         
  176.         
  177.         try:
  178.             fg = self.trtable[15 & attr]
  179.             bg = self.trtable[(240 & attr) >> 4]
  180.         except TypeError:
  181.             fg = attr
  182.  
  183.         for chunk in chunks:
  184.             m = self.escape_parts.match(chunk)
  185.             if m:
  186.                 log(m.group(1))
  187.                 attr = ansicolor.get(m.group(1), self.attr)
  188.             
  189.             n += len(chunk)
  190.             System.Console.ForegroundColor = fg
  191.             System.Console.BackgroundColor = bg
  192.             System.Console.Write(chunk)
  193.         
  194.         return n
  195.  
  196.     
  197.     def write_plain(self, text, attr = None):
  198.         log('write("%s", %s)' % (text, attr))
  199.         if attr is None:
  200.             attr = self.attr
  201.         
  202.         n = c_int(0)
  203.         self.SetConsoleTextAttribute(self.hout, attr)
  204.         self.WriteConsoleA(self.hout, text, len(text), byref(n), None)
  205.         return len(text)
  206.  
  207.     if os.environ.has_key('EMACS'):
  208.         
  209.         def write_color(self, text, attr = None):
  210.             junk = c_int(0)
  211.             self.WriteFile(self.hout, text, len(text), byref(junk), None)
  212.             return len(text)
  213.  
  214.         write_plain = write_color
  215.     
  216.     
  217.     def write(self, text):
  218.         log('write("%s")' % text)
  219.         return self.write_color(text)
  220.  
  221.     
  222.     def isatty(self):
  223.         return True
  224.  
  225.     
  226.     def flush(self):
  227.         pass
  228.  
  229.     
  230.     def page(self, attr = None, fill = ' '):
  231.         System.Console.Clear()
  232.  
  233.     
  234.     def text(self, x, y, text, attr = None):
  235.         self.pos(x, y)
  236.         self.write_color(text, attr)
  237.  
  238.     
  239.     def clear_to_end_of_window(self):
  240.         oldtop = self.WindowTop
  241.         lastline = self.WindowTop + System.Console.WindowHeight
  242.         pos = self.pos()
  243.         (w, h) = self.size()
  244.         length = (w - pos[0]) + min(lastline - pos[1] - 1, 5) * w - 1
  245.         self.write_color(length * ' ')
  246.         self.pos(*pos)
  247.         self.WindowTop = oldtop
  248.  
  249.     
  250.     def rectangle(self, rect, attr = None, fill = ' '):
  251.         oldtop = self.WindowTop
  252.         oldpos = self.pos()
  253.         (x0, y0, x1, y1) = rect
  254.         if attr is None:
  255.             attr = self.attr
  256.         
  257.         if fill:
  258.             rowfill = fill[:1] * abs(x1 - x0)
  259.         else:
  260.             rowfill = ' ' * abs(x1 - x0)
  261.         for y in range(y0, y1):
  262.             System.Console.SetCursorPosition(x0, y)
  263.             self.write_color(rowfill, attr)
  264.         
  265.         self.pos(*oldpos)
  266.  
  267.     
  268.     def scroll(self, rect, dx, dy, attr = None, fill = ' '):
  269.         raise NotImplementedError
  270.  
  271.     
  272.     def scroll_window(self, lines):
  273.         top = self.WindowTop + lines
  274.         if top < 0:
  275.             top = 0
  276.         
  277.         if top + System.Console.WindowHeight > System.Console.BufferHeight:
  278.             top = System.Console.BufferHeight
  279.         
  280.         self.WindowTop = top
  281.  
  282.     
  283.     def getkeypress(self):
  284.         ck = System.ConsoleKey
  285.         while None:
  286.             e = System.Console.ReadKey(True)
  287.             if e.Key == System.ConsoleKey.PageDown:
  288.                 self.scroll_window(12)
  289.                 continue
  290.             if e.Key == System.ConsoleKey.PageUp:
  291.                 self.scroll_window(-12)
  292.                 continue
  293.             if str(e.KeyChar) == '\x00':
  294.                 log_sock('Deadkey: %s' % e)
  295.                 return event(self, e)
  296.             return event(self, e)
  297.             continue
  298.             return None
  299.  
  300.     
  301.     def title(self, txt = None):
  302.         if txt:
  303.             System.Console.Title = txt
  304.         else:
  305.             return System.Console.Title
  306.         return txt
  307.  
  308.     
  309.     def size(self, width = None, height = None):
  310.         sc = System.Console
  311.         if width is not None and height is not None:
  312.             sc.BufferWidth = width
  313.             sc.BufferHeight = height
  314.         else:
  315.             return (sc.BufferWidth, sc.BufferHeight)
  316.         if height is not None is not None and height is not None:
  317.             sc.WindowWidth = width
  318.             sc.WindowHeight = height
  319.         else:
  320.             return (sc.WindowWidth - 1, sc.WindowHeight - 1)
  321.         return height is not None
  322.  
  323.     
  324.     def cursor(self, visible = True, size = None):
  325.         System.Console.CursorVisible = visible
  326.  
  327.     
  328.     def bell(self):
  329.         System.Console.Beep()
  330.  
  331.     
  332.     def next_serial(self):
  333.         self.serial += 1
  334.         return self.serial
  335.  
  336.  
  337.  
  338. class event(Event):
  339.     
  340.     def __init__(self, console, input):
  341.         self.type = '??'
  342.         self.serial = console.next_serial()
  343.         self.width = 0
  344.         self.height = 0
  345.         self.x = 0
  346.         self.y = 0
  347.         self.char = str(input.KeyChar)
  348.         self.keycode = input.Key
  349.         self.state = input.Modifiers
  350.         log_sock('%s,%s,%s' % (input.Modifiers, input.Key, input.KeyChar), 'console')
  351.         self.type = 'KeyRelease'
  352.         self.keysym = make_keysym(self.keycode)
  353.         self.keyinfo = make_KeyPress(self.char, self.state, self.keycode)
  354.  
  355.  
  356.  
  357. def make_event_from_keydescr(keydescr):
  358.     
  359.     def input():
  360.         return 1
  361.  
  362.     input.KeyChar = 'a'
  363.     input.Key = System.ConsoleKey.A
  364.     input.Modifiers = System.ConsoleModifiers.Shift
  365.     input.next_serial = input
  366.     e = event(input, input)
  367.     del input.next_serial
  368.     keyinfo = make_KeyPress_from_keydescr(keydescr)
  369.     e.keyinfo = keyinfo
  370.     return e
  371.  
  372. CTRL_C_EVENT = make_event_from_keydescr('Control-c')
  373.  
  374. def install_readline(hook):
  375.     
  376.     def hook_wrap():
  377.         
  378.         try:
  379.             res = hook()
  380.         except KeyboardInterrupt:
  381.             x = None
  382.             res = ''
  383.         except EOFError:
  384.             return None
  385.  
  386.         if res[-1:] == '\n':
  387.             return res[:-1]
  388.         return res
  389.  
  390.     
  391.     class IronPythonWrapper((IronPythonConsole.IConsole,)):
  392.         
  393.         def ReadLine(self, autoIndentSize):
  394.             return hook_wrap()
  395.  
  396.         
  397.         def Write(self, text, style):
  398.             System.Console.Write(text)
  399.  
  400.         
  401.         def WriteLine(self, text, style):
  402.             System.Console.WriteLine(text)
  403.  
  404.  
  405.     IronPythonConsole.PythonCommandLine.MyConsole = IronPythonWrapper()
  406.  
  407. if __name__ == '__main__':
  408.     import time
  409.     import sys
  410.     c = Console(0)
  411.     sys.stdout = c
  412.     sys.stderr = c
  413.     c.page()
  414.     c.pos(5, 10)
  415.     c.write('hi there')
  416.     c.title('Testing console')
  417.     print 
  418.     print 'size', c.size()
  419.     print '  some printed output'
  420.     for i in range(10):
  421.         e = c.getkeypress()
  422.         print e.Key, chr(e.KeyChar), ord(e.KeyChar), e.Modifiers
  423.     
  424.     del c
  425.     System.Console.Clear()
  426.  
  427.