3
AAd]S  ã               @   sH   d dl Z d dlZd dlZd dlmZ G dd„ deƒZG dd„ deƒZdS )é    N)Ú	ETSConfigc               @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )Ú
Persistentz¦This persists a Traits class to a file.  It is used by the default
    permissions policy and user manager to implement basic (ie.  insecure)
    shared data storage.c             C   s`   || _ || _tjjdtjƒ}tjj||ƒ| _	| j	d | _
ytj|ƒ W n tk
rZ   Y nX dS )a„  Initialise the object.  factory is a callable that will create a
        new instance if there is no existing data.  file_name is the name of
        the file in the ETSConfig.application_home directory (or the directory
        specified by the ETS_PERMS_DATA_DIR environment variable if set) to
        persist the data to.  desc is a description of the data used in
        exceptions.ZETS_PERMS_DATA_DIRz.lockN)Ú_factoryÚ_descÚosÚenvironÚgetr   Zapplication_homeÚpathÚjoinÚ_fnameÚ_lockÚmakedirsÚOSError)ÚselfÚfactoryÚ	file_nameÚdescZdata_dir© r   úƒ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/apptools-4.4.0-py3.6.egg/apptools/permissions/default/persistent.pyÚ__init__   s    zPersistent.__init__c             C   sf   yt j| jƒ W nP tk
r` } z4|jtjkr:d| j }nd| j|f }t|ƒ‚W Y dd}~X nX dS )z$Obtain a lock on the persisted data.z6The lock on %s is held by another application or user.z!Unable to acquire lock on %s: %s.N)r   Úmkdirr   r   ÚerrnoÚEEXISTr   ÚPersistentError)r   ÚeÚmsgr   r   r   Úlock4   s    zPersistent.lockc             C   sJ   yt j| jƒ W n4 tk
rD } ztd| j|f ƒ‚W Y dd}~X nX dS )z'Release the lock on the persisted data.z!Unable to release lock on %s: %s.N)r   Úrmdirr   r   r   r   )r   r   r   r   r   ÚunlockA   s    zPersistent.unlockc             C   sš   yJt | jdƒ}z.ytj|ƒ}W n   td| j ƒ‚Y nX W d|jƒ  X W nJ tk
r” } z.|jtj	krr| j
ƒ }ntd| j|f ƒ‚W Y dd}~X nX |S )z#Read and return the persisted data.ÚrzUnable to read %s.NzUnable to open %s: %s.)Úopenr   ÚpickleÚloadr   r   ÚcloseÚIOErrorr   ÚENOENTr   )r   ÚfÚdatar   r   r   r   ÚreadI   s    
$zPersistent.readc             C   s†   yLt | jdƒ}z0ytj||ƒ W n   td| j ƒ‚Y nX W d|jƒ  X W n4 tk
r€ } ztd| j|f ƒ‚W Y dd}~X nX dS )zWrite the persisted data.ÚwzUnable to write %s.NzUnable to create %s: %s.)r    r   r!   Údumpr   r   r#   r$   )r   r'   r&   r   r   r   r   Úwrite^   s    zPersistent.writeN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r(   r+   r   r   r   r   r      s   r   c               @   s   e Zd ZdZdS )r   z6The class used for all persistence related exceptions.N)r,   r-   r.   r/   r   r   r   r   r   o   s   r   )	r!   r   r   Ztraits.etsconfig.apir   Úobjectr   Ú	Exceptionr   r   r   r   r   Ú<module>   s
   V