@@ -31,44 +31,46 @@ def translate(pat, *, recursive=False, include_hidden=False, seps=None):
3131 seps = (os .path .sep , os .path .altsep )
3232 else :
3333 seps = os .path .sep
34- escaped_seps = '' .join (map (re .escape , seps ))
35- any_sep = f' [{ escaped_seps } ]' if len (seps ) > 1 else escaped_seps
36- not_sep = f' [^{ escaped_seps } ]'
34+ escaped_seps = "" .join (map (re .escape , seps ))
35+ any_sep = f" [{ escaped_seps } ]" if len (seps ) > 1 else escaped_seps
36+ not_sep = f" [^{ escaped_seps } ]"
3737 if include_hidden :
38- one_last_segment = f' { not_sep } +'
39- one_segment = f' { one_last_segment } { any_sep } '
40- any_segments = f' (?:.+{ any_sep } )?'
41- any_last_segments = '.*'
38+ one_last_segment = f" { not_sep } +"
39+ one_segment = f" { one_last_segment } { any_sep } "
40+ any_segments = f" (?:.+{ any_sep } )?"
41+ any_last_segments = ".*"
4242 else :
43- one_last_segment = f' [^{ escaped_seps } .]{ not_sep } *'
44- one_segment = f' { one_last_segment } { any_sep } '
45- any_segments = f' (?:{ one_segment } )*'
46- any_last_segments = f' { any_segments } (?:{ one_last_segment } )?'
43+ one_last_segment = f" [^{ escaped_seps } .]{ not_sep } *"
44+ one_segment = f" { one_last_segment } { any_sep } "
45+ any_segments = f" (?:{ one_segment } )*"
46+ any_last_segments = f" { any_segments } (?:{ one_last_segment } )?"
4747
4848 results = []
4949 parts = re .split (any_sep , pat )
5050 last_part_idx = len (parts ) - 1
5151 for idx , part in enumerate (parts ):
52- if part == '*' :
52+ if part == "*" :
5353 results .append (one_segment if idx < last_part_idx else one_last_segment )
54- elif recursive and part == '**' :
54+ elif recursive and part == "**" :
5555 if idx < last_part_idx :
56- if parts [idx + 1 ] != '**' :
56+ if parts [idx + 1 ] != "**" :
5757 results .append (any_segments )
5858 else :
5959 results .append (any_last_segments )
6060 else :
6161 if part :
62- if not include_hidden and part [0 ] in '*?' :
63- results .append (r' (?!\.)' )
64- results .extend (_translate (part , f' { not_sep } *' , not_sep )[0 ])
62+ if not include_hidden and part [0 ] in "*?" :
63+ results .append (r" (?!\.)" )
64+ results .extend (_translate (part , f" { not_sep } *" , not_sep )[0 ])
6565 if idx < last_part_idx :
6666 results .append (any_sep )
67- res = '' .join (results )
68- return fr'(?s:{ res } )\Z'
67+ res = "" .join (results )
68+ return rf"(?s:{ res } )\Z"
69+
70+
71+ _re_setops_sub = re .compile (r"([&~|])" ).sub
6972
7073
71- _re_setops_sub = re .compile (r'([&~|])' ).sub
7274def _translate (pat , star , question_mark ):
7375 res = []
7476 add = res .append
@@ -77,69 +79,70 @@ def _translate(pat, star, question_mark):
7779 i , n = 0 , len (pat )
7880 while i < n :
7981 c = pat [i ]
80- i = i + 1
81- if c == '*' :
82+ i = i + 1
83+ if c == "*" :
8284 # store the position of the wildcard
8385 star_indices .append (len (res ))
8486 add (star )
8587 # compress consecutive `*` into one
86- while i < n and pat [i ] == '*' :
88+ while i < n and pat [i ] == "*" :
8789 i += 1
88- elif c == '?' :
90+ elif c == "?" :
8991 add (question_mark )
90- elif c == '[' :
92+ elif c == "[" :
9193 j = i
92- if j < n and pat [j ] == '!' :
93- j = j + 1
94- if j < n and pat [j ] == ']' :
95- j = j + 1
96- while j < n and pat [j ] != ']' :
97- j = j + 1
94+ if j < n and pat [j ] == "!" :
95+ j = j + 1
96+ if j < n and pat [j ] == "]" :
97+ j = j + 1
98+ while j < n and pat [j ] != "]" :
99+ j = j + 1
98100 if j >= n :
99- add (' \\ [' )
101+ add (" \\ [" )
100102 else :
101103 stuff = pat [i :j ]
102- if '-' not in stuff :
103- stuff = stuff .replace (' \\ ' , r'\\' )
104+ if "-" not in stuff :
105+ stuff = stuff .replace (" \\ " , r"\\" )
104106 else :
105107 chunks = []
106- k = i + 2 if pat [i ] == '!' else i + 1
108+ k = i + 2 if pat [i ] == "!" else i + 1
107109 while True :
108- k = pat .find ('-' , k , j )
110+ k = pat .find ("-" , k , j )
109111 if k < 0 :
110112 break
111113 chunks .append (pat [i :k ])
112- i = k + 1
113- k = k + 3
114+ i = k + 1
115+ k = k + 3
114116 chunk = pat [i :j ]
115117 if chunk :
116118 chunks .append (chunk )
117119 else :
118- chunks [- 1 ] += '-'
120+ chunks [- 1 ] += "-"
119121 # Remove empty ranges -- invalid in RE.
120- for k in range (len (chunks )- 1 , 0 , - 1 ):
121- if chunks [k - 1 ][- 1 ] > chunks [k ][0 ]:
122- chunks [k - 1 ] = chunks [k - 1 ][:- 1 ] + chunks [k ][1 :]
122+ for k in range (len (chunks ) - 1 , 0 , - 1 ):
123+ if chunks [k - 1 ][- 1 ] > chunks [k ][0 ]:
124+ chunks [k - 1 ] = chunks [k - 1 ][:- 1 ] + chunks [k ][1 :]
123125 del chunks [k ]
124126 # Escape backslashes and hyphens for set difference (--).
125127 # Hyphens that create ranges shouldn't be escaped.
126- stuff = '-' .join (s .replace ('\\ ' , r'\\' ).replace ('-' , r'\-' )
127- for s in chunks )
128- i = j + 1
128+ stuff = "-" .join (
129+ s .replace ("\\ " , r"\\" ).replace ("-" , r"\-" ) for s in chunks
130+ )
131+ i = j + 1
129132 if not stuff :
130133 # Empty range: never match.
131- add (' (?!)' )
132- elif stuff == '!' :
134+ add (" (?!)" )
135+ elif stuff == "!" :
133136 # Negated empty range: match any character.
134- add ('.' )
137+ add ("." )
135138 else :
136139 # Escape set operations (&&, ~~ and ||).
137- stuff = _re_setops_sub (r' \\\1' , stuff )
138- if stuff [0 ] == '!' :
139- stuff = '^' + stuff [1 :]
140- elif stuff [0 ] in ('^' , '[' ):
141- stuff = ' \\ ' + stuff
142- add (f' [{ stuff } ]' )
140+ stuff = _re_setops_sub (r" \\\1" , stuff )
141+ if stuff [0 ] == "!" :
142+ stuff = "^" + stuff [1 :]
143+ elif stuff [0 ] in ("^" , "[" ):
144+ stuff = " \\ " + stuff
145+ add (f" [{ stuff } ]" )
143146 else :
144147 add (re .escape (c ))
145148 assert i == n
0 commit comments