B
    `^                 @   s  d Z ddlZddlmZmZmZmZmZ ddlm	Z	 ddl
Z
ddlZddlZddlmZmZ ddlmZmZmZmZmZ ddd	d
ddddddddgZe
dZee
jrejZndd ZG dd deZG dd deZG dd deZG dd deZ G dd de Z!G dd deZ"G dd deZ#i Z$e%Z&G dd de'Z(ee(G d d deZ)G d!d de)Z*G d"d
 d
e)Z+G d#d de)Z,e,Z-dS )$zContainer and Namespace classes    N   )pickleanydbmadd_metaclassPYVERunicode_text)CreationAbortedErrorMissingCacheParameter)
_threadingfile_synchronizermutex_synchronizerNameLocknull_synchronizerValue	ContainerContainerContextMemoryContainerDBMContainerNamespaceManagerMemoryNamespaceManagerDBMNamespaceManagerFileContainerOpenResourceNamespaceManagerFileNamespaceManagerr   zbeaker.containerc             G   s   d S )N )messageargsr   r   X/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/beaker/container.pydebug   s    r   c               @   s   e Zd ZdZedd Zdd Zdd Zdd	 Zd
d Z	dd Z
d%ddZdd Zdd Zdd Zdd Zd&ddZdd Zdd  Zd!d" Zd#d$ ZdS )'r   aH  Handles dictionary operations and locking for a namespace of
    values.

    :class:`.NamespaceManager` provides a dictionary-like interface,
    implementing ``__getitem__()``, ``__setitem__()``, and
    ``__contains__()``, as well as functions related to lock
    acquisition.

    The implementation for setting and retrieving the namespace data is
    handled by subclasses.

    NamespaceManager may be used alone, or may be accessed by
    one or more :class:`.Value` objects.  :class:`.Value` objects provide per-key
    services like expiration times and automatic recreation of values.

    Multiple NamespaceManagers created with a particular name will all
    share access to the same underlying datasource and will attempt to
    synchronize against a common mutex object.  The scope of this
    sharing may be within a single process or across multiple
    processes, depending on the type of NamespaceManager used.

    The NamespaceManager itself is generally threadsafe, except in the
    case of the DBMNamespaceManager in conjunction with the gdbm dbm
    implementation.

    c             C   s   dS )z`Initialize module-level dependent libraries required
        by this :class:`.NamespaceManager`.Nr   )clsr   r   r   _init_dependencies:   s    z#NamespaceManager._init_dependenciesc             C   s   |    || _d S )N)r    	namespace)selfr!   r   r   r   __init__?   s    zNamespaceManager.__init__c             C   s
   t  dS )a+  Return a locking object that is used to synchronize
        multiple threads or processes which wish to generate a new
        cache value.

        This function is typically an instance of
        :class:`.FileSynchronizer`, :class:`.ConditionSynchronizer`,
        or :class:`.null_synchronizer`.

        The creation lock is only used when a requested value
        does not exist, or has been expired, and is only used
        by the :class:`.Value` key-management object in conjunction
        with a "createfunc" value-creation function.

        N)NotImplementedError)r"   keyr   r   r   get_creation_lockC   s    z"NamespaceManager.get_creation_lockc             C   s
   t  dS )a  Implement removal of the entire contents of this
        :class:`.NamespaceManager`.

        e.g. for a file-based namespace, this would remove
        all the files.

        The front-end to this method is the
        :meth:`.NamespaceManager.remove` method.

        N)r$   )r"   r   r   r   	do_removeT   s    zNamespaceManager.do_removec             C   s   dS )zEstablish a read lock.

        This operation is called before a key is read.    By
        default the function does nothing.

        Nr   )r"   r   r   r   acquire_read_locka   s    z"NamespaceManager.acquire_read_lockc             C   s   dS )zRelease a read lock.

        This operation is called after a key is read.    By
        default the function does nothing.

        Nr   )r"   r   r   r   release_read_locki   s    z"NamespaceManager.release_read_lockTFc             C   s   dS )a  Establish a write lock.

        This operation is called before a key is written.
        A return value of ``True`` indicates the lock has
        been acquired.

        By default the function returns ``True`` unconditionally.

        'replace' is a hint indicating the full contents
        of the namespace may be safely discarded. Some backends
        may implement this (i.e. file backend won't unpickle the
        current contents).

        Tr   )r"   waitreplacer   r   r   acquire_write_lockq   s    z#NamespaceManager.acquire_write_lockc             C   s   dS )zRelease a write lock.

        This operation is called after a new value is written.
        By default this function does nothing.

        Nr   )r"   r   r   r   release_write_lock   s    z#NamespaceManager.release_write_lockc             C   s
   |  |S )zYReturn ``True`` if the given key is present in this
        :class:`.Namespace`.
        )__contains__)r"   r%   r   r   r   has_key   s    zNamespaceManager.has_keyc             C   s
   t  d S )N)r$   )r"   r%   r   r   r   __getitem__   s    zNamespaceManager.__getitem__c             C   s
   t  d S )N)r$   )r"   r%   valuer   r   r   __setitem__   s    zNamespaceManager.__setitem__Nc             C   s   || |< dS )zSets a value in this :class:`.NamespaceManager`.

        This is the same as ``__setitem__()``, but
        also allows an expiration time to be passed
        at the same time.

        Nr   )r"   r%   r1   
