#NEWLINE# def get_file_encoding( self , file_path , preferred_encoding = None ) : #NEWLINE# #INDENT# '\n        Gets an eventual cached encoding for file_path.\n\n        Raises a KeyError if no encoding were cached for the specified file\n        path.\n\n        :param file_path: path of the file to look up\n        :returns: The cached encoding.\n        ' #NEWLINE# _logger ( ) . debug ( 'getting encoding for %s' , file_path ) #NEWLINE# try : #NEWLINE# #INDENT# map = json . loads ( self . _settings . value ( 'cachedFileEncodings' ) ) #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# map = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# return map [ encodings ] #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# encodings = self . preferred_encodings #NEWLINE# if preferred_encoding : #NEWLINE# #INDENT# encodings . insert ( 0 , preferred_encoding ) #NEWLINE# #UNINDENT# for encoding in encodings : #NEWLINE# #INDENT# _logger ( ) . debug ( 'trying encoding: %s' , encoding ) #NEWLINE# try : #NEWLINE# #INDENT# with open ( file_path , encoding = encoding ) as f : #NEWLINE# #INDENT# f . read ( ) #NEWLINE# #UNINDENT# #UNINDENT# except ( UnicodeDecodeError , IOError , OSError ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return encoding #NEWLINE# #UNINDENT# #UNINDENT# raise KeyError ( file_path )