mirror of
https://github.com/soimort/you-get.git
synced 2025-03-13 11:24:02 +03:00
add new module: util.fs
This commit is contained in:
parent
5c27e57d8c
commit
c3f5f6a320
@ -10,7 +10,7 @@ from urllib import request, parse
|
|||||||
import platform
|
import platform
|
||||||
|
|
||||||
from .version import __version__
|
from .version import __version__
|
||||||
from .util import log
|
from .util import log, legitimize
|
||||||
|
|
||||||
dry_run = False
|
dry_run = False
|
||||||
force = False
|
force = False
|
||||||
@ -94,7 +94,7 @@ def parse_query_param(url, param):
|
|||||||
def unicodize(text):
|
def unicodize(text):
|
||||||
return re.sub(r'\\u([0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])', lambda x: chr(int(x.group(0)[2:], 16)), text)
|
return re.sub(r'\\u([0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])', lambda x: chr(int(x.group(0)[2:], 16)), text)
|
||||||
|
|
||||||
# DEPRECATED in favor of filenameable()
|
# DEPRECATED in favor of util.legitimize()
|
||||||
def escape_file_path(path):
|
def escape_file_path(path):
|
||||||
path = path.replace('/', '-')
|
path = path.replace('/', '-')
|
||||||
path = path.replace('\\', '-')
|
path = path.replace('\\', '-')
|
||||||
@ -102,6 +102,7 @@ def escape_file_path(path):
|
|||||||
path = path.replace('?', '-')
|
path = path.replace('?', '-')
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
# DEPRECATED in favor of util.legitimize()
|
||||||
def filenameable(text):
|
def filenameable(text):
|
||||||
"""Converts a string to a legal filename through various OSes.
|
"""Converts a string to a legal filename through various OSes.
|
||||||
"""
|
"""
|
||||||
@ -509,7 +510,7 @@ def download_urls(urls, title, ext, total_size, output_dir = '.', refer = None,
|
|||||||
traceback.print_exc(file = sys.stdout)
|
traceback.print_exc(file = sys.stdout)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
title = filenameable(title)
|
title = legitimize(title)
|
||||||
|
|
||||||
filename = '%s.%s' % (title, ext)
|
filename = '%s.%s' % (title, ext)
|
||||||
filepath = os.path.join(output_dir, filename)
|
filepath = os.path.join(output_dir, filename)
|
||||||
@ -585,7 +586,7 @@ def download_urls_chunked(urls, title, ext, total_size, output_dir = '.', refer
|
|||||||
|
|
||||||
assert ext in ('ts')
|
assert ext in ('ts')
|
||||||
|
|
||||||
title = filenameable(title)
|
title = legitimize(title)
|
||||||
|
|
||||||
filename = '%s.%s' % (title, 'ts')
|
filename = '%s.%s' % (title, 'ts')
|
||||||
filepath = os.path.join(output_dir, filename)
|
filepath = os.path.join(output_dir, filename)
|
||||||
|
@ -0,0 +1,45 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import platform
|
||||||
|
|
||||||
|
def legitimize(text, os=platform.system()):
|
||||||
|
"""Converts a string to a valid filename.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# POSIX systems
|
||||||
|
text = text.translate({
|
||||||
|
0: None,
|
||||||
|
ord('/'): '-',
|
||||||
|
})
|
||||||
|
|
||||||
|
if os == 'Windows':
|
||||||
|
# Windows (non-POSIX namespace)
|
||||||
|
text = text[:255] # Trim to 255 Unicode characters long
|
||||||
|
text = text.translate({
|
||||||
|
# Reserved in Windows VFAT and NTFS
|
||||||
|
ord(':'): '-',
|
||||||
|
ord('*'): '-',
|
||||||
|
ord('?'): '-',
|
||||||
|
ord('\\'): '-',
|
||||||
|
ord('|'): '-',
|
||||||
|
ord('\"'): '\'',
|
||||||
|
# Reserved in Windows VFAT
|
||||||
|
ord('+'): '-',
|
||||||
|
ord('<'): '-',
|
||||||
|
ord('>'): '-',
|
||||||
|
ord('['): '(',
|
||||||
|
ord(']'): ')',
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
# *nix
|
||||||
|
if os == 'Darwin':
|
||||||
|
# Mac OS HFS+
|
||||||
|
text = text.translate({
|
||||||
|
ord(':'): '-',
|
||||||
|
})
|
||||||
|
|
||||||
|
# Remove leading .
|
||||||
|
if text.startswith("."):
|
||||||
|
text = text[1:]
|
||||||
|
|
||||||
|
return text
|
11
tests/test_util.py
Normal file
11
tests/test_util.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from you_get.util import *
|
||||||
|
|
||||||
|
class TestUtil(unittest.TestCase):
|
||||||
|
def test_legitimize(self):
|
||||||
|
self.assertEqual(legitimize("1*2", os="Linux"), "1*2")
|
||||||
|
self.assertEqual(legitimize("1*2", os="Darwin"), "1*2")
|
||||||
|
self.assertEqual(legitimize("1*2", os="Windows"), "1-2")
|
Loading…
x
Reference in New Issue
Block a user