expiretimer   r   r   	set_value   s    zNamespaceManager.set_valuec             C   s
   t  d S )N)r$   )r"   r%   r   r   r   r.      s    zNamespaceManager.__contains__c             C   s
   t  d S )N)r$   )r"   r%   r   r   r   __delitem__   s    zNamespaceManager.__delitem__c             C   s
   t  dS )zReturn the list of all keys.

        This method may not be supported by all
        :class:`.NamespaceManager` implementations.

        N)r$   )r"   r   r   r   keys   s    zNamespaceManager.keysc             C   s   |    dS )zRemove the entire contents of this
        :class:`.NamespaceManager`.

        e.g. for a file-based namespace, this would remove
        all the files.
        N)r'   )r"   r   r   r   remove   s    zNamespaceManager.remove)TF)N)__name__
__module____qualname____doc__classmethodr    r#   r&   r'   r(   r)   r,   r-   r/   r0   r2   r4   r.   r5   r6   r7   r   r   r   r   r      s"   


	c               @   sn   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dddZ
dd ZdddZdddZdd ZdS )r   zzA NamespaceManager where read/write operations require opening/
    closing of a resource which is possibly mutexed.

    c             C   s*   t | | |  | _d| _t | _d S )Nr   )r   r#   get_access_lockaccess_lockopenersr
   Lockmutex)r"   r!   r   r   r   r#      s    
z%OpenResourceNamespaceManager.__init__c             C   s
   t  d S )N)r$   )r"   r   r   r   r=      s    z,OpenResourceNamespaceManager.get_access_lockc             C   s
   t  d S )N)r$   )r"   flagsr+   r   r   r   do_open   s    z$OpenResourceNamespaceManager.do_openc             C   s
   t  d S )N)r$   )r"   r   r   r   do_close   s    z%OpenResourceNamespaceManager.do_closec             C   s:   | j   y| jddd W n   | j    Y nX d S )NrT)
checkcount)r>   r(   openr)   )r"   r   r   r   r(      s    

z.OpenResourceNamespaceManager.acquire_read_lockc             C   s"   z| j dd W d | j  X d S )NT)rF   )closer>   r)   )r"   r   r   r   r)      s    z.OpenResourceNamespaceManager.release_read_lockTFc             C   sF   | j |}y|s|r&| jdd|d |S    | j    Y nX d S )NcT)rF   r+   )r>   r,   rG   r-   )r"   r*   r+   rE   r   r   r   r,      s    
z/OpenResourceNamespaceManager.acquire_write_lockc             C   s"   z| j dd W d | j  X d S )NT)rF   )rH   r>   r-   )r"   r   r   r   r-      s    z/OpenResourceNamespaceManager.release_write_lockc             C   s\   | j   z@|r6| jdkr&| || |  jd7  _n| || d| _W d | j   X d S )Nr   r   )rA   acquirer?   rC   release)r"   rB   rF   r+   r   r   r   rG      s    


