<s> """<STR_LIT>""" <EOL> import re <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> _cache = { } <EOL> _MAXCACHE = <NUM_LIT:100> <EOL> def _purge ( ) : <EOL> """<STR_LIT>""" <EOL> _cache . clear ( ) <EOL> def fnmatch ( name , pat ) : <EOL> """<STR_LIT>""" <EOL> import os <EOL> name = os . path . normcase ( name ) <EOL> pat = os . path . normcase ( pat ) <EOL> return fnmatchcase ( name , pat ) <EOL> def filter ( names , pat ) : <EOL> """<STR_LIT>""" <EOL> import os , posixpath <EOL> result = [ ] <EOL> pat = os . path . normcase ( pat ) <EOL> try : <EOL> re_pat = _cache [ pat ] <EOL> except KeyError : <EOL> res = translate ( pat ) <EOL> if len ( _cache ) >= _MAXCACHE : <EOL> _cache . clear ( ) <EOL> _cache [ pat ] = re_pat = re . compile ( res ) <EOL> match = re_pat . match <EOL> if os . path is posixpath : <EOL> for name in names : <EOL> if match ( name ) : <EOL> result . append ( name ) <EOL> else : <EOL> for name in names : <EOL> if match ( os . path . normcase ( name ) ) : <EOL> result . append ( name ) <EOL> return result <EOL> def fnmatchcase ( name , pat ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> re_pat = _cache [ pat ] <EOL> except KeyError : <EOL> res = translate ( pat ) <EOL> if len ( _cache ) >= _MAXCACHE : <EOL> _cache . clear ( ) <EOL> _cache [ pat ] = re_pat = re . compile ( res ) <EOL> return re_pat . match ( name ) is not None <EOL> def translate ( pat ) : <EOL> """<STR_LIT>""" <EOL> i , n = <NUM_LIT:0> , len ( pat ) <EOL> res = '<STR_LIT>' <EOL> while i < n : <EOL> c = pat [ i ] <EOL> i = i + <NUM_LIT:1> <EOL> if c == '<STR_LIT:*>' : <EOL> res = res + '<STR_LIT>' <EOL> elif c == '<STR_LIT:?>' : <EOL> res = res + '<STR_LIT:.>' <EOL> elif c == '<STR_LIT:[>' : <EOL> j = i <EOL> if j < n and pat [ j ] == '<STR_LIT:!>' : <EOL> j = j + <NUM_LIT:1> <EOL> if j < n and pat [ j ] == '<STR_LIT:]>' : <EOL> j = j + <NUM_LIT:1> <EOL> while j < n and pat [ j ] != '<STR_LIT:]>' : <EOL> j = j + <NUM_LIT:1> <EOL> if j >= n : <EOL> res = res + '<STR_LIT>' <EOL> else : <EOL> stuff = pat [ i : j ] . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) <EOL> i = j + <NUM_LIT:1> <EOL> if stuff [ <NUM_LIT:0> ] == '<STR_LIT:!>' : <EOL> stuff = '<STR_LIT>' + stuff [ <NUM_LIT:1> : ] <EOL> elif stuff [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> stuff = '<STR_LIT:\\>' + stuff <EOL> res = '<STR_LIT>' % ( res , stuff ) <EOL> else : <EOL> res = res + re . escape ( c ) <EOL> return res + '<STR_LIT>' </s>
