From c439d2ae45cdc7ada283b3cde8b71579c96c5158 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 25 Oct 2024 14:54:18 +0300 Subject: [PATCH] bin: Refactor and improve more re-encoding utilities --- bin/iconv.py | 24 +++++++++--------------- bin/iconvx.py | 29 +++++++---------------------- bin/recode-inplace | 14 ++++++-------- 3 files changed, 22 insertions(+), 45 deletions(-) diff --git a/bin/iconv.py b/bin/iconv.py index 9cb164f..8c8d96f 100755 --- a/bin/iconv.py +++ b/bin/iconv.py @@ -2,31 +2,25 @@ "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") + ) diff --git a/bin/iconvx.py b/bin/iconvx.py index b4c990e..7e92973 100755 --- a/bin/iconvx.py +++ b/bin/iconvx.py @@ -1,38 +1,23 @@ #! /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)) diff --git a/bin/recode-inplace b/bin/recode-inplace index 77ff306..91b629a 100755 --- a/bin/recode-inplace +++ b/bin/recode-inplace @@ -1,14 +1,12 @@ #! /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" -- 2.39.5