<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import sys <EOL> from . compat import u , open <EOL> from . dependencies import DependencyParser <EOL> from . packages import ( <EOL> get_lexer_by_name , <EOL> guess_lexer_for_filename , <EOL> get_filetype_from_buffer , <EOL> ClassNotFound , <EOL> ) <EOL> try : <EOL> from . packages import simplejson as json <EOL> except ( ImportError , SyntaxError ) : <EOL> import json <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> def guess_language ( file_name ) : <EOL> """<STR_LIT>""" <EOL> language = get_language_from_extension ( file_name ) <EOL> lexer = smart_guess_lexer ( file_name ) <EOL> if language is None and lexer is not None : <EOL> language = u ( lexer . name ) <EOL> return language , lexer <EOL> def smart_guess_lexer ( file_name ) : <EOL> """<STR_LIT>""" <EOL> lexer = None <EOL> text = get_file_head ( file_name ) <EOL> lexer1 , accuracy1 = guess_lexer_using_filename ( file_name , text ) <EOL> lexer2 , accuracy2 = guess_lexer_using_modeline ( text ) <EOL> if lexer1 : <EOL> lexer = lexer1 <EOL> if ( lexer2 and accuracy2 and <EOL> ( not accuracy1 or accuracy2 > accuracy1 ) ) : <EOL> lexer = lexer2 <EOL> return lexer <EOL> def guess_lexer_using_filename ( file_name , text ) : <EOL> """<STR_LIT>""" <EOL> lexer , accuracy = None , None <EOL> try : <EOL> lexer = guess_lexer_for_filename ( file_name , text ) <EOL> except : <EOL> pass <EOL> if lexer is not None : <EOL> try : <EOL> accuracy = lexer . analyse_text ( text ) <EOL> except : <EOL> pass <EOL> return lexer , accuracy <EOL> def guess_lexer_using_modeline ( text ) : <EOL> """<STR_LIT>""" <EOL> lexer , accuracy = None , None <EOL> file_type = None <EOL> try : <EOL> file_type = get_filetype_from_buffer ( text ) <EOL> except : <EOL> pass <EOL> if file_type is not None : <EOL> try : <EOL> lexer = get_lexer_by_name ( file_type ) <EOL> except ClassNotFound : <EOL> pass <EOL> if lexer is not None : <EOL> try : <EOL> accuracy = lexer . analyse_text ( text ) <EOL> except : <EOL> pass <EOL> return lexer , accuracy <EOL> def get_language_from_extension ( file_name ) : <EOL> """<STR_LIT>""" <EOL> filepart , extension = os . path . splitext ( file_name ) <EOL> if os . path . exists ( u ( '<STR_LIT>' ) . format ( u ( filepart ) , u ( '<STR_LIT>' ) ) ) or os . path . exists ( u ( '<STR_LIT>' ) . format ( u ( filepart ) , u ( '<STR_LIT>' ) ) ) : <EOL> return '<STR_LIT:C>' <EOL> extension = extension . lower ( ) <EOL> if extension == '<STR_LIT>' : <EOL> directory = os . path . dirname ( file_name ) <EOL> available_files = os . listdir ( directory ) <EOL> available_extensions = list ( zip ( * map ( os . path . splitext , available_files ) ) ) [ <NUM_LIT:1> ] <EOL> available_extensions = [ ext . lower ( ) for ext in available_extensions ] <EOL> if '<STR_LIT>' in available_extensions : <EOL> return '<STR_LIT>' <EOL> if '<STR_LIT>' in available_extensions : <EOL> return '<STR_LIT:C>' <EOL> return None <EOL> def number_lines_in_file ( file_name ) : <EOL> lines = <NUM_LIT:0> <EOL> try : <EOL> with open ( file_name , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) as fh : <EOL> for line in fh : <EOL> lines += <NUM_LIT:1> <EOL> except : <EOL> try : <EOL> with open ( file_name , '<STR_LIT:r>' , encoding = sys . getfilesystemencoding ( ) ) as fh : <EOL> for line in fh : <EOL> lines += <NUM_LIT:1> <EOL> except : <EOL> return None <EOL> return lines <EOL> def get_file_stats ( file_name , entity_type = '<STR_LIT:file>' , lineno = None , cursorpos = None , <EOL> plugin = None , alternate_language = None ) : <EOL> if entity_type != '<STR_LIT:file>' : <EOL> stats = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : lineno , <EOL> '<STR_LIT>' : cursorpos , <EOL> } <EOL> else : <EOL> language , lexer = guess_language ( file_name ) <EOL> parser = DependencyParser ( file_name , lexer ) <EOL> dependencies = parser . parse ( ) <EOL> if language is None and alternate_language : <EOL> language = standardize_language ( alternate_language , plugin ) <EOL> stats = { <EOL> '<STR_LIT>' : language , <EOL> '<STR_LIT>' : dependencies , <EOL> '<STR_LIT>' : number_lines_in_file ( file_name ) , <EOL> '<STR_LIT>' : lineno , <EOL> '<STR_LIT>' : cursorpos , <EOL> } <EOL> return stats <EOL> def standardize_language ( language , plugin ) : <EOL> """<STR_LIT>""" <EOL> if plugin : <EOL> plugin = plugin . split ( '<STR_LIT:U+0020>' ) [ - <NUM_LIT:1> ] . split ( '<STR_LIT:/>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:->' ) [ <NUM_LIT:0> ] <EOL> standardized = get_language_from_json ( language , plugin ) <EOL> if standardized is not None : <EOL> return standardized <EOL> standardized = get_language_from_json ( language , '<STR_LIT:default>' ) <EOL> if standardized is not None : <EOL> return standardized <EOL> return None <EOL> def get_language_from_json ( language , key ) : <EOL> """<STR_LIT>""" <EOL> file_name = os . path . join ( <EOL> os . path . dirname ( __file__ ) , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) . format ( key . lower ( ) ) <EOL> try : <EOL> with open ( file_name , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) as fh : <EOL> languages = json . loads ( fh . read ( ) ) <EOL> if language in languages . values ( ) : <EOL> return language <EOL> if languages . get ( language ) : <EOL> return languages [ language ] <EOL> except : <EOL> pass <EOL> return None <EOL> def get_file_head ( file_name ) : <EOL> """<STR_LIT>""" <EOL> text = None <EOL> try : <EOL> with open ( file_name , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) as fh : <EOL> text = fh . read ( <NUM_LIT> ) <EOL> except : <EOL> try : <EOL> with open ( file_name , '<STR_LIT:r>' , encoding = sys . getfilesystemencoding ( ) ) as fh : <EOL> text = fh . read ( <NUM_LIT> ) <EOL> except : <EOL> log . traceback ( '<STR_LIT>' ) <EOL> return text </s>
