o 6aB@sldZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z e e Z Gdd d ZdS) zPlugin storage class.N)Any)Dict)errors) filesystem)osc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) PluginStoragez4Class implementing storage functionality for pluginscCs||_||_d|_||dS)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)selfconfigclasskeyr9/usr/lib/python3/dist-packages/certbot/plugins/storage.py__init__s  zPluginStorage.__init__cCs&tj|jjd|_|d|_dS)zhInitializes PluginStorage data and reads current state from the disk if the storage json exists.z.pluginstorage.jsonTN)rpathjoinr config_dir _storagepath_loadr )r rrr_initialize_storage s z!PluginStorage._initialize_storagec Csi}d}zt|jd }|}Wdn1swYWn,tyL}z d|jt|}tj|jrBt |t |WYd}~nd}~wwzt |}Wn#tyw|set d|jnd|j}t |t |Ynw||_dS)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.)openrreadIOErrorformatstrrrisfileloggererrorrPluginStorageErrorjsonloads ValueErrordebug_data)r datafiledatafheerrmsgrrrr(s@         zPluginStorage._loadc Cs|jsd}t|t|zt|j}Wnty4}zd t |}t|t|d}~wwz-t t |jt jt jBt jBdd}||WdWdS1s[wYWdSty}zd |jt |}t|t|d}~ww)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}Niwz4Could not write PluginStorage data to file {0} : {1})r rr rr!r"dumpsr& TypeErrorrrrfdopenrrrO_WRONLYO_CREATO_TRUNCwriter)r r+ serializedr*r)rrrsaveFsB     &   zPluginStorage.savecCs:|js||j|jvri|j|j<||j|j|<dS)zPut configuration value to PluginStorage :param str key: Key to store the value to :param value: Data to store N)r rr r&)r keyvaluerrrputds   zPluginStorage.putcCs|js||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 rr&r )r r6rrrfetchqszPluginStorage.fetchN) __name__ __module__ __qualname____doc__rrrr5r8r9rrrrrs r)r=r"loggingtypingrrcertbotrcertbot.compatrr getLoggerr:rrrrrrs