ó
Y[c           @   sy   d  Z  d d l Z d d l Z d d l Z d d l m Z m Z d d l m Z e j	 e
  Z d e f d     YZ d S(   s   Plugin storage class.iĸĸĸĸN(   t   Anyt   Dict(   t   errorst   PluginStoragec           B   sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s4   Class implementing storage functionality for pluginsc         C   s1   | |  _  | |  _ t |  _ d |  _ d |  _ d S(   sņ   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

        N(   t   _configt	   _classkeyt   Falset   _initializedt   Nonet   _datat   _storagepath(   t   selft   configt   classkey(    (    s;   /usr/lib/python2.7/dist-packages/certbot/plugins/storage.pyt   __init__   s
    					c         C   s5   t  j j |  j j d  |  _ |  j   t |  _ d S(   sh   Initializes PluginStorage data and reads current state from the disk
        if the storage json exists.s   .pluginstorage.jsonN(	   t   ost   patht   joinR   t
   config_dirR
   t   _loadt   TrueR   (   R   (    (    s;   /usr/lib/python2.7/dist-packages/certbot/plugins/storage.pyt   _initialize_storage   s    
c         C   s  t    } d } y+ t |  j d   } | j   } Wd QXWnb t k
 r } d j |  j t |   } t j j	 |  j  r t
 j |  t j |   q n Xy t j |  } Wn[ t k
 r| sÞ t
 j d |  j  qd j |  j  } t
 j |  t j |   n X| |  _ d S(   s   Reads PluginStorage content from the disk to a dict structure

        :raises .errors.PluginStorageError: when unable to open or read the file
        t    t   rNs1   Could not read PluginStorage data file: {0} : {1}s2   Plugin storage file %s was empty, no values loadeds$   PluginStorage file {0} is corrupted.(   t   dictt   openR
   t   readt   IOErrort   formatt   strR   R   t   isfilet   loggert   errorR   t   PluginStorageErrort   jsont   loadst
   ValueErrort   debugR	   (   R   t   datat   filedatat   fht   et   errmsg(    (    s;   /usr/lib/python2.7/dist-packages/certbot/plugins/storage.pyR   %   s,    		c         C   s+  |  j  s. d } t j |  t j |   n  y t j |  j  } WnD t k
 r } d j	 t
 |   } t j |  t j |   n XyO t j t j |  j t j t j Bt j Bd  d   } | j |  Wd QXWnJ t k
 r&} d j	 |  j t
 |   } t j |  t j |   n Xd S(   sĨ   Saves PluginStorage content to disk

        :raises .errors.PluginStorageError: when unable to serialize the data
            or write it to the filesystem
        s;   Unable to save, no values have been added to PluginStorage.s+   Could not serialize PluginStorage data: {0}i  t   wNs4   Could not write PluginStorage data to file {0} : {1}(   R   R   R    R   R!   R"   t   dumpsR	   t	   TypeErrorR   R   R   t   fdopenR   R
   t   O_WRONLYt   O_CREATt   O_TRUNCt   writeR   (   R   R*   t
   serializedR)   R(   (    (    s;   /usr/lib/python2.7/dist-packages/certbot/plugins/storage.pyt   saveC   s*    	c         C   s\   |  j  s |  j   n  |  j |  j j   k rD t   |  j |  j <n  | |  j |  j | <d S(   s   Put configuration value to PluginStorage

        :param str key: Key to store the value to
        :param value: Data to store
        N(   R   R   R   R	   t   keysR   (   R   t   keyt   value(    (    s;   /usr/lib/python2.7/dist-packages/certbot/plugins/storage.pyt   put`   s
    	c         C   s(   |  j  s |  j   n  |  j |  j | S(   są   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   R6   (    (    s;   /usr/lib/python2.7/dist-packages/certbot/plugins/storage.pyt   fetchm   s    	(	   t   __name__t
   __module__t   __doc__R   R   R   R4   R8   R9   (    (    (    s;   /usr/lib/python2.7/dist-packages/certbot/plugins/storage.pyR      s   					(   R<   R"   t   loggingR   t   acme.magic_typingR    R   t   certbotR   t	   getLoggerR:   R   t   objectR   (    (    (    s;   /usr/lib/python2.7/dist-packages/certbot/plugins/storage.pyt   <module>   s   