]> git.phdru.name Git - dotfiles.git/blob - bin/iconvx.py
Feat(recode-filenames-recursive): Allow to omit parameters
[dotfiles.git] / bin / iconvx.py
1 #! /usr/bin/env python3
2 "iconv wrapper"
3
4 from getopt import getopt
5 import os, shutil, sys, tempfile
6
7 options, arguments = getopt(sys.argv[1:], 'f:t:')
8
9 from_charset = to_charset = None
10
11 for option, value in options:
12     if option == '-f':
13         from_charset = value
14     elif option == '-t':
15         to_charset = value
16
17
18 if from_charset is None:
19     raise ValueError("you must use -f param to name source charset")
20
21 if to_charset is None:
22     raise ValueError("you must use -t param to name destination charset")
23
24
25 tempfname = "_iconvx" + tempfile.gettempprefix() + "tmp"
26
27 if arguments:
28     try:
29         for file in arguments:
30             os.system(
31                 "iconv.py -f '%s' -t '%s' '%s' > '%s'" % (
32                     from_charset, to_charset, file, tempfname))
33             shutil.copy2(tempfname, file)
34     finally:
35         os.unlink(tempfname)
36
37 else:  # filter stdin => stdout
38     os.system("iconv.py -f '%s' -t '%s'" % (from_charset, to_charset))