3 گa@sxdZddlZddlZddlmZddlmZddlmZddlmZddl m Z ddl m Z ej e ZGd d d ZdS) zPlugin storage class.N)Any)Dict) configuration)errors) filesystem)osc@speZdZdZejeddddZddddZddd d Z ddd d Z ee dd ddZ ee dddZ dS) PluginStoragez4Class implementing storage functionality for pluginsN)configclasskeyreturncCs||_||_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)selfr r r/usr/lib/python3.6/storage.py__init__s zPluginStorage.__init__)r cCs&tjj|jjd|_|jd|_dS)zhInitializes PluginStorage data and reads current state from the disk if the storage json exists.z.pluginstorage.jsonTN)rpathjoinr Z config_dir _storagepath_loadr)rrrr_initialize_storage!sz!PluginStorage._initialize_storagecCsi}d}y$t|jd}|j}WdQRXWnVtk r}z:dj|jt|}tjj|jrrt j |t j |WYdd}~XnXyt j|}WnHtk r|st jd|jn dj|j}t j |t j |YnX||_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)rdataZfiledatafheerrmsgrrrr)s,   zPluginStorage._loadcCs|jsd}tj|tj|ytj|j}WnDtk rr}z(dj t |}tj|tj|WYdd}~XnXy@t j t j|jt jt jBt jBdd}|j|WdQRXWnHtk r}z,dj |jt |}tj|tj|WYdd}~XnXdS)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})rr r!rr"r#dumpsr' TypeErrorrrrfdopenrrrO_WRONLYO_CREATO_TRUNCwriter)rr+Z serializedr*r)rrrsaveGs,      zPluginStorage.save)keyvaluer cCs:|js|j|j|jkr&i|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)rrr r')rr5r6rrrputes   zPluginStorage.put)r5r cCs|js|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 )rrr'r )rr5rrrfetchrszPluginStorage.fetch)__name__ __module__ __qualname____doc__rZNamespaceConfigrrrrr4rr7r8rrrrrs r)r<r#ZloggingZtypingrrZcertbotrrZcertbot.compatrrZ getLoggerr9r rrrrrs