@@ -205,61 +205,60 @@ def get_format_template(
205
205
206
206
207
207
def path_file_sanitize (path_file : pathlib .Path , adapt : bool = False , uniquify : bool = False ) -> pathlib .Path :
208
- # Get each directory name separately (first value in tuple; second value is for the file suffix).
209
- to_sanitize : [[str , str ]] = path_split_parts_suffix (path_file )
210
- sanitized_path_file : pathlib .Path = pathlib .Path (to_sanitize .pop (0 )[0 ])
211
-
212
- for name , suffix in to_sanitize :
213
- # Sanitize names: We need first top make sure that none file / directory name has bad chars or is longer than 255 chars.
214
- try :
215
- # sanitize_filename can shorten the file name actually
216
- filename_sanitized : str = sanitize_filename (
217
- name + suffix , replacement_text = " " , validate_after_sanitize = True , platform = "auto"
218
- )
219
-
220
- # Check if the file extension was removed by shortening the filename length
221
- if not filename_sanitized .endswith (suffix ):
222
- # Add the original file extension
223
- file_suffix : str = FILENAME_SANITIZE_PLACEHOLDER + path_file .suffix
224
- filename_sanitized = filename_sanitized [: - len (file_suffix )] + file_suffix
225
- except ValidationError as e :
226
- if adapt :
227
- # TODO: Implement proper exception handling and logging.
228
- # Hacky stuff, since the sanitizing function does not shorten the filename (filename too long)
229
- if str (e ).startswith ("[PV1101]" ):
230
- byte_ct : int = len (name .encode (sys .getfilesystemencoding ())) - FILENAME_LENGTH_MAX
231
- filename_sanitized = (
232
- name [: - byte_ct - len (FILENAME_SANITIZE_PLACEHOLDER ) - len (suffix )]
233
- + FILENAME_SANITIZE_PLACEHOLDER
234
- + suffix
235
- )
236
- else :
237
- raise
208
+ sanitized_filename : str = path_file .name
209
+ sanitized_path : pathlib .Path = path_file .parent
210
+ result : pathlib .Path
211
+
212
+ # Sanitize filename and make sure it does not exceed FILENAME_LENGTH_MAX
213
+ try :
214
+ # sanitize_filename can shorten the file name actually
215
+ sanitized_filename = sanitize_filename (
216
+ sanitized_filename , replacement_text = "_" , validate_after_sanitize = True , platform = "auto"
217
+ )
218
+
219
+ # Check if the file extension was removed by shortening the filename length
220
+ if not sanitized_filename .endswith (path_file .suffix ):
221
+ # Add the original file extension
222
+ file_suffix : str = FILENAME_SANITIZE_PLACEHOLDER + path_file .suffix
223
+ sanitized_filename = sanitized_filename [: - len (file_suffix )] + file_suffix
224
+ except ValidationError as e :
225
+ if adapt :
226
+ # TODO: Implement proper exception handling and logging.
227
+ # Hacky stuff, since the sanitizing function does not shorten the filename (filename too long)
228
+ if str (e ).startswith ("[PV1101]" ):
229
+ byte_ct : int = len (sanitized_filename .encode (sys .getfilesystemencoding ())) - FILENAME_LENGTH_MAX
230
+ sanitized_filename = (
231
+ sanitized_filename [: - byte_ct - len (FILENAME_SANITIZE_PLACEHOLDER ) - len (path_file .suffix )]
232
+ + FILENAME_SANITIZE_PLACEHOLDER
233
+ + path_file .suffix
234
+ )
238
235
else :
239
236
raise
240
- finally :
241
- sanitized_path_file = sanitized_path_file / filename_sanitized
237
+ else :
238
+ raise
242
239
243
- # Sanitize the whole path. The whole path with filename is not allowed to be longer then the max path length depending on the OS .
240
+ # Sanitize the path.
244
241
try :
245
- sanitized_path_file : pathlib .Path = sanitize_filepath (
246
- sanitized_path_file , replacement_text = " " , validate_after_sanitize = True , platform = "auto"
242
+ sanitized_path : pathlib .Path = sanitize_filepath (
243
+ sanitized_path , replacement_text = "_ " , validate_after_sanitize = True , platform = "auto"
247
244
)
248
245
except ValidationError as e :
249
246
# If adaption of path is allowed in case of an error set path to HOME.
250
247
if adapt :
251
248
if str (e ).startswith ("[PV1101]" ):
252
- sanitized_path_file = pathlib .Path .home () / sanitized_path_file . name
249
+ sanitized_path = pathlib .Path .home ()
253
250
else :
254
251
raise
255
252
else :
256
253
raise
257
254
255
+ result = sanitized_path / sanitized_filename
256
+
258
257
# Uniquify
259
258
if uniquify :
260
- sanitized_path_file = path_file_uniquify (sanitized_path_file )
259
+ result = path_file_uniquify (result )
261
260
262
- return sanitized_path_file
261
+ return result
263
262
264
263
265
264
def path_file_uniquify (path_file : pathlib .Path ) -> pathlib .Path :
@@ -340,17 +339,3 @@ def url_to_filename(url: str) -> str:
340
339
raise ValueError # reject '%2f' or 'dir%5Cbasename.ext' on Windows
341
340
342
341
return basename
343
-
344
-
345
- def path_split_parts_suffix (p : pathlib .Path ) -> [[str , str ]]:
346
- """Splits the path to file in parts and also splits the suffix from the file stem.
347
-
348
- :param p: Path to file which should be split in parts.
349
- :type p: pathlib.Path
350
- :return: List of tuples (Stem, Suffix) of each path part.
351
- """
352
- result : [[str , str ]] = [[part , "" ] for part in p .parts ]
353
- result [- 1 ][0 ] = p .stem
354
- result [- 1 ][- 1 ] = p .suffix
355
-
356
- return result
0 commit comments