a ‚oeÆã@sxdZddlZddlZddlmZddlmZddlmZddlmZddl m Z ddl m Z e  e ¡ZGd d „d ƒZdS) zPlugin storage class.éN)ÚAny)ÚDict)Ú configuration)Úerrors)Ú filesystem)Úosc@speZdZdZejeddœdd„Zddœdd„Zddœd d „Z ddœd d „Z ee dd œdd„Z ee dœdd„Z dS)Ú PluginStoragez4Class implementing storage functionality for pluginsN)ÚconfigÚclasskeyÚreturncCs||_||_d|_||dS)zñ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 FN)Ú_configÚ _classkeyÚ _initialized)Úselfr r ©rú8C:\Program Files\Certbot\pkgs\certbot\plugins\storage.pyÚ__init__s zPluginStorage.__init__)r cCs&tj |jjd¡|_| ¡d|_dS)zhInitializes PluginStorage data and reads current state from the disk if the storage json exists.z.pluginstorage.jsonTN)rÚpathÚjoinr Z config_dirÚ _storagepathÚ_loadr)rrrrÚ_initialize_storage!sz!PluginStorage._initialize_storagec Csúi}d}z8t|jdƒ�}| ¡}Wdƒn1s40YWnXty˜}z@d |jt|ƒ¡}tj |j¡r„t   |¡t   |¡‚WYd}~n d}~00zt  |¡}WnFtyî|sÊt  d|j¡n d |j¡}t   |¡t   |¡‚Yn0||_dS)z˜Reads 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ÚstrrrÚisfileÚloggerÚerrorrÚPluginStorageErrorÚjsonÚloadsÚ ValueErrorÚdebugÚ_data)rÚdataZfiledataÚfhÚeÚerrmsgrrrr)s2* ÿ   ÿÿ zPluginStorage._loadc Cs|jsd}t |¡t |¡‚zt |j¡}WnFtyt}z.d  t |ƒ¡}t |¡t |¡‚WYd}~n d}~00zTt   t  |jt jt jBt jBd¡d¡�}| |¡Wdƒn1s¾0YWnLt�y}z2d  |jt |ƒ¡}t |¡t |¡‚WYd}~n d}~00dS)z¥Saves 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})rr r!rr"r#Údumpsr'Ú TypeErrorrrrÚfdopenrrrÚO_WRONLYÚO_CREATÚO_TRUNCÚwriter)rr+Z serializedr*r)rrrÚsaveGs8  ÿ  ýý, ÿ zPluginStorage.save)ÚkeyÚvaluer cCs:|js| ¡|j|jvr&i|j|j<||j|j|<dS)zˆPut configuration value to PluginStorage :param str key: Key to store the value to :param value: Data to store N)rrr r')rr5r6rrrÚputes   zPluginStorage.put)r5r cCs|js| ¡|j|j|S)z±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 )rrr'r )rr5rrrÚfetchrszPluginStorage.fetch)Ú__name__Ú __module__Ú __qualname__Ú__doc__rZNamespaceConfigrrrrr4rr7r8rrrrrs r)r<r#ZloggingÚtypingrrZcertbotrrZcertbot.compatrrZ getLoggerr9r rrrrrÚs