<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import os <EOL> import stat <EOL> import threading <EOL> from anyjson import simplejson <EOL> from client import Storage as BaseStorage <EOL> from client import Credentials <EOL> class CredentialsFileSymbolicLinkError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class Storage ( BaseStorage ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename ) : <EOL> self . _filename = filename <EOL> self . _lock = threading . Lock ( ) <EOL> def _validate_file ( self ) : <EOL> if os . path . islink ( self . _filename ) : <EOL> raise CredentialsFileSymbolicLinkError ( <EOL> '<STR_LIT>' % self . _filename ) <EOL> def acquire_lock ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _lock . acquire ( ) <EOL> def release_lock ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _lock . release ( ) <EOL> def locked_get ( self ) : <EOL> """<STR_LIT>""" <EOL> credentials = None <EOL> self . _validate_file ( ) <EOL> try : <EOL> f = open ( self . _filename , '<STR_LIT:rb>' ) <EOL> content = f . read ( ) <EOL> f . close ( ) <EOL> except IOError : <EOL> return credentials <EOL> try : <EOL> credentials = Credentials . new_from_json ( content ) <EOL> credentials . set_store ( self ) <EOL> except ValueError : <EOL> pass <EOL> return credentials <EOL> def _create_file_if_needed ( self ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( self . _filename ) : <EOL> old_umask = os . umask ( <NUM_LIT:0> <NUM_LIT> ) <EOL> try : <EOL> open ( self . _filename , '<STR_LIT>' ) . close ( ) <EOL> finally : <EOL> os . umask ( old_umask ) <EOL> def locked_put ( self , credentials ) : <EOL> """<STR_LIT>""" <EOL> self . _create_file_if_needed ( ) <EOL> self . _validate_file ( ) <EOL> f = open ( self . _filename , '<STR_LIT:wb>' ) <EOL> f . write ( credentials . to_json ( ) ) <EOL> f . close ( ) <EOL> def locked_delete ( self ) : <EOL> """<STR_LIT>""" <EOL> os . unlink ( self . _filename ) </s>
