B
    `t#                 @   s   d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZmZmZ dZe  ZG d	d
 d
ZG dd dZG dd dZG dd dZdd ZdddZdS )    )md5)Accept)ConfigurationError)PHASE1_CONFIGIPredicateList)Notted)
predvalseq)TopologicalSorterbytes_is_nonstr_iteri   @c               @   s   e Zd Zdd ZdddZdS )PredicateConfiguratorMixinc             C   s4   | j jt|d}|d kr0t }| j j|t|d |S )N)name)registryZqueryUtilityr   PredicateListZregisterUtility)selfr   predlist r   a/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/config/predicates.pyget_predlist   s
    z'PredicateConfiguratorMixin.get_predlistNc       	         s      d f}d |df d }|d<  |d< |d< |d<  fd	d
}j|||ftd d S )Nz	%s optionz%s predicatesz%s predicate named %sz%s predicater   factoryweighs_more_thanweighs_less_thanc                 s     } | j d d S )N)r   r   )r   add)r   )r   r   r   typer   r   r   r   register&   s    
z;PredicateConfiguratorMixin._add_predicate.<locals>.register)Zintrospectablesorder)maybe_dottedZintrospectableactionr   )	r   r   r   r   r   r   ZdiscriminatorZintrr   r   )r   r   r   r   r   r   r   _add_predicate   s"    


	z)PredicateConfiguratorMixin._add_predicate)NN)__name__
__module____qualname__r   r   r   r   r   r   r      s   r   c               @   s   e Zd ZdZdd ZdS )not_aw  

    You can invert the meaning of any predicate value by wrapping it in a call
    to :class:`pyramid.config.not_`.

    .. code-block:: python
       :linenos:

       from pyramid.config import not_

       config.add_view(
           'mypackage.views.my_view',
           route_name='ok',
           request_method=not_('POST')
           )

    The above example will ensure that the view is called if the request method
    is *not* ``POST``, at least if no other view is more specific.

    This technique of wrapping a predicate value in ``not_`` can be used
    anywhere predicate values are accepted:

    - :meth:`pyramid.config.Configurator.add_view`

    - :meth:`pyramid.config.Configurator.add_route`

    - :meth:`pyramid.config.Configurator.add_subscriber`

    - :meth:`pyramid.view.view_config`

    - :meth:`pyramid.events.subscriber`

    .. versionadded:: 1.5
    c             C   s
   || _ d S )N)value)r   r#   r   r   r   __init__[   s    znot_.__init__N)r   r    r!   __doc__r$   r   r   r   r   r"   7   s   "r"   c               @   s   e Zd Zdd ZdS )PredicateInfoc             C   s   || _ || _|| _|| _d S )N)packager   settingsr   )r   r'   r   r(   r   r   r   r   r$   d   s    zPredicateInfo.__init__N)r   r    r!   r$   r   r   r   r   r&   c   s   r&   c               @   s.   e Zd Zdd Zd
ddZdd Zdd	 ZdS )r   c             C   s   t  | _d | _d S )N)r	   sorter
last_added)r   r   r   r   r$   l   s    zPredicateList.__init__Nc             C   s   || _ | jj||||d d S )N)afterbefore)r*   r)   r   )r   r   r   r   r   r   r   r   r   p   s    zPredicateList.addc             C   s   | j jS )N)r)   names)r   r   r   r   r-   {   s    zPredicateList.namesc             K   s  | j  }t }g }g }t|j|j| |jd}xt|D ]\}\}	}
|	|	d }|d kr^q<t
|tsn|f}x|D ]}|}d}t
|tr|j}d}|
||}|rt|}| }t|s|g}x|D ]}|t| qW |d|d >  || qtW q<W |r\ddlm} g }dd |D }x |D ]}	|||	|d	 q*W td
|d|f d}x|D ]}||B }qfW t| t|d  }||| fS )N)r'   r   r(   r   FT   r   )get_close_matchesc             S   s   g | ]\}}|qS r   r   ).0r   _r   r   r   
<listcomp>   s    z&PredicateList.make.<locals>.<listcomp>   z.Unknown predicate values: %r (did you mean %s),)r)   sortedr   r&   r'   r   Zget_settingsr   	enumeratepop
isinstancer   r"   r#   r   phashr   updater
   appenddifflibr/   extendr   join	MAX_ORDERlen	hexdigest)r   configkwZorderedr9   weightspredsinfonr   Zpredicate_factoryvalsvalZrealvalZnottedpredhasheshr/   Zclosestr-   Zscorebitr   r   r   r   make   sZ    








zPredicateList.make)NN)r   r    r!   r$   r   r-   rN   r   r   r   r   r   k   s   
r   c             C   s   t t| S )N)strr   parse_offer)Zofferr   r   r   normalize_accept_offer   s    rQ   Nc                s<   dkrg t | dfdd	  fdd}t| |dS )a  
    Sort a list of offers by preference.

    For a given ``type/subtype`` category of offers, this algorithm will
    always sort offers with params higher than the bare offer.

    :param offers: A list of offers to be sorted.
    :param order: A weighted list of offers where items closer to the start of
                  the list will be a preferred over items closer to the end.
    :return: A list of offers sorted first by specificity (higher to lower)
             then by ``order``.

    Nc                s   t  fddtD |S )Nc             3   s   | ]\}}| kr|V  qd S )Nr   )r0   ix)r#   r   r   	<genexpr>   s    z?sort_accept_offers.<locals>.find_order_index.<locals>.<genexpr>)nextr6   )r#   default)r   )r#   r   find_order_index   s    z,sort_accept_offers.<locals>.find_order_indexc                sB   t | } |jd |j }|jr2 | }nd }||fS )aw  
        (type_weight, params_weight)

        type_weight:
            - index of specific ``type/subtype`` in order list
            - ``max_weight * 2`` if no match is found

        params_weight:
            - index of specific ``type/subtype;params`` in order list
            - ``max_weight`` if not found
            - ``max_weight + 1`` if no params at all

        /r.   )r   rP   r   subtypeparams)r#   parsedZtype_wZparam_w)rW   
max_weightr   r   offer_sort_key   s    
z*sort_accept_offers.<locals>.offer_sort_key)key)N)r@   r5   )Zoffersr   r]   r   )rW   r\   r   r   sort_accept_offers   s    r_   )N)hashlibr   Zwebob.acceptparser   Zpyramid.exceptionsr   Zpyramid.interfacesr   r   Zpyramid.predicatesr   Zpyramid.registryr   Zpyramid.utilr	   r
   r   r?   rA   ZDEFAULT_PHASHr   r"   r&   r   rQ   r_   r   r   r   r   <module>   s   
),j