ó
ú2ec           @   sÄ   d  Z  d d l Z d d l Z d d l m Z y d d l m Z Wn! e k
 re d d l	 m Z n Xd d l
 m Z d d g Z d	 „  Z d
 „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s8   
Map URL prefixes to WSGI applications.  See ``URLMap``
iÿÿÿÿN(   t   html(   t   MutableMapping(   t	   DictMixin(   t   httpexceptionst   URLMapt   PathProxyURLMapc         K   s¦   d | k r | j  d ƒ } n | j d ƒ } | rK |  j | d | ƒ} n  t d | ƒ } xE | j ƒ  D]7 \ } } t | ƒ } |  j | d | ƒ} | | | <qg W| S(   Nt   not_found_appt   global_conf(   t   popt   gett   get_appR   t   itemst   parse_path_expression(   t   loaderR   t
   local_confR   t   urlmapt   patht   app_namet   app(    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   urlmap_factory   s    c         C   sz  |  j  ƒ  } d } } }  xî | r
| d d k r| | j d ƒ | sU t d ƒ ‚ n  | rj t d ƒ ‚ n  | j d ƒ } q | d d k rÕ | j d ƒ | s® t d ƒ ‚ n  | rÃ t d ƒ ‚ n  | j d ƒ } q |  rø t d |  | d f ƒ ‚ n  | j d ƒ }  q Wd	 } | r$d
 | } n  | rP| s?t d ƒ ‚ n  | d | 7} n  |  rv| ri| d 7} n  | |  7} n  | S(   s›   
    Parses a path expression like 'domain foobar.com port 20 /' or
    just '/foobar' for a path alone.  Returns as an address that
    URLMap likes.
    i    t   domains,   'domain' must be followed with a domain names   'domain' given twicet   ports*   'port' must be followed with a port numbers   'port' given twices*   more than one path given (have %r, got %r)t    s	   http://%ss/   If you give a port, you must also give a domaint   :t   /N(   t   splitt   NoneR   t
   ValueError(   R   t   partsR   R   t   s(    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyR   #   sB    	c           B   s•   e  Z d  Z d d „ Z d „  Z d „  Z e j d ƒ Z	 e j d ƒ Z
 d „  Z e d „ Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z RS(   sÈ  
    URLMap instances are dictionary-like object that dispatch to one
    of several applications based on the URL.

    The dictionary keys are URLs to match (like
    ``PATH_INFO.startswith(url)``), and the values are applications to
    dispatch to.  URLs are matched most-specific-first, i.e., longest
    URL first.  The ``SCRIPT_NAME`` and ``PATH_INFO`` environmental
    variables are adjusted to indicate the new context.

    URLs can also include domains, like ``http://blah.com/foo``, or as
    tuples ``('blah.com', '/foo')``.  This will match domain names; without
    the ``http://domain`` or with a domain of ``None`` any domain will be
    matched (so long as no other explicit domain matches).  c         C   s(   g  |  _  | s |  j } n  | |  _ d  S(   N(   t   applicationsR   t   not_found_application(   t   selfR   (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   __init__]   s    	c         C   s   t  |  j ƒ S(   N(   t   lenR   (   R    (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   __len__c   s    c         c   s#   x |  j  D] \ } } | Vq
 Wd  S(   N(   R   (   R    t   app_urlR   (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   __iter__f   s    s   //+s   ^(http|https)://c   	      C   sî   | j  d ƒ } | rV g  | j D] \ } } | ^ q } d d j t t | ƒ ƒ } n d } | d t j | j  d ƒ ƒ 7} | d t j | j  d ƒ ƒ 7} | d	 t j | j  d
 ƒ ƒ 7} t j | d d t j | ƒ ƒj	 } | | | ƒ S(   Ns   paste.urlmap_objects   defined apps: %ss   ,
  R   s   
SCRIPT_NAME: %rt   SCRIPT_NAMEs   
PATH_INFO: %rt	   PATH_INFOs   
HTTP_HOST: %rt	   HTTP_HOSTt   comment(
   R	   R   t   joint   mapt   reprR    t   escapeR   t   HTTPNotFoundt   wsgi_application(	   R    t   environt   start_responset   mappert   pt   at   matchest   extraR   (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyR   m   s    "   
c         C   s  t  | t t f ƒ r@ | d } |  j | d ƒ d } | | f S| sx | j d ƒ sx |  j j | ƒ sx t d | ƒ ‚ |  j j | ƒ } | rá | | j ƒ  } d | k rÑ | j	 d d ƒ \ } } d | } qç | d } } n d  } |  j j d | ƒ } | r| j d ƒ } n  | | f S(   Ni    i   R   s8   URL fragments must start with / or http:// (you gave %r)R   (   t
   isinstancet   listt   tuplet   normalize_urlt
   startswitht   domain_url_ret   searcht   AssertionErrort   endR   R   t   norm_url_ret   subt   rstrip(   R    t   urlt   trimR   t   match(    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyR:   }   s&    

c         C   sa   d „  } g  |  j  D] } | | ƒ | f ^ q } | j ƒ  g  | D] \ } } | ^ qB |  _  d S(   sK   
        Make sure applications are sorted with longest URLs first
        c         S   s>   |  \ \ } } } | s) d t  | ƒ f S| t  | ƒ f Sd  S(   Ns   ÿ(   R"   (   t   app_descR   RC   R   (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   key˜   s    N(   R   t   sort(   R    RG   t   desct   appst   sortable(    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt	   sort_apps”   s    	(
c         C   sx   | d  k r/ y |  | =Wn t k
 r* n Xd  S|  j | ƒ } | |  k rT |  | =n  |  j j | | f ƒ |  j ƒ  d  S(   N(   R   t   KeyErrorR:   R   t   appendRL   (   R    RC   R   t   dom_url(    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   __setitem__£   s    
c         C   sg   |  j  | ƒ } x' |  j D] \ } } | | k r | Sq Wt d | d | d pS d |  j f ƒ ‚ d  S(   Ns9   No application with the url %r (domain: %r; existing: %s)i   i    t   *(   R:   R   RM   (   R    RC   RO   R$   R   (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   __getitem__°   s    c         C   sf   |  j  | ƒ } xP |  j D]2 \ } } | | k r |  j j | | f ƒ Pq q Wt d | f ƒ ‚ d  S(   Ns   No application with the url %r(   R:   R   t   removeRM   (   R    RC   R$   R   (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   __delitem__¹   s    c         C   s    g  |  j  D] \ } } | ^ q
 S(   N(   R   (   R    R$   R   (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   keysÃ   s    c   	      C   sB  | j  d | j  d ƒ ƒ j ƒ  } d | k rH | j d d ƒ \ } } n | d d k ra d } n d } | j  d	 ƒ } |  j | t ƒ d } x™ |  j D]Ž \ \ } } } | rÔ | | k rÔ | | d | k rÔ q– n  | | k só | j | d
 ƒ r– | d c | 7<| t | ƒ | d	 <| | | ƒ Sq– W|  | d <|  j | | ƒ S(   NR(   t   SERVER_NAMER   i   s   wsgi.url_schemet   httpt   80t   443R'   R   R&   s   paste.urlmap_object(	   R	   t   lowerR   R:   t   FalseR   R;   R"   R   (	   R    R0   R1   t   hostR   t	   path_infoR   R$   R   (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   __call__Æ   s$    !	&
N(   t   __name__t
   __module__t   __doc__R   R!   R#   R%   t   ret   compileR@   R<   R   t   TrueR:   RL   RP   RR   RT   RU   R^   (    (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyR   L   s   								
	c           B   sJ   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z e e e ƒ Z	 RS(   sý  
    This is a wrapper for URLMap that catches any strings that
    are passed in as applications; these strings are treated as
    filenames (relative to `base_path`) and are passed to the
    callable `builder`, which will return an application.

    This is intended for cases when configuration files can be
    treated as applications.

    `base_paste_url` is the URL under which all applications added through
    this wrapper must go.  Use ``""`` if you want this to not
    change incoming URLs.
    c         C   s4   | |  _  |  j  j | ƒ |  _ | |  _ | |  _ d  S(   N(   R+   R:   t   base_paste_urlt	   base_patht   builder(   R    R+   Re   Rf   Rg   (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyR!   í   s    		c         C   sŽ   t  | t t f ƒ r? t j j |  j | ƒ } |  j | ƒ } n  |  j j	 | ƒ } | d pe |  j
 d |  j
 d | d f } | |  j | <d  S(   Ni    i   (   R7   t   strt   unicodet   osR   R*   Rf   Rg   R+   R:   Re   (   R    RC   R   t   app_fn(    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyRP   ó   s    c         C   s   t  |  j | ƒ S(   N(   t   getattrR+   (   R    t   attr(    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   __getattr__þ   s    c         C   s
   |  j  j S(   N(   R+   R   (   R    (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   not_found_application__get  s    c         C   s   | |  j  _ d  S(   N(   R+   R   (   R    t   value(    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   not_found_application__set  s    (
   R_   R`   Ra   R!   RP   Rn   Ro   Rq   t   propertyR   (    (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyR   Ý   s   					(   Ra   Rb   Rj   t
   paste.utilR    t   collections.abcR   R   t   ImportErrort   UserDictt   pasteR   t   __all__R   R   R   t   objectR   (    (    (    s6   /usr/local/lib/python2.7/dist-packages/paste/urlmap.pyt   <module>   s   		)‘