you-get/src/you_get/util/fs.py
2013-10-30 00:19:08 +01:00

46 lines
1.0 KiB
Python

#!/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