z!OpenResourceNamespaceManager.openc             C   s^   | j   zB|r2|  jd8  _| jdkrJ|   n| jdkrD|   d| _W d | j   X d S )Nr   r   )rA   rJ   r?   rD   rK   )r"   rF   r   r   r   rH      s    




z"OpenResourceNamespaceManager.closec             C   s4   | j   z| jdd |   W d | j   X d S )NF)rF   )r>   r,   rH   r'   r-   )r"   r   r   r   r7     s
    
z#OpenResourceNamespaceManager.removeN)TF)FF)F)r8   r9   r:   r;   r#   r=   rC   rD   r(   r)   r,   r-   rG   rH   r7   r   r   r   r   r      s   



c               @   s`   e Zd ZdZ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ddZdd ZdS )r   zImplements synchronization, expiration, and value-creation logic
    for a single value stored in a :class:`.NamespaceManager`.

    )r%   
createfuncr3   expire_argument	starttime
storedtimer!   Nc             C   s(   || _ || _|| _|| _d| _|| _d S )N)r%   rL   rM   rN   rO   r!   )r"   r%   r!   rL   r3   rN   r   r   r   r#     s    zValue.__init__c             C   s(   | j   z| j| j kS | j   X dS )zrreturn true if the container has a value stored.

        This is regardless of it being expired or not.

        N)r!   r(   r%   r)   )r"   r   r   r   	has_value   s    
zValue.has_valuec             C   s   |   p| jd k	S )N)has_current_valuerL   )r"   r   r   r   can_have_value,  s    zValue.can_have_valuec             C   sb   | j   zF| j| j k}|rNy|  \}}}| || S  tk
rL   Y nX dS | j   X d S )NF)r!   r(   r%   
_get_value_is_expiredKeyErrorr)   )r"   rQ   storedexpiredr1   r   r   r   rR   /  s    
zValue.has_current_valuec             C   s,   | j dk	r|| j k p*|dk	o*t || kS )z1Return true if this container's value is expired.N)rN   time)r"   rO   r3   r   r   r   rU   =  s    

zValue._is_expiredc             C   sb  | j   z\|  }|rTy"|  \}}}| ||s8|S W n tk
rR   d}Y nX | jsdt| jW d | j   X d}| j 	| j}|r|j
ddstd |S td d}|std |
  td z| j   zJ|  ry$|  \}}}| ||s|S W n tk
r   Y nX W d | j   X td |  }| | |S |  td	 X d S )
NF)r*   z6get_value returning old value while new one is createdzlock_creatfunc (didnt wait)Tzlock_createfunc (waiting)zlock_createfunc (waited)zget_value creating new valuezreleased create lock)r!   r(   rQ   rT   rU   rV   rL   r%   r)   r&   rJ   r   r4   rK   )r"   rQ   rW   rX   r1   Zhas_createlockZcreation_lockvr   r   r   	get_valueK  sT    





zValue.get_valuec             C   s   | j | j }y|\}}}W nz tk
rx   t|dks8 |\}}| j}td|| j | j   | || | j   Y n t	k
r   t
| jY nX |||fS )N   z*get_value upgrading time %r expire time %r)r!   r%   
ValueErrorlenrM   r   r)   r4   r(   	TypeErrorrV   )r"   r1   rW   rX   r   r   r   rT     s    
zValue._get_valuec             C   s\   | j   z@|d krt }td|| j | j j| j|| j|f| jd W d | j   X d S )Nz'set_value stored time %r expire time %r)r3   )r!   r,   rY   r   rM   r4   r%   r-   )r"   r1   rO   r   r   r   r4     s    
zValue.set_valuec             C   s^   | j   zBtd | j| j krDy| j | j= W n tk
rB   Y nX d| _W d | j   X d S )Nclear_valuerP   )r!   r,   r   r%   rV   rO   r-   )r"   r   r   r   r`     s    

