ó
_y]c           @   s`  d  Z  d d l m Z d d l Z d d l m Z d d l m Z d d l m	 Z	 e d d d	 d
 g ƒ Z
 e d d d d d g ƒ Z e d d d d d g ƒ Z e d d g ƒ Z e d d g ƒ Z e d d g ƒ Z e d d g ƒ Z d Z d e Z d Z d e e f Z e j d e ƒ Z e j d ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   sJ    Utilities for processing ANSI escape codes and special ASCII characters.
iÿÿÿÿ(   t
   namedtupleN(   t   QtGui(   t   string_types(   t
   dark_stylet   EraseActiont   actiont   areat   erase_tot
   MoveActiont   dirt   unitt   countt   ScrollActiont   CarriageReturnActiont   NewLineActiont
   BeepActiont   BackSpaceActiont   ABCDEFGHJKSTfmnsus   \[(.*?)([%s])s   \](.*?)[]s   ?(%s|%s)?s   (||(?!
)|?
)|(?:%s)s   ([])t   AnsiCodeProcessorc           B   se   e  Z d  Z e Z i  Z d „  Z d „  Z d „  Z g  d „ Z	 d „  Z
 d „  Z d „  Z d „  Z RS(	   sœ    Translates special ASCII characters and ANSI escape codes into readable
        attributes. It also supports a few non-standard, xterm-specific codes.
    c         C   s)   g  |  _  |  j j ƒ  |  _ |  j ƒ  d  S(   N(   t   actionst   default_color_mapt   copyt	   color_mapt	   reset_sgr(   t   self(    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyt   __init__K   s    	c         C   s:   d |  _  t |  _ t |  _ t |  _ d |  _ d |  _ d S(   s;    Reset graphics attributs to their default values.
        i    N(   t	   intensityt   Falset   italict   boldt	   underlinet   Nonet   foreground_colort   background_color(   R   (    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyR   P   s    					c         c   sÖ  g  |  _  d } t | ƒ d k r7 | d d k r7 d n d } | d k	 rS | d  n | } xt j | ƒ D]} | | | j ƒ  !} t j |  j | ƒ } | s¦ |  j  r· | Vg  |  _  n  | j	 ƒ  } g  | j
 ƒ  D] } | d k	 rÐ | ^ qÐ } | d }	 |	 d k r+|  j  j t d ƒ ƒ d Vg  |  _  qi |	 d k r^|  j  j t d ƒ ƒ d Vg  |  _  qi |	 d k r‘|  j  j t d	 ƒ ƒ d Vg  |  _  qi |	 d k s©|	 d
 k rÐ|  j  j t d ƒ ƒ |	 Vg  |  _  qi g  | d j d ƒ D] }
 |
 rä|
 ^ qä} |	 j d ƒ rOy t t t | ƒ ƒ } Wn t k
 r7qnX|  j | d | ƒ qi |	 j d ƒ ri |  j | ƒ qi qi W| | } t j |  j | ƒ } | s |  j  r¨| Vn  | d k	 rÒ|  j  j t d ƒ ƒ | Vn  d S(   sC    Yields substrings for which the same escape code applies.
        i    iÿÿÿÿs   
s   t   beeps   s   carriage-returns   t	   backspaces   
t   newlinei   t   ;t   [i   t   ]N(   R   t   lenR   t   ANSI_OR_SPECIAL_PATTERNt   finditert   startt   SPECIAL_PATTERNt   subt   _replace_specialt   endt   groupst   appendR   R   R   R   t   splitt
   startswitht   listt   mapt   intt
   ValueErrort   set_csi_codet   set_osc_code(   R   t   stringR+   t	   last_chart   matcht   rawt	   substringt   gR0   t   g0t   paramt   params(    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyt   split_stringZ   sZ    	.+
,
c         C   sd  | d k r5 | r" |  j  | ƒ q`|  j  d g ƒ n+| d k sM | d k rø | r] | d n d } d | k oz d k n r`| d k r‘ d n d } | d k r¬ d } n* | d	 k rÁ d
 } n | d k rÖ d } n  |  j j t d | | ƒ ƒ q`nh | d k s| d k r`| d k r"d n d } | r8| d n d	 } |  j j t d | d | ƒ ƒ n  d S(   s3   Set attributes based on CSI (Control Sequence Introducer) code.

        Parameters
        ----------
        command : str
            The code identifier, i.e. the final character in the sequence.

        params : sequence of integers, optional
            The parameter codes for the command.
        t   mi    t   Jt   Ki   t   screent   lineR/   i   R+   t   allt   eraset   St   Tt   upt   downt   scrollN(   t   set_sgr_codeR   R1   R   R   (   R   t   commandRB   t   codeR   R   R	   R   (    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyR8   ™   s*    			"c         C   sŸ   y t  | j d ƒ ƒ } Wn t t f k
 r3 d SX| d k r› y> t  | j d ƒ ƒ } | j d ƒ } |  j | ƒ |  j | <Wq› t t f k
 r— q› Xn  d S(   sÀ    Set attributes based on OSC (Operating System Command) parameters.

        Parameters
        ----------
        params : sequence of str
            The parameters for the command.
        i    Ni   (   R6   t   popt
   IndexErrorR7   t   _parse_xterm_color_specR   (   R   RB   RQ   t   colort   spec(    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyR9   ½   s    c         C   sk  | s
 d S| j  d ƒ } | d k r2 |  j ƒ  n(| d k r_ |  j rS t |  _ qZd |  _ nû| d k rw d |  _ nã| d k r t |  _ nË| d k r§ t |  _ n³| d k rÈ d |  _ t |  _ n’| d k rà t |  _ nz| d	 k rø t |  _ nb| d
 k r | d k r | d
 |  _	 n:| d k r‘| r‘| j  d ƒ } | d k rh| rh| j  d ƒ |  _	 qZ| d k rZ| d  |  _	 g  | d *qZnÉ | d k r©d |  _	 n± | d k rÑ| d k rÑ| d |  _ n‰ | d k rB| rB| j  d ƒ } | d k r| r| j  d ƒ |  _ qZ| d k rZ| d  |  _ g  | d *qZn | d k rZd |  _ n  |  j | ƒ d S(   sd   Set attributes based on SGR (Select Graphic Rendition) codes.

        Parameters
        ----------
        params : sequence of ints
            A list of SGR codes for one or more SGR commands. Usually this
            sequence will have one element per command, although certain
            xterm-specific commands requires multiple elements.
        Ni    i   i   i   i   i   i   i   i   i%   i&   i   i'   i(   i/   i0   i1   (   RS   R   t   bold_text_enabledt   TrueR   R   R   R   R   R    R   R!   RP   (   R   RB   RR   t   _color_type(    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyRP   Ó   sZ    		c         C   sƒ   | j  d ƒ r2 t t d „  | d j d ƒ ƒ ƒ S| j  d ƒ rd t t d „  | d j d ƒ ƒ ƒ S| d k r t d	 ƒ ‚ n  | S(
   Ns   rgb:c         S   s   t  |  d ƒ S(   Ni   (   R6   (   t   x(    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyt   <lambda>  t    i   t   /s   rgbi:c         S   s   t  t |  ƒ d ƒ S(   Niÿ   (   R6   t   float(   R[   (    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyR\     R]   i   t   ?s   Unsupported xterm color spec(   R3   t   tupleR5   R2   R7   (   R   RW   (    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyRU     s    #c         C   sA   | j  d ƒ } | d k r= |  j j t d d d d ƒ ƒ n  d S(   Ni   s   RO   RN   t   pageR]   (   t   groupR   R1   R   (   R   R<   t   special(    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyR.   "  s    "(   t   __name__t
   __module__t   __doc__R   RX   R   R   R   RC   R8   R9   RP   RU   R.   (    (    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyR   :   s   		
	?$		E	
t   QtAnsiCodeProcessorc           B   s®   e  Z d  Z i d d 6d d 6d d 6d d 6d	 d
 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6Z e j ƒ  Z d d  „ Z d! „  Z d" „  Z RS(#   s9    Translates ANSI escape codes into QTextCharFormats.
    t   blacki    t   darkredi   t	   darkgreeni   t   browni   t   darkbluei   t
   darkvioleti   t	   steelbluei   t   greyi   i   t   redi	   t   limei
   t   yellowi   t   deepskybluei   t   magentai   t   cyani   t   whitei   c         C   s°   t  | t ƒ rL | d k  r4 | d k r4 | d 7} n  |  j j | d ƒ } n" t  | t t f ƒ rj | } n d St  | t ƒ rŠ t j	 | ƒ St  | t t f ƒ r¬ t j	 | Œ  Sd S(   sp    Returns a QColor for a given color code or rgb list, or None if one
            cannot be constructed.
        i   i    N(
   t
   isinstanceR6   R   t   getR   Ra   R4   R   R   t   QColor(   R   RV   R   t   constructor(    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyt	   get_colorC  s    	c         C   sÊ   t  j ƒ  } |  j |  j |  j ƒ } | d k	 r@ | j | ƒ n  |  j |  j |  j ƒ } | d k	 rt | j | ƒ n  |  j	 r“ | j
 t  j j ƒ n | j
 t  j j ƒ | j |  j ƒ | j |  j ƒ | S(   sN    Returns a QTextCharFormat that encodes the current style attributes.
        N(   R   t   QTextCharFormatR|   R    R   R   t   setForegroundR!   t   setBackgroundR   t   setFontWeightt   QFontt   Boldt   Normalt   setFontItalicR   t   setFontUnderlineR   (   R   t   formatt   qcolor(    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyt
   get_format^  s    	c         C   s   |  j  j ƒ  |  _ t | ƒ sh x, t d ƒ D] } |  j | |  j | d <q+ Wd |  j d <|  j d <n  |  j j |  j ƒ d S(   so   
        Given a syntax style, attempt to set a color map that will be
        aesthetically pleasing.
        i   Ri   i   i   N(   t   darkbg_color_mapR   R   R   t   rangeR   t   update(   R   t   stylet   i(    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyt   set_background_colorw  s    (	   Re   Rf   Rg   R‰   R   R   R|   Rˆ   RŽ   (    (    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyRh   )  s,   
	(   Rg   t   collectionsR    t   ret   qtconsole.qtR   t   ipython_genutils.py3compatR   t   qtconsole.stylesR   R   R   R   R   R   R   R   t   CSI_COMMANDSt   CSI_SUBPATTERNt   OSC_SUBPATTERNt   ANSI_PATTERNt   compileR)   R,   t   objectR   Rh   (    (    (    s<   lib/python2.7/site-packages/qtconsole/ansi_code_processor.pyt   <module>   s*   
ï