
,]c           @   st   d  Z  d d l Z d Z d Z e d  Z e e d d  Z e d e e d  Z d   Z	 e
 d	 k rp e	   n  d S(
   sK   
String search and match utilities usefull when filtering a list of texts.
iNi    c         C   st   g  |  D] } | d k r | ^ q } d j  |  } d j |  } | ra t j | t j  } n t j |  } | S(   s  Returns a compiled regex pattern to search for query letters in order.

    Parameters
    ----------
    query : str
        String to search in another string (in order of character occurrence).
    ignore_case : True
        Optional value perform a case insensitive search (True by default).

    Returns
    -------
    pattern : SRE_Pattern

    Notes
    -----
    This function adds '.*' between the query characters and compiles the
    resulting regular expression.
    t    s   .*s   ({0})(   t   joint   formatt   ret   compilet
   IGNORECASE(   t   queryt   ignore_caset   chart
   regex_textt   regext   pattern(    (    s:   lib/python2.7/site-packages/spyder/utils/stringmatching.pyt   get_search_regex   s    %s   {}c         C   s  | } | t  f } |  s | S| r= |  j   }  | j   } n  | rz t |  d | } t j | |  } | d k r| Snfd }	 d }
 d } g  | j d  D] } |  | k ^ q } g  | j d  D] } |  | k ^ q } t |  s t |  re| j |   } | t	 |   } | | 7} | j
 |  |	 t	 |   d  } | |  | j | | | ! | | } n  t |  r~| d 7} n t |  r| d 7} n g  | D] } | ^ q} | rg  | D] } | j   ^ q} n | } | | j |  d  7} | } xv |  D]n } | d k r| | k r| j |  } | j | |  | | <|	 | | <d g | d | | d } qqWd j |  } g  } xN t |  D]@ \ } } | d k r| |	 k r|
 } n | } | j |  qWd j |  } xL t t d t	 |   d   D]+ } | t	 |   | j |	 |  d	 7} qW| j |	  } x d | k rh| j d  qLW| j |	  s| d } n  | j |	  s| d
  } n  x< | D]4 } | | j d  d 7} | | j |
  d 7} qW| | | f S(   s  Returns a tuple with the enriched text (if a template is provided) and
    a score for the match.

    Parameters
    ----------
    query : str
        String with letters to search in choice (in order of appearance).
    choice : str
        Sentence/words in which to search for the 'query' letters.
    ignore_case : bool, optional
        Optional value perform a case insensitive search (True by default).
    apply_regex : bool, optional
        Optional value (True by default) to perform a regex search. Useful
        when this function is called directly.
    template : str, optional
        Optional template string to surround letters found in choices. This is
        useful when using a rich text editor ('{}' by default).
        Examples: '<b>{}</b>', '<code>{}</code>', '<i>{}</i>'

    Returns
    -------
    results : tuple
        Tuples where the first item is the text (enriched if a template was
        used) and the second item is a search score.

    Notes
    -----
    The score is given according the following precedence (high to low):

    - Letters in one word and no spaces with exact match.
      Example: 'up' in 'up stroke'
    - Letters in one word and no spaces with partial match.
      Example: 'up' in 'upstream stroke'
    - Letters in one word but with skip letters.
      Example: 'cls' in 'close up'
    - Letters in two or more words
      Example: 'cls' in 'car lost'
    R   u   -u   xi    u    i   id   u    i ii'  N(   t   NOT_FOUND_SCOREt   lowerR   R   t   searcht   Nonet   splitt   anyt   findt   lent   replaceR   t   indexR   t	   enumeratet   appendt   reversedt   ranget   countt   removet
   startswitht   endswith(   R   t   choiceR   t   apply_regext   templatet   original_choicet   resultR   t   rt   sept   lett   scoret   wordt   exact_wordst   partial_wordst	   pos_startt   pos_endt   textt   enriched_textt   lt	   temp_textR   R   t   patterns_textt   it   new_chart   tempt   pat(    (    s:   lib/python2.7/site-packages/spyder/utils/stringmatching.pyt   get_search_score2   sz    (((
"
$	&)c      
   C   s   |  j  d d  }  t |  |  } g  } x | D] } t j | |  }	 |  rv |	 rv t |  | d | d t d | }
 n' |  r | | t f }
 n | | t f }
 | r |
 d t k r | j |
  q q. | j |
  q. W| r t	 | d d   } n  | S(	   s  Search for query inside choices and return a list of tuples.

    Returns a list of tuples of text with the enriched text (if a template is
    provided) and a score for the match. Lower scores imply a better match.

    Parameters
    ----------
    query : str
        String with letters to search in each choice (in order of appearance).
    choices : list of str
        List of sentences/words in which to search for the 'query' letters.
    ignore_case : bool, optional
        Optional value perform a case insensitive search (True by default).
    template : str, optional
        Optional template string to surround letters found in choices. This is
        useful when using a rich text editor ('{}' by default).
        Examples: '<b>{}</b>', '<code>{}</code>', '<i>{}</i>'

    Returns
    -------
    results : list of tuples
        List of tuples where the first item is the text (enriched if a
        template was used) and a search score. Lower scores means better match.
    R    t    R   R    R!   it   keyc         S   s   |  d S(   Ni(    (   t   row(    (    s:   lib/python2.7/site-packages/spyder/utils/stringmatching.pyt   <lambda>   R7   (
   R   R   R   R   R6   t   FalseR   t   NO_SCORER   t   sorted(   R   t   choicesR   R!   t
   valid_onlyt   sortR   t   resultsR   R$   R#   (    (    s:   lib/python2.7/site-packages/spyder/utils/stringmatching.pyt   get_search_scores   s$    c       _   C   s  d }  d d d d d d d d	 d
 d d d d d d d d d d d d d d d d d d d d d d  d! d" d# d$ d% d& d' d( d) d* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d  d9 d: d; d< d= d> d? d@ dA dB dC dD dE dF dG dH dI dJ dK dL dM dN dO dP dQ dR dS dT dU dV dW dX dY dZ d[ d\ d] d^ d_ g_ } t  d` | da |  } t  d` | da |  db t dc t } x | D] } | GHqfWdd GHx | D] } | GHqWd  S(e   Ns
   <b>{0}</b>s
   close panes   debug continues
   debug exits   debug step intos   debug step overs   debug step returns   fullscreen modes   layout preferencess   lock unlock paness   maximize panet   preferencest   quitt   restarts   save current layouts   switch to breakpointss   switch to consoles   switch to editors   switch to explorers   switch to find_in_filess   switch to historylogs   switch to helps   switch to ipython_consoles   switch to onlinehelps   switch to outline_explorers   switch to project_explorers   switch to variable_explorers   use next layouts   use previous layouts
   clear lines   clear shells   inspect current objectt   blockcommentt
   breakpoints	   close alls   code completions   conditional breakpointt	   configuret   copys	   copy linet   cutt   debugs   debug with winpdbt   deletes   delete lines   duplicate lines   end of documents   end of lines   file list managements	   find nexts   find previouss	   find texts   go to definitions
   go to lines   go to next files   go to previous files   kill next words   kill previous words   kill to line ends   kill to line starts   last edit locations   move line downs   move line ups   new files	   next chars   next cursor positions	   next lines	   next words	   open filet   pastes   previous chars   previous cursor positions   previous lines   previous wordt   prints   re-run last scriptt   redos   replace texts   rotate kill ringt   runs   run selections   save alls   save ass	   save files
   select alls   show/hide outlines   show/hide project explorers   start of documents   start of lines   toggle commentt   unblockcommentt   undot   yanks   run profilers   run analysist   layR!   R?   R@   s   
(   RB   t   True(   R!   t   namest   at   bR$   (    (    s:   lib/python2.7/site-packages/spyder/utils/stringmatching.pyt   test   sN    															t   __main__(   t   __doc__R   R   R<   RU   R   R6   R;   RB   RY   t   __name__(    (    (    s:   lib/python2.7/site-packages/spyder/utils/stringmatching.pyt   <module>	   s    5	.