B
    `?(                 @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZmZ G dd deeZee
G d	d
 d
Zee	G dd deZG dd dZdd ZG dd deZedZdS )    N)implementer)
Components)reify)IIntrospectableIIntrospector	ISettings)CALLER_PACKAGEcaller_packagec               @   s   e Zd ZdZdZdZefddZdd Zdd	 Z	e
d
d Zdd ZdddZdddZdd Zdd Zdd Zdd ZeeeZdS )Registryal  A registry object is an :term:`application registry`.

    It is used by the framework itself to perform mappings of URLs to view
    callables, as well as servicing other various framework duties. A registry
    has its own internal API, but this API is rarely used by Pyramid
    application developers (it's usually only used by developers of the
    Pyramid framework and Pyramid addons).  But it has a number of attributes
    that may be useful to application developers within application code,
    such as ``settings``, which is a dictionary containing application
    deployment settings.

    For information about the purpose and usage of the application registry,
    see :ref:`zca_chapter`.

    The registry may be used both as an :class:`pyramid.interfaces.IDict` and
    as a Zope component registry.
    These two ways of storing configuration are independent.
    Applications will tend to prefer to store information as key-values
    whereas addons may prefer to use the component registry to avoid naming
    conflicts and to provide more complex lookup mechanisms.

    The application registry is usually accessed as ``request.registry`` in
    application code. By the time a registry is used to handle requests it
    should be considered frozen and read-only. Any changes to its internal
    state should be done with caution and concern for thread-safety.

    FNc             O   sD   t  | _|   |tkr"t j}tj| |f|| t	|  d S )N)
	threadingLock_lock_clear_view_lookup_cacher   r	   __name__r   __init__dict)selfpackage_nameargskw r   X/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/registry.pyr   .   s    
zRegistry.__init__c             C   s
   i | _ d S )N)Z_view_lookup_cache)r   r   r   r   r   9   s    z!Registry._clear_view_lookup_cachec             C   s   dS )NTr   )r   r   r   r   __bool__<   s    zRegistry.__bool__c             C   s   | j S )N)r   )r   r   r   r   r   @   s    zRegistry.package_namec             O   s   t j| f||}d| _|S )NT)r   registerSubscriptionAdapterhas_listeners)r   argr   resultr   r   r   r   D   s    z$Registry.registerSubscriptionAdapter Tc             C   s   | j dd |||||dS )Nc             S   s   | S )Nr   )xr   r   r   <lambda>O       z.Registry.registerSelfAdapter.<locals>.<lambda>)requiredprovidednameinfoevent)ZregisterAdapter)r   r!   r"   r#   r$   r%   r   r   r   registerSelfAdapterI   s    zRegistry.registerSelfAdapterc             C   s   | |s| j|||dS |S )N)default)Z
providedByZqueryAdapter)r   objectZ	interfacer'   r   r   r   queryAdapterOrSelfW   s    
zRegistry.queryAdapterOrSelfc             O   s   t j| f||}d| _|S )NT)r   registerHandlerr   )r   r   r   r   r   r   r   r*   _   s    zRegistry.registerHandlerc             G   s    | j rdd | |d D  d S )Nc             S   s   g | ]}|qS r   r   ).0_r   r   r   
<listcomp>g   s    z#Registry.notify.<locals>.<listcomp>)r   Zsubscribers)r   eventsr   r   r   notifyd   s    zRegistry.notifyc             C   s   | j S )N)	_settings)r   r   r   r   _get_settingsk   s    zRegistry._get_settingsc             C   s   |  |t || _d S )N)ZregisterUtilityr   r0   )r   settingsr   r   r   _set_settingsn   s    zRegistry._set_settings)NNr   r   T)N)r   
__module____qualname____doc__r   r0   r   r   r   r   r   r   r   r&   r)   r*   r/   r1   r3   propertyr2   r   r   r   r   r
      s   

r
   c               @   sj   e Zd Zdd Zdd ZdddZddd	Zdd
dZdd Zdd Z	dd Z
dd Zdd Zdd ZdS )Introspectorc             C   s   i | _ i | _d| _d S )Nr   )_refs_categories_counter)r   r   r   r   r   w   s    zIntrospector.__init__c             C   s>   | j |ji }|||j< |||j< | j|_|  jd7  _d S )N   )r:   
setdefaultcategory_namediscriminatordiscriminator_hashr;   order)r   intrcategoryr   r   r   add|   s
    

zIntrospector.addNc             C   s   | j |i }|||}|S )N)r:   r=   get)r   r>   r?   r'   rC   rB   r   r   r   rE      s    zIntrospector.getc                sT   |d krt d} j|}|d kr*|S | }tt||d} fdd|D S )NrA   )keyc                s   g | ]}|  |d qS ))Zintrospectablerelated)rG   )r+   rB   )r   r   r   r-      s   z-Introspector.get_category.<locals>.<listcomp>)operator
attrgetterr:   rE   valuessortedset)r   r>   r'   sort_keyrC   rJ   r   )r   r   get_category   s    

zIntrospector.get_categoryc             C   s2   g }x(|   D ]}||| j||df qW |S )N)rM   )
categoriesappendrN   )r   rM   Lr>   r   r   r   categorized   s    zIntrospector.categorizedc             C   s   t | j S )N)rK   r:   keys)r   r   r   r   rO      s    zIntrospector.categoriesc             C   sh   |  ||}|d krd S | j|g }x |D ]}| j| }|| q,W | j|j }||j= ||j= d S )N)rE   r9   popremover:   r>   r?   r@   )r   r>   r?   rB   rQ   dZL2rC   r   r   r   rU      s    

zIntrospector.removec             C   sP   g }xF|D ]>}|\}}| j |i |}|d kr>t||f|| q
W |S )N)r:   rE   KeyErrorrP   )r   pairsintrospectablespairr>   r?   rB   r   r   r   _get_intrs_by_pairs   s    
z Introspector._get_intrs_by_pairsc                sZ   |  |  fdd D }x8|D ]0\}}| j|g }||k	r"||kr"|| q"W d S )Nc             3   s    | ]} D ]}||fV  q
qd S )Nr   )r+   r   y)rY   r   r   	<genexpr>   s    z&Introspector.relate.<locals>.<genexpr>)r[   r9   r=   rP   )r   rX   	relatabler   r\   rQ   r   )rY   r   relate   s    
zIntrospector.relatec                sR   |  |  fdd D }x0|D ](\}}| j|g }||kr"|| q"W d S )Nc             3   s    | ]} D ]}||fV  q
qd S )Nr   )r+   r   r\   )rY   r   r   r]      s    z(Introspector.unrelate.<locals>.<genexpr>)r[   r9   rE   rU   )r   rX   r^   r   r\   rQ   r   )rY   r   unrelate   s    
zIntrospector.unrelatec             C   sD   |j |j }}| j|i |}|d kr6t||f| j|g S )N)r>   r?   r:   rE   rW   r9   )r   rB   r>   r?   r   r   r   rG      s
    zIntrospector.related)N)NN)N)r   r4   r5   r   rD   rE   rN   rR   rO   rU   r[   r_   r`   rG   r   r   r   r   r8   u   s   



