Browse Source

add new module: util.fs

Mort Yao 11 years ago
parent
commit
c3f5f6a320
3 changed files with 61 additions and 4 deletions
  1. 5 4
      src/you_get/common.py
  2. 45 0
      src/you_get/util/fs.py
  3. 11 0
      tests/test_util.py

+ 5 - 4
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)

+ 45 - 0
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

+ 11 - 0
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")