ó
N_c           @   s·   d  Z  y d d l Z Wn e k
 r5 d d l Z n Xd e f d „  ƒ  YZ d d d d „ Z d	 „  Z d d
 „ Z	 d d „ Z
 e j d d k r› e
 Z n e	 Z d „  Z d „  Z d S(   s«   
utility functions to handle differences in pysqlite versions
These are from Wichert Akkerman <wichert@deephackmode.org>'s python-dhm
http://www.wiggy.net/code/python-dhm
iÿÿÿÿNt   TokenizeErrorc           B   s   e  Z d  Z RS(   s   Tokenizer error class(   t   __name__t
   __module__t   __doc__(    (    (    s0   /usr/lib/python2.7/site-packages/yum/sqlutils.pyR       s   s    	
s   "s   \c         C   s™  |  g  d d f \ } } } } y,x%| rH| d | k rC d } nø | d k rl | d | k rl | d } nÏ | d | k rÐ | d k r™ | | d 7} q;| j | ƒ d } xŒ | d | k rÌ | d } q¯ Wnk | d | k r| d k rù | d } n | | d 7} | d } n' | d k r-| d } n | | d 7} | d } q$ WWn t k
 rft d ‚ n X| ryt d ‚ n  | d k r•| j | ƒ n  | S(   s–  String tokenizer

    This function tokenizes a string while taking quotation and
    escaping into account.

      >>> import dhm.strtools
      >>> dhm.strtools.Tokenize("this is a test")
      ['this', 'is', 'a', 'test']
      >>> dhm.strtools.Tokenize("this "is a" test")
      ['this', 'is a', 'test']
      >>> dhm.strtools.Tokenize("this \"is\" a test")
      ['this', '"is"', 'a', 'test']
      >>> dhm.strtools.Tokenize("this "is a test")
      Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        File "/usr/local/lib/python2.2/site-packages/dhm/strtools.py", line 80, in Tokenize
          raise TokenizeError, "Unexpected end of string in quoted text"
      dhm.strtools.TokenizeError: Unexecpted end of string in quoted text

    @param        str: string to tokenize
    @type         str: string
    @param whitespace: whitespace characters separating tokens
    @type  whitespace: string
    @param     quotes: legal quoting characters
    @type      quotes: string
    @param    escapes: characters which can escape quoting characters
    @type     escapes: string
    @return: list of tokens
    @rtype:  sequence of strings
    i    i   s   Unexpected end of strings'   Unexpected end of string in quoted textN(   t   Nonet   appendt
   IndexErrorR    (   t   strt
   whitespacet   quotest   escapest   buffert   tokenst   curtokent   quote(    (    s0   /usr/lib/python2.7/site-packages/yum/sqlutils.pyt   Tokenize   s<    		c   	      C   s  t  |  d d ƒ} g  } d } x¨ | D]  } | j d ƒ r` | j | d  d | ƒ | d 7} q% | j d ƒ s~ | j d ƒ r¸ | d	  d | } | | d 7} | j | ƒ | d 7} q% | j | ƒ q% Wi  } d } x& | D] } | | d
 | <| d 7} qÜ Wd j | ƒ | f S(   sê  Convert from qmark to pyformat parameter style.

    The python DB-API 2.0 specifies four different possible parameter
    styles that can be used by drivers. This function converts from the
    qmark style to pyformat style.

    @param  query: SQL query to transform
    @type   query: string
    @param params: arguments to query
    @type  params: sequence of strings
    @return: converted query and parameters
    @rtype:  tuple with the new command and a dictionary of arguments
    R	   t   'i   t   ?iÿÿÿÿs   %%(param%d)ss   ?,s   ?)iþÿÿÿs   param%dt    (   R   t   endswithR   t   join(	   t   queryt   paramsR   t   outputt   countt   tokent   ntokent   dictt   param(    (    s0   /usr/lib/python2.7/site-packages/yum/sqlutils.pyt   QmarkToPyformatg   s&    c         C   sJ   | d k r |  j | ƒ S| j ƒ  } t | | ƒ \ } } |  j | | ƒ S(   sÐ   
    Execute a python < 2.5 (external sqlite module) style query.

    @param cursor: A sqlite cursor
    @param query: The query to execute
    @param params: An optional list of parameters to the query
    N(   R   t   executet   stripR   (   t   cursorR   R   t   qt   p(    (    s0   /usr/lib/python2.7/site-packages/yum/sqlutils.pyt   executeSQLPyFormat   s
    c         C   s)   | d k r |  j | ƒ S|  j | | ƒ S(   s¿   
    Execute a python 2.5 (sqlite3) style query.

    @param cursor: A sqlite cursor
    @param query: The query to execute
    @param params: An optional list of parameters to the query
    N(   R   R   (   R    R   R   (    (    s0   /usr/lib/python2.7/site-packages/yum/sqlutils.pyt   executeSQLQmark   s    i    i   c         C   sg   d } d |  k s d |  k r] d } |  j  d d ƒ }  |  j  d d ƒ }  |  j  d d ƒ }  n  |  | f S(	   s<    Apply SQLite escaping, if needed. Returns pattern and esc. t    t   _t   %s    ESCAPE "!"t   !s   !!s   !%s   !_(   t   replace(   t   patternt   esc(    (    s0   /usr/lib/python2.7/site-packages/yum/sqlutils.pyt   sql_esc°   s    c         C   st   g  } xg |  D]_ } d | k r# g  St  | ƒ \ } } | j d d ƒ } | j d d ƒ } | j | | f ƒ q W| S(   sZ    Converts patterns to SQL LIKE format, if required (or gives up if
        not possible). t   [t   *R'   R   R&   (   R,   R)   R   (   t   patternst   retR*   R+   (    (    s0   /usr/lib/python2.7/site-packages/yum/sqlutils.pyt   sql_esc_globº   s    (   R   t   sqlite3t   sqlitet   ImportErrort	   ExceptionR    R   R   R   R#   R$   t   version_infot
   executeSQLR,   R1   (    (    (    s0   /usr/lib/python2.7/site-packages/yum/sqlutils.pyt   <module>   s   H	&		
