2013-10-30 03:19:08 +04:00
|
|
|
#!/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('/'): '-',
|
2016-10-10 06:28:45 +03:00
|
|
|
ord('|'): '-',
|
2013-10-30 03:19:08 +04:00
|
|
|
})
|
|
|
|
|
|
|
|
if os == 'Windows':
|
|
|
|
# Windows (non-POSIX namespace)
|
|
|
|
text = text.translate({
|
|
|
|
# Reserved in Windows VFAT and NTFS
|
|
|
|
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:]
|
|
|
|
|
2017-11-17 19:08:31 +03:00
|
|
|
text = text[:80] # Trim to 82 Unicode characters long
|
2013-10-30 03:19:08 +04:00
|
|
|
return text
|