zValue.clear_value)NNN)N)r8   r9   r:   r;   	__slots__r#   rQ   rS   rR   rU   r[   rT   r4   r`   r   r   r   r   r     s   
8
c               @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )AbstractDictionaryNSManagera  A subclassable NamespaceManager that places data in a dictionary.

    Subclasses should provide a "dictionary" attribute or descriptor
    which returns a dict-like object.   The dictionary will store keys
    that are local to the "namespace" attribute of this manager, so
    ensure that the dictionary will not be used by any other namespace.

    e.g.::

        import collections
        cached_data = collections.defaultdict(dict)

        class MyDictionaryManager(AbstractDictionaryNSManager):
            def __init__(self, namespace):
                AbstractDictionaryNSManager.__init__(self, namespace)
                self.dictionary = cached_data[self.namespace]

    The above stores data in a global dictionary called "cached_data",
    which is structured as a dictionary of dictionaries, keyed
    first on namespace name to a sub-dictionary, then on actual
    cache key to value.

    c             C   s   t d| j|f ddS )Nzmemorynamespace/funclock/%s/%sT)
identifierZ	reentrant)r   r!   )r"   r%   r   r   r   r&     s    
z-AbstractDictionaryNSManager.get_creation_lockc             C   s
   | j | S )N)
dictionary)r"   r%   r   r   r   r0     s    z'AbstractDictionaryNSManager.__getitem__c             C   s   | j |S )N)rd   r.   )r"   r%   r   r   r   r.     s    z(AbstractDictionaryNSManager.__contains__c             C   s   | j |S )N)rd   r.   )r"   r%   r   r   r   r/     s    z#AbstractDictionaryNSManager.has_keyc             C   s   || j |< d S )N)rd   )r"   r%   r1   r   r   r   r2     s    z'AbstractDictionaryNSManager.__setitem__c             C   s   | j |= d S )N)rd   )r"   r%   r   r   r   r5     s    z'AbstractDictionaryNSManager.__delitem__c             C   s   | j   d S )N)rd   clear)r"   r   r   r   r'     s    z%AbstractDictionaryNSManager.do_removec             C   s
   | j  S )N)rd   r6   )r"   r   r   r   r6     s    z AbstractDictionaryNSManager.keysN)r8   r9   r:   r;   r&   r0   r.   r/   r2   r5   r'   r6   r   r   r   r   rb     s   rb   c               @   s    e Zd ZdZe Zdd ZdS )r   zE:class:`.NamespaceManager` that uses a Python dictionary for storage.c             K   s"   t | | tj| jt| _d S )N)rb   r#   r   
namespacesgetr!   dictrd   )r"   r!   kwargsr   r   r   r#     s    zMemoryNamespaceManager.__init__N)r8   r9   r:   r;   utilZSyncDictrf   r#   r   r   r   r   r     s   c               @   s   e Zd Z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d Zdd Zdd Zdd Zdd Zdd Zdd Zd d! ZdS )#r   z?:class:`.NamespaceManager` that uses ``dbm`` files for storage.NTc             K   s   || _ |s|stdn|r$|| _n
|d | _t| j |sL|sLtdn|rX|| _n
|d | _t| j |ptt| _d | _t	
| | tj| j| jgd| j d| _td| j |   d S )Nzdata_dir or dbm_dir is requiredz/container_dbmz data_dir or lock_dir is requiredz/container_dbm_lockz.dbm)rootidentifiers	extensiondigest_filenameszdata file %s)rn   r	   dbm_dirrj   verify_directorylock_dirr   	dbmmoduledbmr   r#   encoded_pathr!   filer   
_checkfile)r"   r!   rr   data_dirro   rq   rn   ri   r   r   r   r#     s,    




