
    M/e                         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
 ddl	mZ  ej                  e      Z G d	 d
      Zy)zPlugin storage class.    N)Any)Dict)configuration)errors)
filesystem)osc                   t    e Zd ZdZdej
                  deddfdZddZddZ	dd	Z
d
ededdfdZd
edefdZy)PluginStoragez4Class implementing storage functionality for pluginsconfigclasskeyreturnNc                 6    || _         || _        d| _        |  |  y)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)selfr   r   s      9/usr/lib/python3/dist-packages/certbot/plugins/storage.py__init__zPluginStorage.__init__   s      !!    c                     t         j                  j                  | j                  j                  d      | _        | j                          d| _        y)zhInitializes PluginStorage data and reads current state from the disk
        if the storage json exists.z.pluginstorage.jsonTN)r   pathjoinr   
config_dir_storagepath_loadr   )r   s    r   _initialize_storagez!PluginStorage._initialize_storage!   s7     GGLL)@)@BWX

 r   c                    i }d}	 t        | j                  d      5 }|j                         }ddd       	 t        j                  |      }|| _        y# 1 sw Y   'xY w# t        $ r}dj	                  | j                  t        |            }t        j                  j                  | j                        r*t        j                  |       t        j                  |      Y d}~d}~ww xY w# t        $ rr |s!t        j!                  d| j                         nEdj	                  | j                        }t        j                  |       t        j                  |      Y || _        yw xY w)zReads 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debug_data)r   datafiledatafheerrmsgs         r   r   zPluginStorage._load)   s9   
  "		8d''- %779%
	8::h'D 
)% % 	8HOO!!3q6+Fww~~d//0V$//77 1	8  	8Q!..0 @FF%%'V$//770 
	8s@   A AA C- AA 	C*(A8C%%C*-A1E('E(c                 6   | j                   s,d}t        j                  |       t        j                  |      	 t        j                  | j                        }	 t        j                  t        j                  | j                  t        j                   t        j"                  z  t        j$                  z  d      d      5 }|j'                  |       ddd       y# t        $ rI}dj                  t        |            }t        j                  |       t        j                  |      d}~ww xY w# 1 sw Y   yxY w# t(        $ rT}dj                  | j                  t        |            }t        j                  |       t        j                  |      d}~ww xY w)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   r2   
serializedr1   r0   s        r   savezPluginStorage.saveG   sJ      RFLL ++F33	4DJJ/J
	4:??%%KK"**,rzz9  ! % %'$	% %  	4BIIAFLL ++F33		4% %
  	4KRR!!3q6+FLL ++F33		4sP   C A%D; ?D/D; 	D,#AD''D,/D84D; 8D; ;	FAFFkeyvaluec                     | j                   s| j                          | j                  | j                  vri | j                  | j                  <   || j                  | j                     |<   y)zPut configuration value to PluginStorage

        :param str key: Key to store the value to
        :param value: Data to store
        N)r   r   r   r-   )r   r>   r?   s      r   putzPluginStorage.pute   sR       $$&>>+)+DJJt~~&*/

4>>"3'r   c                 r    | j                   s| j                          | 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   r>   s     r   fetchzPluginStorage.fetchr   s1       $$&zz$..)#..r   )r   N)__name__
__module____qualname____doc__r   NamespaceConfigr$   r   r   r   r=   r   rA   rC    r   r   r
   r
      sb    >}<<  PT !<4<0s 03 04 0
/ 
/ 
/r   r
   )rG   r)   loggingtypingr   r   certbotr   r   certbot.compatr   r   	getLoggerrD   r&   r
   rI   r   r   <module>rO      s>         !  % 			8	$m/ m/r   