-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencfix.py
executable file
·105 lines (83 loc) · 3.22 KB
/
encfix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
""" EncFix: Guess and fix character encodings of garbled filenames
Copyright (C) 2020 Arnie97 <arnie97@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import argparse
import chardet
import itertools
import os
import re
import unicodedata
import urllib.parse
CHARSETS = [
'latin-1',
'cp1251',
'cp1252',
'mac-roman',
'iso2022-jp',
'mbcs',
'euc-kr',
'shift-jis',
'big5',
'gbk',
'utf-8',
]
def recover_raw_data(s: str) -> bytes:
if re.search('%[0-9A-F]{2}', s, re.IGNORECASE):
return urllib.parse.unquote_to_bytes(s)
s = unicodedata.normalize('NFC', s)
for charset in CHARSETS:
try:
raw = s.encode(charset)
except (UnicodeEncodeError, LookupError):
pass
else:
return raw
def fix_encoding(s: str) -> str:
raw = recover_raw_data(s)
detected_charset = chardet.detect(raw)['encoding']
detected_charset = [detected_charset] if detected_charset else []
for charset in itertools.chain(detected_charset, reversed(CHARSETS)):
try:
fixed = raw.decode(charset)
assert s != fixed
except (UnicodeDecodeError, LookupError, AssertionError):
pass
else:
return fixed
return s
def main() -> None:
parser = argparse.ArgumentParser(description='Guess and fix character encodings of garbled filenames.')
parser.add_argument('-f', '--force', action='store_true', help='overwrite existing files; without this flag, the default behavior is to skip them')
parser.add_argument('-q', '--quiet', action='store_true', help='do not print what is being done')
parser.add_argument('-n', '--dry-run', action='store_true', help='do not rename the files, just show what would have been done')
parser.add_argument('-v', '--verbose', action='store_true', help='also print untouched files')
parser.add_argument('files', nargs='+', metavar='FILE', help="file with garbled name")
args = parser.parse_args()
for path in args.files:
directory, name = os.path.split(path)
new_name = fix_encoding(name)
if name == new_name:
if args.verbose and not args.quiet:
print(name, '?')
continue
new_path = os.path.join(directory, new_name)
if args.force or not os.path.exists(new_path):
if not args.quiet:
print(name, '->', new_name)
if not args.dry_run:
os.rename(path, new_path)
elif args.verbose and not args.quiet:
print(name, '!>', new_name)
if __name__ == '__main__':
main()