zDBMNamespaceManager.__init__c             C   s   t | j| jdS )N)rc   rq   )r   r!   rq   )r"   r   r   r   r=     s    z#DBMNamespaceManager.get_access_lockc             C   s   t d| j|f | jdS )Nzdbmcontainer/funclock/%s/%s)rc   rq   )r   r!   rq   )r"   r%   r   r   r   r&     s    
z%DBMNamespaceManager.get_creation_lockc             C   s@   t |t jrdS x(dD ] }t |t j | t jrdS qW dS )NT)dbdatpagdirF)osaccessF_OKextsep)r"   ru   extr   r   r   file_exists$  s    
zDBMNamespaceManager.file_existsc             C   s&   t j|}t j|s"t| d S )N)r|   pathdirnameexistsrj   rp   )r"   filenamer   r   r   r   
_ensuredir.  s    zDBMNamespaceManager._ensuredirc             C   s4   |  | js0| | j | j| jd}|  d S )NrI   )r   ru   r   rr   rG   rH   )r"   gr   r   r   rv   3  s    zDBMNamespaceManager._checkfilec             C   sb   g }t | jt jr || j x<dD ]4}t | jt j | t jr&|| jt j |  q&W |S )N)rz   r{   rx   ry   )r|   r}   ru   r~   appendr   )r"   listr   r   r   r   get_filenames9  s    
z!DBMNamespaceManager.get_filenamesc             C   sN   t d| j y| j| j|| _W n&   |   | j| j|| _Y nX d S )Nzopening dbm file %s)r   ru   rr   rG   rs   rv   )r"   rB   r+   r   r   r   rC   C  s    zDBMNamespaceManager.do_openc             C   s$   | j d k	r td| j | j   d S )Nzclosing dbm file %s)rs   r   ru   rH   )r"   r   r   r   rD   K  s    
zDBMNamespaceManager.do_closec             C   s    x|   D ]}t| q
W d S )N)r   r|   r7   )r"   fr   r   r   r'   P  s    zDBMNamespaceManager.do_removec             C   s   t | j| S )N)r   loadsrs   )r"   r%   r   r   r   r0   T  s    zDBMNamespaceManager.__getitem__c             C   s&   t dkrt|tr|d}|| jkS )N)   r\   zUTF-8)r   
isinstancer   encoders   )r"   r%   r   r   r   r.   W  s    

z DBMNamespaceManager.__contains__c             C   s   t || j|< d S )N)r   dumpsrs   )r"   r%   r1   r   r   r   r2   _  s    zDBMNamespaceManager.__setitem__c             C   s   | j |= d S )N)rs   )r"   r%   r   r   r   r5   b  s    zDBMNamespaceManager.__delitem__c             C   s
   | j  S )N)rs   r6   )r"   r   r   r   r6   e  s    zDBMNamespaceManager.keys)NNNNT)r8   r9   r:   r;   r#   r=   r&   r   r   rv   r   rC   rD   r'   r0   r.   r2   r5   r6   r   r   r   r   r     s$     
 

c               @   sr   e Zd Z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d Zdd Zdd Zdd Zdd ZdS )r   z:class:`.NamespaceManager` that uses binary files for storage.

    Each namespace is implemented as a single file storing a
    dictionary of key/value pairs, serialized using the Python
    ``pickle`` module.

    NTc             K   s   || _ |s|stdn|r$|| _n
|d | _t| j |sL|sLtdn|rX|| _n
|d | _t| j t| | tj| j| j	gd| j d| _
i | _td| j
 d S )Nz data_dir or file_dir is requiredz/container_filez data_dir or lock_dir is requiredz/container_file_lockz.cache)rk   rl   rm   rn   zdata file %s)rn   r	   file_dirrj   rp   rq   r   r#   rt   r!   ru   hashr   )r"   r!   rw   r   rq   rn   ri   r   r   r   r#   q  s(    



