<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> import re <EOL> from webassets import six <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 fnmatch_filter ( names , pat ) : <EOL> """<STR_LIT>""" <EOL> import os , posixpath <EOL> result = [ ] <EOL> pat = os . path . normcase ( pat ) <EOL> if not pat in _cache : <EOL> res = translate ( pat ) <EOL> if len ( _cache ) >= _MAXCACHE : <EOL> _cache . clear ( ) <EOL> _cache [ pat ] = re . compile ( res ) <EOL> match = _cache [ pat ] . match <EOL> if os . path is posixpath : <EOL> for name in names : <EOL> m = match ( name ) <EOL> if m : <EOL> result . append ( ( name , m . groups ( ) ) ) <EOL> else : <EOL> for name in names : <EOL> m = match ( os . path . normcase ( name ) ) <EOL> if m : <EOL> result . append ( ( name , m . groups ( ) ) ) <EOL> return result <EOL> def fnmatchcase ( name , pat ) : <EOL> """<STR_LIT>""" <EOL> if not pat in _cache : <EOL> res = translate ( pat ) <EOL> if len ( _cache ) >= _MAXCACHE : <EOL> _cache . clear ( ) <EOL> _cache [ pat ] = re . compile ( res ) <EOL> return _cache [ 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>' <EOL> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import re <EOL> class Globber ( object ) : <EOL> listdir = staticmethod ( os . listdir ) <EOL> isdir = staticmethod ( os . path . isdir ) <EOL> islink = staticmethod ( os . path . islink ) <EOL> exists = staticmethod ( os . path . lexists ) <EOL> def walk ( self , top , followlinks = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> names = self . listdir ( top ) <EOL> except os . error : <EOL> return <EOL> items = [ ] <EOL> for name in names : <EOL> items . append ( name ) <EOL> yield top , items <EOL> for name in items : <EOL> new_path = os . path . join ( top , name ) <EOL> if followlinks or not self . islink ( new_path ) : <EOL> for x in self . walk ( new_path , followlinks ) : <EOL> yield x <EOL> def glob ( self , pathname , with_matches = False ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . iglob ( pathname , with_matches ) ) <EOL> def iglob ( self , pathname , with_matches = False ) : <EOL> """<STR_LIT>""" <EOL> result = self . _iglob ( pathname ) <EOL> if with_matches : <EOL> return result <EOL> return map ( lambda s : s [ <NUM_LIT:0> ] , result ) <EOL> def _iglob ( self , pathname , rootcall = True ) : <EOL> """<STR_LIT>""" <EOL> if not has_magic ( pathname ) : <EOL> if self . exists ( pathname ) : <EOL> yield pathname , ( ) <EOL> return <EOL> dirname , basename = os . path . split ( pathname ) <EOL> if dirname and has_magic ( dirname ) : <EOL> dirs = self . _iglob ( dirname , rootcall = False ) <EOL> else : <EOL> dirs = [ ( dirname , ( ) ) ] <EOL> for dirname , dir_groups in dirs : <EOL> for name , groups in self . resolve_pattern ( <EOL> dirname , basename , not rootcall ) : <EOL> yield os . path . join ( dirname , name ) , dir_groups + groups <EOL> def resolve_pattern ( self , dirname , pattern , globstar_with_root ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( pattern , six . text_type ) and not isinstance ( dirname , six . text_type ) : <EOL> dirname = six . u ( dirname , sys . getfilesystemencoding ( ) or <EOL> sys . getdefaultencoding ( ) ) <EOL> if not has_magic ( pattern ) : <EOL> if pattern == '<STR_LIT>' : <EOL> if self . isdir ( dirname ) : <EOL> return [ ( pattern , ( ) ) ] <EOL> else : <EOL> if self . exists ( os . path . join ( dirname , pattern ) ) : <EOL> return [ ( pattern , ( ) ) ] <EOL> return [ ] <EOL> if not dirname : <EOL> dirname = os . curdir <EOL> try : <EOL> if pattern == '<STR_LIT>' : <EOL> names = [ '<STR_LIT>' ] if globstar_with_root else [ ] <EOL> for top , entries in self . walk ( dirname ) : <EOL> _mkabs = lambda s : os . path . join ( top [ len ( dirname ) + <NUM_LIT:1> : ] , s ) <EOL> names . extend ( map ( _mkabs , entries ) ) <EOL> pattern = '<STR_LIT:*>' <EOL> else : <EOL> names = self . listdir ( dirname ) <EOL> except os . error : <EOL> return [ ] <EOL> if pattern [ <NUM_LIT:0> ] != '<STR_LIT:.>' : <EOL> names = filter ( lambda x : not x or x [ <NUM_LIT:0> ] != '<STR_LIT:.>' , names ) <EOL> return fnmatch_filter ( names , pattern ) <EOL> default_globber = Globber ( ) <EOL> glob = default_globber . glob <EOL> iglob = default_globber . iglob <EOL> del default_globber <EOL> magic_check = re . compile ( '<STR_LIT>' ) <EOL> def has_magic ( s ) : <EOL> return magic_check . search ( s ) is not None </s>
