U
    ]                     @   sf   d Z ddlZddlZddlmZ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)os)
filesystemc                   @   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 )zInitializes 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   t  }d}z$t| jd}| }W 5 Q R X W nV tk
r } z8d| jt|}tj	| jrtt
| t|W 5 d}~X Y nX zt|}W nH tk
r   |st
d| j n d| j}t
| t|Y nX || _dS )zReads PluginStorage content from the disk to a dict structure

        :raises .errors.PluginStorageError: when unable to open or read the file
         rz1Could not read PluginStorage data file: {0} : {1}Nz2Plugin storage file %s was empty, no values loadedz$PluginStorage file {0} is corrupted.)dictopenr   readIOErrorformatstrr   r   isfileloggererrorr   PluginStorageErrorjsonloads
ValueErrordebugr   )r   dataZfiledatafheerrmsgr   r   r   r   (   s4     

zPluginStorage._loadc              
   C   s  | j sd}t| t|zt| j}W nD tk
rr } z&d	t
|}t| t|W 5 d}~X Y nX z@tt| jtjtjB tjB dd}|| W 5 Q R X W nH tk
r } z*d	| jt
|}t| t|W 5 d}~X Y nX dS )zSaves 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   s:    


 
zPluginStorage.savec                 C   s@   | j s|   | j| j kr,t | j| j< || j| j |< dS )zPut configuration value to PluginStorage

        :param str key: Key to store the value to
        :param value: Data to store
        N)r
   r   r	   r   keysr   )r   keyvaluer   r   r   putd   s
    zPluginStorage.putc                 C   s   | j s|   | j| j | S )zGet 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   r3   r   r   r   fetchq   s    zPluginStorage.fetchN)
__name__
__module____qualname____doc__r   r   r   r1   r5   r6   r   r   r   r   r      s   r   )r:   r!   ZloggingZacme.magic_typingr   r   Zcertbotr   Zcertbot.compatr   r   Z	getLoggerr7   r   objectr   r   r   r   r   <module>   s   