zFileNamespaceManager.__init__c             C   s   t | j| jdS )N)rc   rq   )r   r!   rq   )r"   r   r   r   r=     s    z$FileNamespaceManager.get_access_lockc             C   s   t d| j|f | jdS )Nzdbmcontainer/funclock/%s/%s)rc   rq   )r   r!   rq   )r"   r%   r   r   r   r&     s    
z&FileNamespaceManager.get_creation_lockc             C   s   t |t jS )N)r|   r}   r~   )r"   ru   r   r   r   r     s    z FileNamespaceManager.file_existsc          
   C   sz   |sp|  | jrpy(t| jd}t|| _W d Q R X W n6 tk
rn } z|jtjtj	gkr^ W d d }~X Y nX || _
d S )Nrb)r   ru   rG   r   loadr   IOErrorerrnoEACCESENOENTrB   )r"   rB   r+   fher   r   r   rC     s    zFileNamespaceManager.do_openc             C   s>   | j dks| j dkr.t| j}t| j| i | _d | _ d S )NrI   w)rB   r   r   r   rj   Z
safe_writeru   )r"   Zpickledr   r   r   rD     s
    zFileNamespaceManager.do_closec             C   s0   yt | j W n tk
r$   Y nX i | _d S )N)r|   r7   ru   OSErrorr   )r"   r   r   r   r'     s
    zFileNamespaceManager.do_removec             C   s
   | j | S )N)r   )r"   r%   r   r   r   r0     s    z FileNamespaceManager.__getitem__c             C   s
   || j kS )N)r   )r"   r%   r   r   r   r.     s    z!FileNamespaceManager.__contains__c             C   s   || j |< d S )N)r   )r"   r%   r1   r   r   r   r2     s    z FileNamespaceManager.__setitem__c             C   s   | j |= d S )N)r   )r"   r%   r   r   r   r5     s    z FileNamespaceManager.__delitem__c             C   s
   | j  S )N)r   r6   )r"   r   r   r   r6     s    zFileNamespaceManager.keys)NNNT)r8   r9   r:   r;   r#   r=   r&   r   rC   rD   r'   r0   r.   r2   r5   r6   r   r   r   r   r   i  s    
	c               @   s   e Zd Zdd ZdddZdS )ContainerMetac             C   s   | j t| < t| |||S )N)namespace_classnamespace_classestyper#   )r   	classnamebasesZdict_r   r   r   r#     s    
zContainerMeta.__init__Nc       
      K   s@   ||kr|| }nt |  }	|	|f| ||< }t|||||dS )N)rL   r3   rN   )r   r   )
r"   r%   contextr!   rL   r3   rN   ri   nsZnsclsr   r   r   __call__  s    
zContainerMeta.__call__)NNN)r8   r9   r:   r#   r   r   r   r   r   r     s    r   c               @   s   e Zd ZdZeZdS )r   zImplements synchronization and value-creation logic
    for a 'value' stored in a :class:`.NamespaceManager`.

    :class:`.Container` and its subclasses are deprecated.   The
    :class:`.Value` class is now used for this purpose.

    N)r8   r9   r:   r;   r   r   r   r   r   r   r     s   c               @   s   e Zd ZeZdS )r   N)r8   r9   r:   r   r   r   r   r   r   r     s   c               @   s   e Zd ZeZdS )r   N)r8   r9   r:   r   r   r   r   r   r   r     s   c               @   s   e Zd ZeZdS )r   N)r8   r9   r:   r   r   r   r   r   r   r     s   ).r;   r   _compatr   r   r   r   r   Zbeaker.utilrj   loggingr|   rY   Zbeaker.exceptionsr   r	   Zbeaker.synchronizationr
   r   r   r   r   __all__	getLoggerloggerisEnabledForDEBUGr   objectr   r   r   rb   r   r   r   r   rh   r   r   r   r   r   r   r   ZDbmContainerr   r   r   r   <module>   sF   
 V $6vd