a
    á `k  ã                   @   sn   d Z 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	 ddlm
Z
 e e¡ZG dd	„ d	eƒZdS )
zPlugin storage class.é    N)ÚAny)ÚDict)Úerrors)Ú
filesystem)Úosc                   @   s@   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S )ÚPluginStoragez4Class implementing storage functionality for pluginsc                 C   s"   || _ || _d| _d| _d| _dS )zñInitializes PluginStorage object storing required configuration
        options.

        :param .configuration.NamespaceConfig config: Configuration object
        :param str classkey: class name to use as root key in storage file

        FN)Ú_configÚ	_classkeyÚ_initializedÚ_dataÚ_storagepath)ÚselfZconfigZclasskey© r   ú9/usr/lib/python3/dist-packages/certbot/plugins/storage.pyÚ__init__   s
    	zPluginStorage.__init__c                 C   s&   t j | jjd¡| _|  ¡  d| _dS )zhInitializes PluginStorage data and reads current state from the disk
        if the storage json exists.z.pluginstorage.jsonTN)r   ÚpathÚjoinr   Z
config_dirr   Ú_loadr
   )r   r   r   r   Ú_initialize_storage    s    z!PluginStorage._initialize_storagec              
   C   sú   i }d}z8t | jdƒ}| ¡ }W d  ƒ n1 s40    Y  W nX ty˜ } z@d | jt|ƒ¡}tj | j¡r„t	 
|¡ t |¡‚W Y d}~n
d}~0 0 zt |¡}W nF tyî   |sÊt	 d| j¡ n d | j¡}t	 
|¡ t |¡‚Y n0 || _dS )z˜Reads PluginStorage content from the disk to a dict structure

        :raises .errors.PluginStorageError: when unable to open or read the file
        Ú ÚrNz1Could not read PluginStorage data file: {0} : {1}z2Plugin storage file %s was empty, no values loadedz$PluginStorage file {0} is corrupted.)Úopenr   ÚreadÚIOErrorÚformatÚstrr   r   ÚisfileÚloggerÚerrorr   ÚPluginStorageErrorÚjsonÚloadsÚ
ValueErrorÚdebugr   )r   ÚdataZfiledataÚfhÚeÚerrmsgr   r   r   r   (   s2    *
ÿ
 ÿÿ
zPluginStorage._loadc              
   C   s  | j sd}t |¡ t |¡‚zt | j¡}W nF tyt } z.d 	t
|ƒ¡}t |¡ t |¡‚W Y d}~n
d}~0 0 zTt t | jtjtjB tjB d¡d¡}| |¡ W d  ƒ n1 s¾0    Y  W nL ty } z2d 	| jt
|ƒ¡}t |¡ t |¡‚W Y d}~n
d}~0 0 dS )z¥Saves PluginStorage content to disk

        :raises .errors.PluginStorageError: when unable to serialize the data
            or write it to the filesystem
        z;Unable to save, no values have been added to PluginStorage.z+Could not serialize PluginStorage data: {0}Ni€  Úwz4Could not write PluginStorage data to file {0} : {1})r
   r   r   r   r   r    Údumpsr   Ú	TypeErrorr   r   r   Úfdopenr   r   r   ÚO_WRONLYÚO_CREATÚO_TRUNCÚwriter   )r   r'   Z
serializedr&   r%   r   r   r   ÚsaveF   s8    

ÿ
 ýý,
ÿ
zPluginStorage.savec                 C   s:   | j s|  ¡  | j| jvr&i | j| j< || j| j |< dS )zˆPut configuration value to PluginStorage

        :param str key: Key to store the value to
        :param value: Data to store
        N)r
   r   r	   r   )r   ÚkeyÚvaluer   r   r   Úputd   s
    zPluginStorage.putc                 C   s   | j s|  ¡  | j| j | S )z±Get configuration value from PluginStorage

        :param str key: Key to get value from the storage

        :raises KeyError: If the key doesn't exist in the storage
        )r
   r   r   r	   )r   r1   r   r   r   Úfetchq   s    zPluginStorage.fetchN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r0   r3   r4   r   r   r   r   r      s   r   )r8   r    ZloggingZacme.magic_typingr   r   Zcertbotr   Zcertbot.compatr   r   Z	getLoggerr5   r   Úobjectr   r   r   r   r   Ú<module>   s   
