<s> """<STR_LIT>""" <EOL> import sys <EOL> import types <EOL> import fnmatch <EOL> from os . path import basename <EOL> from pygments . lexers . _mapping import LEXERS <EOL> from pygments . plugin import find_plugin_lexers <EOL> from pygments . util import ClassNotFound , bytes <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] + LEXERS . keys ( ) <EOL> _lexer_cache = { } <EOL> def _load_lexers ( module_name ) : <EOL> """<STR_LIT>""" <EOL> mod = __import__ ( module_name , None , None , [ '<STR_LIT>' ] ) <EOL> for lexer_name in mod . __all__ : <EOL> cls = getattr ( mod , lexer_name ) <EOL> _lexer_cache [ cls . name ] = cls <EOL> def get_all_lexers ( ) : <EOL> """<STR_LIT>""" <EOL> for item in LEXERS . itervalues ( ) : <EOL> yield item [ <NUM_LIT:1> : ] <EOL> for lexer in find_plugin_lexers ( ) : <EOL> yield lexer . name , lexer . aliases , lexer . filenames , lexer . mimetypes <EOL> def find_lexer_class ( name ) : <EOL> """<STR_LIT>""" <EOL> if name in _lexer_cache : <EOL> return _lexer_cache [ name ] <EOL> for module_name , lname , aliases , _ , _ in LEXERS . itervalues ( ) : <EOL> if name == lname : <EOL> _load_lexers ( module_name ) <EOL> return _lexer_cache [ name ] <EOL> for cls in find_plugin_lexers ( ) : <EOL> if cls . name == name : <EOL> return cls <EOL> def get_lexer_by_name ( _alias , ** options ) : <EOL> """<STR_LIT>""" <EOL> for module_name , name , aliases , _ , _ in LEXERS . itervalues ( ) : <EOL> if _alias in aliases : <EOL> if name not in _lexer_cache : <EOL> _load_lexers ( module_name ) <EOL> return _lexer_cache [ name ] ( ** options ) <EOL> for cls in find_plugin_lexers ( ) : <EOL> if _alias in cls . aliases : <EOL> return cls ( ** options ) <EOL> raise ClassNotFound ( '<STR_LIT>' % _alias ) <EOL> def get_lexer_for_filename ( _fn , code = None , ** options ) : <EOL> """<STR_LIT>""" <EOL> matches = [ ] <EOL> fn = basename ( _fn ) <EOL> for modname , name , _ , filenames , _ in LEXERS . itervalues ( ) : <EOL> for filename in filenames : <EOL> if fnmatch . fnmatch ( fn , filename ) : <EOL> if name not in _lexer_cache : <EOL> _load_lexers ( modname ) <EOL> matches . append ( _lexer_cache [ name ] ) <EOL> for cls in find_plugin_lexers ( ) : <EOL> for filename in cls . filenames : <EOL> if fnmatch . fnmatch ( fn , filename ) : <EOL> matches . append ( cls ) <EOL> if sys . version_info > ( <NUM_LIT:3> , ) and isinstance ( code , bytes ) : <EOL> code = code . decode ( '<STR_LIT>' ) <EOL> def get_rating ( cls ) : <EOL> d = cls . analyse_text ( code ) <EOL> return d <EOL> if code : <EOL> matches . sort ( key = get_rating ) <EOL> if matches : <EOL> return matches [ - <NUM_LIT:1> ] ( ** options ) <EOL> raise ClassNotFound ( '<STR_LIT>' % _fn ) <EOL> def get_lexer_for_mimetype ( _mime , ** options ) : <EOL> """<STR_LIT>""" <EOL> for modname , name , _ , _ , mimetypes in LEXERS . itervalues ( ) : <EOL> if _mime in mimetypes : <EOL> if name not in _lexer_cache : <EOL> _load_lexers ( modname ) <EOL> return _lexer_cache [ name ] ( ** options ) <EOL> for cls in find_plugin_lexers ( ) : <EOL> if _mime in cls . mimetypes : <EOL> return cls ( ** options ) <EOL> raise ClassNotFound ( '<STR_LIT>' % _mime ) <EOL> def _iter_lexerclasses ( ) : <EOL> """<STR_LIT>""" <EOL> for module_name , name , _ , _ , _ in LEXERS . itervalues ( ) : <EOL> if name not in _lexer_cache : <EOL> _load_lexers ( module_name ) <EOL> yield _lexer_cache [ name ] <EOL> for lexer in find_plugin_lexers ( ) : <EOL> yield lexer <EOL> def guess_lexer_for_filename ( _fn , _text , ** options ) : <EOL> """<STR_LIT>""" <EOL> fn = basename ( _fn ) <EOL> primary = None <EOL> matching_lexers = set ( ) <EOL> for lexer in _iter_lexerclasses ( ) : <EOL> for filename in lexer . filenames : <EOL> if fnmatch . fnmatch ( fn , filename ) : <EOL> matching_lexers . add ( lexer ) <EOL> primary = lexer <EOL> for filename in lexer . alias_filenames : <EOL> if fnmatch . fnmatch ( fn , filename ) : <EOL> matching_lexers . add ( lexer ) <EOL> if not matching_lexers : <EOL> raise ClassNotFound ( '<STR_LIT>' % fn ) <EOL> if len ( matching_lexers ) == <NUM_LIT:1> : <EOL> return matching_lexers . pop ( ) ( ** options ) <EOL> result = [ ] <EOL> for lexer in matching_lexers : <EOL> rv = lexer . analyse_text ( _text ) <EOL> if rv == <NUM_LIT:1.0> : <EOL> return lexer ( ** options ) <EOL> result . append ( ( rv , lexer ) ) <EOL> result . sort ( ) <EOL> if not result [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] and primary is not None : <EOL> return primary ( ** options ) <EOL> return result [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ( ** options ) <EOL> def guess_lexer ( _text , ** options ) : <EOL> """<STR_LIT>""" <EOL> best_lexer = [ <NUM_LIT:0.0> , None ] <EOL> for lexer in _iter_lexerclasses ( ) : <EOL> rv = lexer . analyse_text ( _text ) <EOL> if rv == <NUM_LIT:1.0> : <EOL> return lexer ( ** options ) <EOL> if rv > best_lexer [ <NUM_LIT:0> ] : <EOL> best_lexer [ : ] = ( rv , lexer ) <EOL> if not best_lexer [ <NUM_LIT:0> ] or best_lexer [ <NUM_LIT:1> ] is None : <EOL> raise ClassNotFound ( '<STR_LIT>' ) <EOL> return best_lexer [ <NUM_LIT:1> ] ( ** options ) <EOL> class _automodule ( types . ModuleType ) : <EOL> """<STR_LIT>""" <EOL> def __getattr__ ( self , name ) : <EOL> info = LEXERS . get ( name ) <EOL> if info : <EOL> _load_lexers ( info [ <NUM_LIT:0> ] ) <EOL> cls = _lexer_cache [ info [ <NUM_LIT:1> ] ] <EOL> setattr ( self , name , cls ) <EOL> return cls <EOL> raise AttributeError ( name ) <EOL> oldmod = sys . modules [ '<STR_LIT>' ] <EOL> newmod = _automodule ( '<STR_LIT>' ) <EOL> newmod . __dict__ . update ( oldmod . __dict__ ) <EOL> sys . modules [ '<STR_LIT>' ] = newmod <EOL> del newmod . newmod , newmod . oldmod , newmod . sys , newmod . types </s>
