41 lines
992 B
Python
Executable File
41 lines
992 B
Python
Executable File
#!/usr/bin/python3
|
|
"""
|
|
This is a very-very simple filename sanitizer program written in python
|
|
"""
|
|
|
|
import sys # argv
|
|
import os # rename
|
|
import unicodedata as unid # normalize
|
|
sys.tracebacklimit = 0
|
|
|
|
def test_special_char(my_char):
|
|
"""
|
|
Test if a character is special in terms of filename
|
|
"""
|
|
char_num = ord(my_char)
|
|
if char_num > 127: # case ß
|
|
return False
|
|
|
|
return (
|
|
my_char.isalnum() or
|
|
char_num == 95 or
|
|
char_num == 47 or
|
|
char_num == 46 or
|
|
char_num == 45
|
|
) and (char_num < 127)
|
|
|
|
|
|
def sanitize(filename):
|
|
"""
|
|
A simple filename sanitizer
|
|
"""
|
|
spaceless_unicode = ["_" if i == " " else i for i in filename]
|
|
spaceless_unicode = unid.normalize("NFKD", "".join(spaceless_unicode))
|
|
ret = [i if test_special_char(i) else "" for i in spaceless_unicode]
|
|
return "".join(ret)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
for row in sys.stdin:
|
|
print(sanitize(row))
|