X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;ds=inline;f=bin%2Ficonv.py;h=83de413dc373ac5093e3cedfd971870676dbe6d9;hb=HEAD;hp=41631fea5bd449cd6128f4a33adbe5835c8bb8ac;hpb=c5883d2a782366c0a3468a989e756cf37dabbd46;p=dotfiles.git diff --git a/bin/iconv.py b/bin/iconv.py index 41631fe..6f594c6 100755 --- a/bin/iconv.py +++ b/bin/iconv.py @@ -1,28 +1,26 @@ -#! /usr/bin/env python +#! /home/phd/.local/bin/python3 "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 -if arguments: - for file in arguments: - infile = open(file) - try: - for line in infile: - sys.stdout.write(unicode(line, from_charset, "replace").encode(to_charset, "replace")) - except: - infile.close() +output = getattr(sys.stdout, 'buffer', sys.stdout) + + +if files != ['-']: + for file in files: + with open(file, 'rb') as infile: + for line in infile: + output.write( + line.decode(from_encoding, "replace"). + encode(to_encoding, "replace")) else: - for line in sys.stdin: - sys.stdout.write(unicode(line, from_charset, "replace").encode(to_charset, "replace")) + input = getattr(sys.stdin, 'buffer', sys.stdin) + for line in input: + output.write( + line.decode(from_encoding, "replace"). + encode(to_encoding, "replace") + )