#NEWLINE# def translate( pat ) : #NEWLINE# #INDENT# 'Translate a shell PATTERN to a regular expression.\n\n    There is no way to quote meta-characters.\n    ' #NEWLINE# ( i , n ) = ( 0 , len ( pat ) ) #NEWLINE# res = '' #NEWLINE# while ( i < n ) : #NEWLINE# #INDENT# c = pat [ i ] #NEWLINE# i = ( i + 1 ) #NEWLINE# if ( c == '*' ) : #NEWLINE# #INDENT# res = ( res + '.*' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( c == '?' ) : #NEWLINE# #INDENT# res = ( res + '.' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( c == '[' ) : #NEWLINE# #INDENT# j = i #NEWLINE# if ( ( j < n ) and ( pat [ j ] == '!' ) ) : #NEWLINE# #INDENT# j = ( j + 1 ) #NEWLINE# #UNINDENT# if ( ( j < n ) and ( pat [ j ] == ']' ) ) : #NEWLINE# #INDENT# j = ( j + 1 ) #NEWLINE# #UNINDENT# while ( ( j < n ) and ( pat [ j ] != ']' ) ) : #NEWLINE# #INDENT# j = ( j + 1 ) #NEWLINE# #UNINDENT# if ( j >= n ) : #NEWLINE# #INDENT# res = ( res + '\\[' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# stuff = pat [ i : j ] . replace ( '\\' , '\\\\' ) #NEWLINE# i = ( j + 1 ) #NEWLINE# if ( stuff [ 0 ] == '!' ) : #NEWLINE# #INDENT# stuff = ( '^' + stuff [ 1 : ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( stuff [ 0 ] == '^' ) : #NEWLINE# #INDENT# stuff = ( '\\' + stuff ) #NEWLINE# #UNINDENT# #UNINDENT# res = ( '%s[%s]' % ( res , stuff ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# res = ( res + re . escape ( c ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return ( res + '$' )