<s> import os <EOL> import json <EOL> import logging <EOL> from botocore . exceptions import ProfileNotFound <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def register_assume_role_provider ( event_handlers ) : <EOL> event_handlers . register ( '<STR_LIT>' , <EOL> inject_assume_role_provider_cache , <EOL> unique_id = '<STR_LIT>' ) <EOL> def inject_assume_role_provider_cache ( session , ** kwargs ) : <EOL> try : <EOL> cred_chain = session . get_component ( '<STR_LIT>' ) <EOL> except ProfileNotFound : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return <EOL> provider = cred_chain . get_provider ( '<STR_LIT>' ) <EOL> provider . cache = JSONFileCache ( ) <EOL> class JSONFileCache ( object ) : <EOL> """<STR_LIT>""" <EOL> CACHE_DIR = os . path . expanduser ( os . path . join ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def __init__ ( self , working_dir = CACHE_DIR ) : <EOL> self . _working_dir = working_dir <EOL> def __contains__ ( self , cache_key ) : <EOL> actual_key = self . _convert_cache_key ( cache_key ) <EOL> return os . path . isfile ( actual_key ) <EOL> def __getitem__ ( self , cache_key ) : <EOL> """<STR_LIT>""" <EOL> actual_key = self . _convert_cache_key ( cache_key ) <EOL> try : <EOL> with open ( actual_key ) as f : <EOL> return json . load ( f ) <EOL> except ( OSError , ValueError , IOError ) : <EOL> raise KeyError ( cache_key ) <EOL> def __setitem__ ( self , cache_key , value ) : <EOL> full_key = self . _convert_cache_key ( cache_key ) <EOL> try : <EOL> file_content = json . dumps ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % value ) <EOL> if not os . path . isdir ( self . _working_dir ) : <EOL> os . makedirs ( self . _working_dir ) <EOL> with os . fdopen ( os . open ( full_key , <EOL> os . O_WRONLY | os . O_CREAT , <NUM_LIT> ) , '<STR_LIT:w>' ) as f : <EOL> f . truncate ( ) <EOL> f . write ( file_content ) <EOL> def _convert_cache_key ( self , cache_key ) : <EOL> full_path = os . path . join ( self . _working_dir , cache_key + '<STR_LIT>' ) <EOL> return full_path </s>
