mirror of
https://gitlab.com/dstftw/youtube-dl.git
synced 2020-11-16 09:42:26 +00:00
[utils] Add sanitize_path
This commit is contained in:
parent
bdf6eee0ae
commit
a2aaf4dbc6
|
@ -38,6 +38,7 @@ from youtube_dl.utils import (
|
|||
parse_iso8601,
|
||||
read_batch_urls,
|
||||
sanitize_filename,
|
||||
sanitize_path,
|
||||
shell_quote,
|
||||
smuggle_url,
|
||||
str_to_int,
|
||||
|
@ -131,6 +132,26 @@ class TestUtil(unittest.TestCase):
|
|||
self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
|
||||
self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
|
||||
|
||||
def test_sanitize_path(self):
|
||||
if sys.platform != 'win32':
|
||||
return
|
||||
|
||||
self.assertEqual(sanitize_path('abc'), 'abc')
|
||||
self.assertEqual(sanitize_path('abc/def'), 'abc\\def')
|
||||
self.assertEqual(sanitize_path('abc\\def'), 'abc\\def')
|
||||
self.assertEqual(sanitize_path('abc|def'), 'abc#def')
|
||||
self.assertEqual(sanitize_path('<>:"|?*'), '#######')
|
||||
self.assertEqual(sanitize_path('C:/abc/def'), 'C:\\abc\\def')
|
||||
self.assertEqual(sanitize_path('C?:/abc/def'), 'C##\\abc\\def')
|
||||
|
||||
self.assertEqual(sanitize_path('\\\\?\\UNC\\ComputerName\\abc'), '\\\\?\\UNC\\ComputerName\\abc')
|
||||
self.assertEqual(sanitize_path('\\\\?\\UNC/ComputerName/abc'), '\\\\?\\UNC\\ComputerName\\abc')
|
||||
|
||||
self.assertEqual(sanitize_path('\\\\?\\C:\\abc'), '\\\\?\\C:\\abc')
|
||||
self.assertEqual(sanitize_path('\\\\?\\C:/abc'), '\\\\?\\C:\\abc')
|
||||
self.assertEqual(sanitize_path('\\\\?\\C:\\ab?c\\de:f'), '\\\\?\\C:\\ab#c\\de#f')
|
||||
self.assertEqual(sanitize_path('\\\\?\\C:\\abc'), '\\\\?\\C:\\abc')
|
||||
|
||||
def test_ordered_set(self):
|
||||
self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
|
||||
self.assertEqual(orderedSet([]), [])
|
||||
|
|
|
@ -311,6 +311,24 @@ def sanitize_filename(s, restricted=False, is_id=False):
|
|||
return result
|
||||
|
||||
|
||||
def sanitize_path(s):
|
||||
"""Sanitizes and normalizes path on Windows"""
|
||||
if sys.platform != 'win32':
|
||||
return s
|
||||
drive, _ = os.path.splitdrive(s)
|
||||
unc, _ = os.path.splitunc(s)
|
||||
unc_or_drive = unc or drive
|
||||
norm_path = os.path.normpath(remove_start(s, unc_or_drive)).split(os.path.sep)
|
||||
if unc_or_drive:
|
||||
norm_path.pop(0)
|
||||
sanitized_path = [
|
||||
re.sub('[/<>:"\\|\\\\?\\*]', '#', path_part)
|
||||
for path_part in norm_path]
|
||||
if unc_or_drive:
|
||||
sanitized_path.insert(0, unc_or_drive + os.path.sep)
|
||||
return os.path.join(*sanitized_path)
|
||||
|
||||
|
||||
def orderedSet(iterable):
|
||||
""" Remove all duplicates from the input iterable """
|
||||
res = []
|
||||
|
|
Loading…
Reference in a new issue