@@ -140,25 +140,33 @@ def resolve(path, posix=True):
140
140
return path
141
141
142
142
143
- legal_punctuation = r"!\#$%&\(\)\+,\-\.;\=@\[\]_\{\}\~"
144
- legal_spaces = r" "
145
- legal_chars = r'A-Za-z0-9' + legal_punctuation
143
+ legal_punctuation = r'!\#$%&\(\)\+,\-\.;\=@\[\]_\{\}\~'
144
+ legal_spaces = r' '
145
+ legal_alphanumeric = r'A-Za-z0-9'
146
+ legal_chars = legal_alphanumeric + legal_punctuation
146
147
legal_chars_inc_spaces = legal_chars + legal_spaces
147
148
illegal_chars_re = r'[^' + legal_chars + r']'
148
149
illegal_chars_exc_spaces_re = r'[^' + legal_chars_inc_spaces + r']'
149
150
replace_illegal_chars = re .compile (illegal_chars_re ).sub
150
151
replace_illegal_chars_exc_spaces = re .compile (illegal_chars_exc_spaces_re ).sub
151
152
152
153
153
- posix_legal_punctuation = r"!@#$%^&\*\(\)-_=\+\[\{\]\}\\\|;:'\",<.>\/\?`~"
154
- posix_legal_chars = r"A-Za-z0-9" + posix_legal_punctuation
154
+ posix_legal_punctuation = r'<:"/>\|\*\^\\\'`\?' + legal_punctuation
155
+ posix_legal_chars = legal_alphanumeric + posix_legal_punctuation
155
156
posix_legal_chars_inc_spaces = posix_legal_chars + legal_spaces
156
- posix_illegal_chars_re = r"[^" + posix_legal_chars + r"]"
157
- posix_illegal_chars_exc_spaces_re = r"[^" + posix_legal_chars_inc_spaces + r"]"
157
+ posix_illegal_chars_re = r'[^' + posix_legal_chars + r']'
158
+ posix_illegal_chars_exc_spaces_re = r'[^' + posix_legal_chars_inc_spaces + r']'
158
159
replace_illegal_posix_chars = re .compile (posix_illegal_chars_re ).sub
159
160
replace_illegal_posix_chars_exc_spaces = re .compile (posix_illegal_chars_exc_spaces_re ).sub
160
161
161
162
163
+ ILLEGAL_WINDOWS_NAMES = set ([
164
+ 'com1' , 'com2' , 'com3' , 'com4' , 'com5' , 'com6' , 'com7' , 'com8' , 'com9' ,
165
+ 'lpt1' , 'lpt2' , 'lpt3' , 'lpt4' , 'lpt5' , 'lpt6' , 'lpt7' , 'lpt8' , 'lpt9' ,
166
+ 'aux' , 'con' , 'nul' , 'prn'
167
+ ])
168
+
169
+
162
170
def portable_filename (filename , preserve_spaces = False , posix_only = False ):
163
171
"""
164
172
Return a new name for `filename` that is portable across operating systems.
@@ -197,16 +205,8 @@ def portable_filename(filename, preserve_spaces=False, posix_only=False):
197
205
filename = replace_illegal_chars ('_' , filename )
198
206
199
207
if not posix_only :
200
- # these are illegal both upper and lowercase and with or without an extension
201
- # we insert an underscore after the base name.
202
- windows_illegal_names = set ([
203
- 'com1' , 'com2' , 'com3' , 'com4' , 'com5' , 'com6' , 'com7' , 'com8' , 'com9' ,
204
- 'lpt1' , 'lpt2' , 'lpt3' , 'lpt4' , 'lpt5' , 'lpt6' , 'lpt7' , 'lpt8' , 'lpt9' ,
205
- 'aux' , 'con' , 'nul' , 'prn'
206
- ])
207
-
208
208
basename , dot , extension = filename .partition ('.' )
209
- if basename .lower () in windows_illegal_names :
209
+ if basename .lower () in ILLEGAL_WINDOWS_NAMES :
210
210
filename = '' .join ([basename , '_' , dot , extension ])
211
211
212
212
# no name made only of dots.
0 commit comments