def get_lexer_for_filename(_fn, code=None, **options):
 matches = []
 fn = basename(_fn)
 for (modname, name, _, filenames, _) in LEXERS.itervalues():
  for filename in filenames:
   if fnmatch.fnmatch(fn, filename):
    if (name not in _lexer_cache):
     _load_lexers(modname)
    matches.append((_lexer_cache[name], filename))
 for cls in find_plugin_lexers():
  for filename in cls.filenames:
   if fnmatch.fnmatch(fn, filename):
    matches.append((cls, filename))
 if ((sys.version_info > (3,)) and isinstance(code, bytes)):
  code = code.decode('latin1')
 def get_rating(info):
  (cls, filename) = info
  bonus = ((('*' not in filename) and 0.5) or 0)
  if code:
   return (cls.analyse_text(code) + bonus)
  return (cls.priority + bonus)
 if matches:
  matches.sort(key=get_rating)
  return matches[(-1)][0](**options)
 raise ClassNotFound(('no lexer for filename %r found' % _fn))

def get_lexer_for_filename ( _fn , code = None , ** options ) : matches = [ ] fn = basename ( _fn ) for ( modname , name , _ , filenames , _ ) in LEXERS . itervalues ( ) : for filename in filenames : if fnmatch . fnmatch ( fn , filename ) : if ( name not in _lexer_cache ) : _load_lexers ( modname ) matches . append ( ( _lexer_cache [ name ] , filename ) ) for cls in find_plugin_lexers ( ) : for filename in cls . filenames : if fnmatch . fnmatch ( fn , filename ) : matches . append ( ( cls , filename ) ) if ( ( sys . version_info > ( 3 , ) ) and isinstance ( code , bytes ) ) : code = code . decode ( 'latin1' ) def get_rating ( info ) : ( cls , filename ) = info bonus = ( ( ( '*' not in filename ) and 0.5 ) or 0 ) if code : return ( cls . analyse_text ( code ) + bonus ) return ( cls . priority + bonus ) if matches : matches . sort ( key = get_rating ) return matches [ ( - 1 ) ] [ 0 ] ( ** options ) raise ClassNotFound ( ( 'no lexer for filename %r found' % _fn ) )
get a lexer for a filename .