]> git.phdru.name Git - dotfiles.git/blob - bin/iconvx.py
Initial import
[dotfiles.git] / bin / iconvx.py
1 #! /usr/bin/env python
2 "iconv wrapper"
3
4 import sys
5 from getopt import getopt
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 import tempfile, os, shutil
26 tempfname = "_iconvx" + tempfile.gettempprefix() + "tmp"
27
28 if arguments:
29    try:
30          for file in arguments:
31             os.system("iconv.py -f '%s' -t '%s' '%s' > '%s'" % (from_charset, to_charset, file, tempfname))
32             shutil.copy2(tempfname, file)
33    finally:
34       os.unlink(tempfname)
35
36 else: # filter stdin => stdout
37    os.system("iconv.py -f '%s' -t '%s'" % (from_charset, to_charset))