r8   c               @   s`   e Zd ZdZdZdd Zdd Zdd Zd	d
 Ze	dd Z
dd Zdd Zdd Zdd ZdS )Introspectabler   Nc             C   s"   || _ || _|| _|| _g | _d S )N)r>   r?   title	type_name
_relations)r   r>   r?   rb   rc   r   r   r   r      s
    zIntrospectable.__init__c             C   s   | j d||f d S )NT)rd   rP   )r   r>   r?   r   r   r   r_      s    zIntrospectable.relatec             C   s   | j d||f d S )NF)rd   rP   )r   r>   r?   r   r   r   r`      s    zIntrospectable.unrelatec             C   s   t | j| jkstd S )N)undeferr?   AssertionError)r   r   r   r   _assert_resolved   s    zIntrospectable._assert_resolvedc             C   s   |    t| jS )N)rg   hashr?   )r   r   r   r   r@      s    z!Introspectable.discriminator_hashc             C   s   |    t| jf| jf S )N)rg   rh   r>   r?   )r   r   r   r   __hash__   s    zIntrospectable.__hash__c             C   s   |    d| jj| j| jf S )Nz"<%s category %r, discriminator %r>)rg   	__class__r   r>   r?   )r   r   r   r   __repr__   s
    zIntrospectable.__repr__c             C   s   dS )NTr   )r   r   r   r   r      s    zIntrospectable.__bool__c             C   sf   t | j| _|| _||  xD| jD ]:\}}}t |}|rB|j}n|j}|| j| jf||f q$W d S )N)re   r?   action_inforD   rd   r_   r`   r>   )r   Zintrospectorrl   r_   r>   r?   methodr   r   r   register   s    

zIntrospectable.register)r   r4   r5   rA   rl   r   r_   r`   rg   r7   r@   ri   rk   r   rn   r   r   r   r   ra      s   ra   c               @   s,   e Zd ZdZdd Zedd Zdd ZdS )	Deferreda0  Can be used by a third-party configuration extender to wrap a
    :term:`discriminator` during configuration if an immediately hashable
    discriminator cannot be computed because it relies on unresolved values.
    The function should accept no arguments and should return a hashable
    discriminator.c             C   s
   || _ d S )N)func)r   rp   r   r   r   r     s    zDeferred.__init__c             C   s   |   }| ` |S )N)rp   )r   r   r   r   r   value  s    zDeferred.valuec             C   s   | j S )N)rq   )r   r   r   r   resolve  s    zDeferred.resolveN)r   r4   r5   r6   r   r   rq   rr   r   r   r   r   ro     s   ro   c             C   s   t | tr|  } | S )zFunction which accepts an object and returns it unless it is a
    :class:`pyramid.registry.Deferred` instance.  If it is an instance of
    that class, its ``resolve`` method is called, and the result of the
    method is returned.)
isinstancero   rr   )vr   r   r   re     s    
re   c               @   s   e Zd ZdZdS )
predvalseqzE A subtype of tuple used to represent a sequence of predicate values N)r   r4   r5   r6   r   r   r   r   ru   )  s   ru   global)rH   r   Zzope.interfacer   Zzope.interface.registryr   Zpyramid.decoratorr   Zpyramid.interfacesr   r   r   Zpyramid.pathr   r	   r   r
   r8   ra   ro   re   tupleru   Zglobal_registryr   r   r   r   <module>   s   j\9
