U
    ÓZj’  ã                   @   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 )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)ÚselfÚconfigZclasskey© r   úZ/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-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Ü   |s¸t
 d| j¡ n d | j¡}t
 |¡ t |¡‚Y nX || _dS )z˜Reads 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 )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   s:    

ÿ
ýý ÿ
zPluginStorage.savec                 C   s@   | j s|  ¡  | j| j ¡ kr,tƒ | 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   Úkeysr   )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   r4   r   r   r   Úfetchq   s    zPluginStorage.fetchN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r2   r6   r7   r   r   r   r   r      s   r   )r;   r"   ÚloggingZacme.magic_typingr   r   Zcertbotr   Zcertbot.compatr   r   Ú	getLoggerr8   r   Úobjectr   r   r   r   r   Ú<module>   s   
