From c3f5f6a320e9a922717238128a6a58bb509327c7 Mon Sep 17 00:00:00 2001 From: Mort Yao Date: Wed, 30 Oct 2013 00:19:08 +0100 Subject: [PATCH] add new module: util.fs --- src/you_get/common.py | 9 +++++---- src/you_get/util/fs.py | 45 ++++++++++++++++++++++++++++++++++++++++++ tests/test_util.py | 11 +++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 tests/test_util.py diff --git a/src/you_get/common.py b/src/you_get/common.py index 4ad45053..2c99976f 100644 --- a/src/you_get/common.py +++ b/src/you_get/common.py @@ -10,7 +10,7 @@ from urllib import request, parse import platform from .version import __version__ -from .util import log +from .util import log, legitimize dry_run = False force = False @@ -94,7 +94,7 @@ def parse_query_param(url, param): 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) -# DEPRECATED in favor of filenameable() +# DEPRECATED in favor of util.legitimize() def escape_file_path(path): path = path.replace('/', '-') path = path.replace('\\', '-') @@ -102,6 +102,7 @@ def escape_file_path(path): path = path.replace('?', '-') return path +# DEPRECATED in favor of util.legitimize() def filenameable(text): """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) pass - title = filenameable(title) + title = legitimize(title) filename = '%s.%s' % (title, ext) 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') - title = filenameable(title) + title = legitimize(title) filename = '%s.%s' % (title, 'ts') filepath = os.path.join(output_dir, filename) diff --git a/src/you_get/util/fs.py b/src/you_get/util/fs.py index e69de29b..09aa48a9 100644 --- a/src/you_get/util/fs.py +++ b/src/you_get/util/fs.py @@ -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 diff --git a/tests/test_util.py b/tests/test_util.py new file mode 100644 index 00000000..0b7b0231 --- /dev/null +++ b/tests/test_util.py @@ -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")