"Recode to default charset"
import sys
-from getopt import getopt
-from m_lib.defenc import default_encoding
-from_charset = "cp1251"
-to_charset = default_encoding
+from recode_filenames import parse_args
+from_encoding, to_encoding, files = parse_args(default='-')
-options, arguments = getopt(sys.argv[1:], 'f:t:')
-for option, value in options:
- if option == '-f':
- from_charset = value
- elif option == '-t':
- to_charset = value
output = getattr(sys.stdout, 'buffer', sys.stdout)
-if arguments:
- for file in arguments:
+if files != ['-']:
+ for file in files:
with open(file, 'rb') as infile:
for line in infile:
output.write(
- line.decode(from_charset, "replace").
- encode(to_charset, "replace"))
+ line.decode(from_encoding, "replace").
+ encode(to_encoding, "replace"))
else:
input = getattr(sys.stdin, 'buffer', sys.stdin)
for line in input:
output.write(
- line.decode(from_charset, "replace").encode(to_charset, "replace"))
+ line.decode(from_encoding, "replace").
+ encode(to_encoding, "replace")
+ )
#! /usr/bin/env python3
"iconv wrapper"
-from getopt import getopt
-import os, shutil, sys, tempfile
+import os, shutil, tempfile
-options, arguments = getopt(sys.argv[1:], 'f:t:')
-
-from_charset = to_charset = None
-
-for option, value in options:
- if option == '-f':
- from_charset = value
- elif option == '-t':
- to_charset = value
-
-
-if from_charset is None:
- raise ValueError("you must use -f param to name source charset")
-
-if to_charset is None:
- raise ValueError("you must use -t param to name destination charset")
+from recode_filenames import parse_args
+from_encoding, to_encoding, files = parse_args(default='-')
tempfname = "_iconvx" + tempfile.gettempprefix() + "tmp"
-if arguments:
+if files != ['-']:
try:
- for file in arguments:
+ for file in files:
os.system(
"iconv.py -f '%s' -t '%s' '%s' > '%s'" % (
- from_charset, to_charset, file, tempfname))
+ from_encoding, to_encoding, file, tempfname))
shutil.copy2(tempfname, file)
finally:
os.unlink(tempfname)
else: # filter stdin => stdout
- os.system("iconv.py -f '%s' -t '%s'" % (from_charset, to_charset))
+ os.system("iconv.py -f '%s' -t '%s'" % (from_encoding, to_encoding))
#! /bin/sh
+set -e
-fromenc="$1"
-toenc="$2"
-shift 2
+tmpfile="`mktemp`"
+trap "exec rm -f $tmpfile" EXIT HUP INT QUIT PIPE TERM
-tmpfile="`mktemp`" &&
+. get_encodings
-for file in "$@"; do
- iconv -f "$fromenc" -t "$toenc" "$file" >"$tmpfile" &&
+for file in "$filenames"; do
+ iconv -f "$from_encoding" -t "$to_encoding" "$file" >"$tmpfile"
cp "$tmpfile" "$file"
done
-
-exec rm "$tmpfile"