def guess_lexer_for_filename(_fn, _text, **options):
 fn = basename(_fn)
 primary = None
 matching_lexers = set()
 for lexer in _iter_lexerclasses():
  for filename in lexer.filenames:
   if fnmatch.fnmatch(fn, filename):
    matching_lexers.add(lexer)
    primary = lexer
  for filename in lexer.alias_filenames:
   if fnmatch.fnmatch(fn, filename):
    matching_lexers.add(lexer)
 if (not matching_lexers):
  raise ClassNotFound(('no lexer for filename %r found' % fn))
 if (len(matching_lexers) == 1):
  return matching_lexers.pop()(**options)
 result = []
 for lexer in matching_lexers:
  rv = lexer.analyse_text(_text)
  if (rv == 1.0):
   return lexer(**options)
  result.append((rv, lexer))
 result.sort()
 if ((not result[(-1)][0]) and (primary is not None)):
  return primary(**options)
 return result[(-1)][1](**options)

def guess_lexer_for_filename ( _fn , _text , ** options ) : fn = basename ( _fn ) primary = None matching_lexers = set ( ) for lexer in _iter_lexerclasses ( ) : for filename in lexer . filenames : if fnmatch . fnmatch ( fn , filename ) : matching_lexers . add ( lexer ) primary = lexer for filename in lexer . alias_filenames : if fnmatch . fnmatch ( fn , filename ) : matching_lexers . add ( lexer ) if ( not matching_lexers ) : raise ClassNotFound ( ( 'no lexer for filename %r found' % fn ) ) if ( len ( matching_lexers ) == 1 ) : return matching_lexers . pop ( ) ( ** options ) result = [ ] for lexer in matching_lexers : rv = lexer . analyse_text ( _text ) if ( rv == 1.0 ) : return lexer ( ** options ) result . append ( ( rv , lexer ) ) result . sort ( ) if ( ( not result [ ( - 1 ) ] [ 0 ] ) and ( primary is not None ) ) : return primary ( ** options ) return result [ ( - 1 ) ] [ 1 ] ( ** options )
lookup all lexers that handle those filenames primary or secondary .