ó
º9—^c        	   @   s?  d  Z  d d l Z d d l m Z m Z d d l m Z m Z m Z d d l m	 Z	 m
 Z
 m Z d d l m Z d e f d „  ƒ  YZ e d	 „ Z d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e e e ƒ e e e d d d d d d ƒZ e j ƒ  e e e d d d d d d  ƒZ e j ƒ  e e e d d! d d" d d# ƒZ e j ƒ  e d$ e d d% d d& d d' ƒZ e j ƒ  e d( e d d) d d* d d+ ƒZ e j ƒ  e d, e d d- d d. d d/ ƒZ e j ƒ  d S(0   s1   Implementation of the Range type and adaptation

iÿÿÿÿN(   t   ProgrammingErrort   InterfaceError(   t	   ISQLQuotet   adaptt   register_adapter(   t   new_typet   new_array_typet   register_type(   t   string_typest   Rangec           B   s  e  Z d  Z d Z d d d e d „ Z d „  Z d „  Z e	 d „  ƒ Z
 e	 d	 „  ƒ Z e	 d
 „  ƒ Z e	 d „  ƒ Z e	 d „  ƒ Z e	 d „  ƒ Z e	 d „  ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s…  Python representation for a PostgreSQL |range|_ type.

    :param lower: lower bound for the range. `!None` means unbound
    :param upper: upper bound for the range. `!None` means unbound
    :param bounds: one of the literal strings ``()``, ``[)``, ``(]``, ``[]``,
        representing whether the lower or upper bounds are included
    :param empty: if `!True`, the range is empty

    t   _lowert   _uppert   _boundss   [)c         C   s^   | sC | d k r% t  d | ƒ ‚ n  | |  _ | |  _ | |  _ n d  |  _ |  _ |  _ d  S(   Ns   [)s   (]s   ()s   []s   bound flags not valid: %r(   s   [)s   (]s   ()s   [](   t
   ValueErrorR
   R   R   t   None(   t   selft   lowert   uppert   boundst   empty(    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __init__/   s    		c         C   sD   |  j  d  k r d |  j j Sd |  j j |  j |  j |  j  f Sd  S(   Ns   %s(empty=True)s   %s(%r, %r, %r)(   R   R   t	   __class__t   __name__R
   R   (   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __repr__:   s    c         C   sU   |  j  d  k r d S|  j  d t |  j ƒ d t |  j ƒ |  j  d g } d j | ƒ S(   NR   i    s   , i   t    (   R   R   t   strR
   R   t   join(   R   t   items(    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __str__A   s    
c         C   s   |  j  S(   s:   The lower bound of the range. `!None` if empty or unbound.(   R
   (   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyR   N   s    c         C   s   |  j  S(   s:   The upper bound of the range. `!None` if empty or unbound.(   R   (   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyR   S   s    c         C   s   |  j  d k S(   s   `!True` if the range is empty.N(   R   R   (   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   isemptyX   s    c         C   s    |  j  d k r t S|  j d k S(   s0   `!True` if the range doesn't have a lower bound.N(   R   R   t   FalseR
   (   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt	   lower_inf]   s    c         C   s    |  j  d k r t S|  j d k S(   s1   `!True` if the range doesn't have an upper bound.N(   R   R   R   R   (   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt	   upper_infd   s    c         C   s3   |  j  d k s |  j d k r" t S|  j  d d k S(   s4   `!True` if the lower bound is included in the range.i    t   [N(   R   R   R
   R   (   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt	   lower_inck   s    c         C   s3   |  j  d k s |  j d k r" t S|  j  d d k S(   s4   `!True` if the upper bound is included in the range.i   t   ]N(   R   R   R   R   (   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt	   upper_incr   s    c         C   s³   |  j  d  k r t S|  j d  k	 ra |  j  d d k rK | |  j k  r^ t Sqa | |  j k ra t Sn  |  j d  k	 r¯ |  j  d d k r™ | |  j k r¬ t Sq¯ | |  j k r¯ t Sn  t S(   Ni    R!   i   R#   (   R   R   R   R
   R   t   True(   R   t   x(    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __contains__y   s    c         C   s   |  j  d  k	 S(   N(   R   R   (   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __bool__   s    c         C   s   t  |  ƒ j |  ƒ S(   N(   t   typeR(   (   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __nonzero__’   s    c         C   sG   t  | t ƒ s t S|  j | j k oF |  j | j k oF |  j | j k S(   N(   t
   isinstanceR	   R   R
   R   R   (   R   t   other(    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __eq__–   s
    c         C   s   |  j  | ƒ S(   N(   R-   (   R   R,   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __ne__   s    c         C   s   t  |  j |  j |  j f ƒ S(   N(   t   hashR
   R   R   (   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __hash__    s    c         C   s   t  | t ƒ s t Sxe d D]] } t |  | ƒ } t | | ƒ } | | k rM q | d  k r] t S| d  k rm t S| | k  Sq Wt S(   NR
   R   R   (   R
   R   R   (   R+   R	   t   NotImplementedt   getattrR   R%   R   (   R   R,   t   attrt
   self_valuet   other_value(    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __lt__§   s    c         C   s!   |  | k r t  S|  j | ƒ Sd  S(   N(   R%   R6   (   R   R,   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __le__·   s    c         C   s$   t  | t ƒ r | j |  ƒ St Sd  S(   N(   R+   R	   R6   R1   (   R   R,   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __gt__½   s    c         C   s!   |  | k r t  S|  j | ƒ Sd  S(   N(   R%   R8   (   R   R,   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __ge__Ã   s    c            s   ‡  f d †  ˆ  j  Dƒ S(   Nc            s1   i  |  ]' } t  ˆ  | ƒ r t ˆ  | ƒ | “ q S(    (   t   hasattrR2   (   t   .0t   slot(   R   (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pys
   <dictcomp>Ê   s   	(   t	   __slots__(   R   (    (   R   s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __getstate__É   s    c         C   s1   x* | j  ƒ  D] \ } } t |  | | ƒ q Wd  S(   N(   R   t   setattr(   R   t   stateR<   t   value(    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __setstate__Í   s    (   R
   R   R   N(   R   t
   __module__t   __doc__R=   R   R   R   R   R   t   propertyR   R   R   R   R    R"   R$   R'   R(   R*   R-   R.   R0   R6   R7   R8   R9   R>   RB   (    (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyR	   #   s0   														c         C   s3   t  j |  | | ƒ } | j | r( | p+ d ƒ | S(   sé  Create and register an adapter and the typecasters to convert between
    a PostgreSQL |range|_ type and a PostgreSQL `Range` subclass.

    :param pgrange: the name of the PostgreSQL |range| type. Can be
        schema-qualified
    :param pyrange: a `Range` strict subclass, or just a name to give to a new
        class
    :param conn_or_curs: a connection or cursor used to find the oid of the
        range and its subtype; the typecaster is registered in a scope limited
        to this object, unless *globally* is set to `!True`
    :param globally: if `!False` (default) register the typecaster only on
        *conn_or_curs*, otherwise register it globally
    :return: `RangeCaster` instance responsible for the conversion

    If a string is passed to *pyrange*, a new `Range` subclass is created
    with such name and will be available as the `~RangeCaster.range` attribute
    of the returned `RangeCaster` object.

    The function queries the database on *conn_or_curs* to inspect the
    *pgrange* type and raises `~psycopg2.ProgrammingError` if the type is not
    found.  If querying the database is not advisable, use directly the
    `RangeCaster` class and register the adapter and typecasters using the
    provided functions.

    N(   t   RangeCastert   _from_dbt	   _registerR   (   t   pgranget   pyranget   conn_or_curst   globallyt   caster(    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   register_rangeÒ   s    t   RangeAdapterc           B   s8   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z RS(   s¢   `ISQLQuote` adapter for `Range` subclasses.

    This is an abstract class: concrete classes must set a `name` class
    attribute or override `getquoted()`.
    c         C   s   | |  _  d  S(   N(   t   adapted(   R   RP   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyR   ù   s    c         C   s   |  j  t k r |  Sd  S(   N(   t   _protoR   (   R   t   proto(    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   __conform__ü   s    c         C   s   | |  _  d  S(   N(   t   _conn(   R   t   conn(    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   prepare   s    c         C   s&  |  j  d  k r t d ƒ ‚ n  |  j } | j rD d |  j  j d ƒ S| j d  k	 r“ t | j ƒ } t | d ƒ r„ | j	 |  j
 ƒ n  | j ƒ  } n d } | j d  k	 rè t | j ƒ } t | d ƒ rÙ | j	 |  j
 ƒ n  | j ƒ  } n d } |  j  j d ƒ d | d | d | j j d ƒ d	 S(
   NsM   RangeAdapter must be subclassed overriding its name or the getquoted() methods	   'empty'::t   utf8RV   t   NULLt   (s   , s   , 's   ')(   t   nameR   t   NotImplementedErrorRP   R   t   encodeR   R   R:   RV   RT   t	   getquotedR   R   (   R   t   rt   aR   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyR]     s&    		N(	   R   RC   RD   R   RZ   R   RS   RV   R]   (    (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyRO   ñ   s   			RF   c           B   sn   e  Z d  Z d d „ Z d „  Z e d „  ƒ Z e j	 d e j
 ƒ Z e j	 d ƒ Z d d „ Z d d „ Z RS(	   s	  Helper class to convert between `Range` and PostgreSQL range types.

    Objects of this class are usually created by `register_range()`. Manual
    creation could be useful if querying the database is not advisable: in
    this case the oids must be provided.
    c         C   sŠ   | |  _  |  j | | ƒ |  j j p1 |  j j j } t | f | |  j ƒ |  _ | d  k	 r} t
 | f | d |  j ƒ |  _ n	 d  |  _ d  S(   Nt   ARRAY(   t   subtype_oidt   _create_rangest   adapterRZ   R   R   R   t   parset
   typecasterR   R   t   array_typecaster(   R   RI   RJ   t   oidRa   t	   array_oidRZ   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyR   (  s    	c         C   s0  d |  _ t | t ƒ r? t | t f i  ƒ |  _ | |  j _ n? y+ t | t ƒ ri | t k	 ri | |  _ n  Wn t k
 r} n X|  j d k rœ t d ƒ ‚ n  d |  _	 yU t | t ƒ rÒ t | t
 f i  ƒ |  _	 n  t | t
 ƒ rù | t
 k	 rù | |  _	 n  Wn t k
 rn X|  j	 d k r,t d ƒ ‚ n  d S(   s0   Create Range and RangeAdapter classes if needed.s:   pgrange must be a string or a RangeAdapter strict subclasss1   pyrange must be a type or a Range strict subclassN(   R   Rc   R+   R   R)   RO   RZ   t
   issubclasst	   TypeErrort   rangeR	   (   R   RI   RJ   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyRb   6  s0    		c      	   C   sV  d d l  m } d d l m } | | ƒ \ } } | j j d k  r] t d | j j ƒ ‚ n  | j } d | k r | j d d ƒ \ }	 }
 n | }
 d }	 y | j	 d	 |
 |	 f ƒ Wn* t k
 rß | j
 sÙ | j ƒ  n  ‚  n0 X| j ƒ  } | | k r| j
 r| j ƒ  n  | s(t d
 | ƒ ‚ n  | \ } } } t | | d | d | d | ƒS(   s|   Return a `RangeCaster` instance for the type *pgrange*.

        Raise `ProgrammingError` if the type is not found.
        iÿÿÿÿ(   t   STATUS_IN_TRANSACTION(   t   _solve_conn_cursiX` s'   range types not available in version %st   .i   t   publicsÞ   select rngtypid, rngsubtype,
    (select typarray from pg_type where oid = rngtypid)
from pg_range r
join pg_type t on t.oid = rngtypid
join pg_namespace ns on ns.oid = typnamespace
where typname = %s and ns.nspname = %s;
s   PostgreSQL type '%s' not foundRg   Ra   Rh   (   t   psycopg2.extensionsRl   t   psycopg2.extrasRm   t   infot   server_versionR    t   statust   splitt   executet
   autocommitt   rollbackt   fetchoneRF   (   R   RZ   RJ   RK   Rl   Rm   RU   t   curst   conn_statust   schemat   tnamet   recR)   t   subtypet   array(    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyRG   Y  s8    		
s]  
        ( \(|\[ )                   # lower bound flag
        (?:                         # lower bound:
          " ( (?: [^"] | "")* ) "   #   - a quoted string
          | ( [^",]+ )              #   - or an unquoted string
        )?                          #   - or empty (not catched)
        ,
        (?:                         # upper bound:
          " ( (?: [^"] | "")* ) "   #   - a quoted string
          | ( [^"\)\]]+ )           #   - or an unquoted string
        )?                          #   - or empty (not catched)
        ( \)|\] )                   # upper bound flag
        s	   (["\\])\1c         C   sg  | d  k r d  S| d k r, |  j d t ƒ S|  j j | ƒ } | d  k r] t d | ƒ ‚ n  | j d ƒ } | d  k r® | j d ƒ } | d  k	 r® |  j j d | ƒ } q® n  | j d ƒ } | d  k rÿ | j d ƒ } | d  k	 rÿ |  j j d | ƒ } qÿ n  | d  k	 r8| j	 |  j
 | ƒ } | j	 |  j
 | ƒ } n  | j d ƒ | j d	 ƒ } |  j | | | ƒ S(
   NR   s   failed to parse range: '%s'i   i   s   \1i   i   i   i   (   R   Rk   R%   t	   _re_ranget   matchR   t   groupt   _re_undoublet   subt   castRa   (   R   t   st   curt   mR   R   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyRd   ¡  s,    c         C   sI   t  |  j | ƒ |  j d  k	 r2 t  |  j | ƒ n  t |  j |  j ƒ d  S(   N(   R   Re   Rf   R   R   Rk   Rc   (   R   t   scope(    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyRH   À  s    N(   R   RC   RD   R   R   Rb   t   classmethodRG   t   ret   compilet   VERBOSER   R„   Rd   RH   (    (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyRF   !  s   	#8t   NumericRangec           B   s   e  Z d  Z RS(   sÄ   A `Range` suitable to pass Python numeric types to a PostgreSQL range.

    PostgreSQL types :sql:`int4range`, :sql:`int8range`, :sql:`numrange` are
    casted into `!NumericRange` instances.
    (   R   RC   RD   (    (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyR   È  s   t	   DateRangec           B   s   e  Z d  Z RS(   s#   Represents :sql:`daterange` values.(   R   RC   RD   (    (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyR   Ñ  s   t   DateTimeRangec           B   s   e  Z d  Z RS(   s!   Represents :sql:`tsrange` values.(   R   RC   RD   (    (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyR‘   Ö  s   t   DateTimeTZRangec           B   s   e  Z d  Z RS(   s#   Represents :sql:`tstzrange` values.(   R   RC   RD   (    (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyR’   Û  s   t   NumberRangeAdapterc           B   s   e  Z d  Z d „  Z RS(   s1   Adapt a range if the subtype doesn't need quotes.c         C   s¡   |  j  } | j r d S| j s@ t | j ƒ j ƒ  j d ƒ } n d } | j sp t | j ƒ j ƒ  j d ƒ } n d } d | j	 d | | | j	 d f j
 d ƒ S(   Ns   'empty't   asciiR   s   '%s%s,%s%s'i    i   (   RP   R   R   R   R   R]   t   decodeR    R   R   R\   (   R   R^   R   R   (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyR]   ç  s    			!	!(   R   RC   RD   R]   (    (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyR“   å  s   Rg   i@  Ra   i   Rh   iA  iV  i   iW  iB  i¤  iC  t	   daterangeiH  i:  iI  t   tsrangeiD  iZ  iE  t	   tstzrangeiF  i   iG  (    RD   RŒ   t   psycopg2._psycopgR    R   Rp   R   R   R   R   R   R   t   psycopg2.compatR   t   objectR	   R   RN   RO   RF   R   R   R‘   R’   R“   t   int4range_casterRH   t   int8range_castert   numrange_castert   daterange_castert   tsrange_castert   tstzrange_caster(    (    (    s1   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_range.pyt   <module>   sB   ¯0§	





