ó
Į£ō_c           @   s   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 j e  Z d e f d	     YZ d S(
   s   Plugin storage class.i’’’’N(   t   Any(   t   Dict(   t   errors(   t
   filesystem(   t   ost   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/site-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(	   R   t   patht   joinR   t
   config_dirR   t   _loadt   TrueR	   (   R   (    (    s;   /usr/lib/python2.7/site-packages/certbot/plugins/storage.pyt   _initialize_storage    s    
c         C   s  i  } 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   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/site-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   R   t   O_WRONLYt   O_CREATt   O_TRUNCt   writeR   (   R   R*   t
   serializedR)   R(   (    (    s;   /usr/lib/python2.7/site-packages/certbot/plugins/storage.pyt   saveF   s,    	c         C   sS   |  j  s |  j   n  |  j |  j k r; i  |  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   (   R   t   keyt   value(    (    s;   /usr/lib/python2.7/site-packages/certbot/plugins/storage.pyt   putd   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   R5   (    (    s;   /usr/lib/python2.7/site-packages/certbot/plugins/storage.pyt   fetchq   s    	(	   t   __name__t
   __module__t   __doc__R   R   R   R4   R7   R8   (    (    (    s;   /usr/lib/python2.7/site-packages/certbot/plugins/storage.pyR      s   					(   R;   R"   t   loggingt   acme.magic_typingR    R   t   certbotR   t   certbot.compatR   R   t	   getLoggerR9   R   t   objectR   (    (    (    s;   /usr/lib/python2.7/site-packages/certbot/plugins/storage.pyt   <module>   s   