From: Oleg Broytman Date: Sat, 9 Nov 2019 23:01:39 +0000 (+0300) Subject: Adapt Python scripts to Python 3 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=f0344023aa21ede3b47d77559af54b97c37f328b;p=dotfiles.git Adapt Python scripts to Python 3 Fix some `flake8` errors. --- diff --git a/bin/abspath.py b/bin/abspath.py index fe57b54..9dbfebe 100755 --- a/bin/abspath.py +++ b/bin/abspath.py @@ -1,4 +1,4 @@ #! /usr/bin/env python import sys, os -print os.path.abspath(sys.argv[1]) +print(os.path.abspath(sys.argv[1])) diff --git a/bin/decode-URLs.py b/bin/decode-URLs.py index 70400ab..4d58193 100755 --- a/bin/decode-URLs.py +++ b/bin/decode-URLs.py @@ -9,4 +9,4 @@ while True: unqoted = urllib.unquote(url.strip()) if unqoted.startswith('file://'): unqoted = unqoted[len('file://'):] - print unqoted + print(unqoted) diff --git a/bin/get_html_encoding.py b/bin/get_html_encoding.py index 8fd9656..4ffb449 100755 --- a/bin/get_html_encoding.py +++ b/bin/get_html_encoding.py @@ -62,12 +62,12 @@ if __name__ == '__main__': import sys parser = parse_html(sys.argv[1]) if hasattr(parser, "charset"): - print parser.charset + print(parser.charset) else: import chardet charset = chardet.detect(open(sys.argv[1]).read())["encoding"] if charset in ("ISO-8859-2", "MacCyrillic"): charset = "cp1251" - print charset + print(charset) except: pass diff --git a/bin/get_xml_encoding.py b/bin/get_xml_encoding.py index 5007971..ec79c1a 100755 --- a/bin/get_xml_encoding.py +++ b/bin/get_xml_encoding.py @@ -15,4 +15,4 @@ if __name__ == '__main__': parser.ParseFile(open(sys.argv[1], 'r')) except: pass - if charset: print charset + if charset: print(charset) diff --git a/bin/iconv.py b/bin/iconv.py index 41631fe..83de413 100755 --- a/bin/iconv.py +++ b/bin/iconv.py @@ -20,9 +20,9 @@ if arguments: infile = open(file) try: for line in infile: - sys.stdout.write(unicode(line, from_charset, "replace").encode(to_charset, "replace")) + sys.stdout.write(line.decode(from_charset, "replace").encode(to_charset, "replace")) except: infile.close() else: for line in sys.stdin: - sys.stdout.write(unicode(line, from_charset, "replace").encode(to_charset, "replace")) + sys.stdout.write(line.decode(from_charset, "replace").encode(to_charset, "replace")) diff --git a/bin/iconvx.py b/bin/iconvx.py index 9352e73..d3c20d1 100755 --- a/bin/iconvx.py +++ b/bin/iconvx.py @@ -16,10 +16,10 @@ for option, value in options: if from_charset is None: - raise ValueError, "you must use -f param to name source charset" + 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" + raise ValueError("you must use -t param to name destination charset") import tempfile, os, shutil diff --git a/bin/iconvxml.py b/bin/iconvxml.py index 86eb66d..2892a91 100755 --- a/bin/iconvxml.py +++ b/bin/iconvxml.py @@ -20,9 +20,9 @@ if arguments: infile = open(file) try: for line in infile: - sys.stdout.write(unicode(line, from_charset, "replace").encode(to_charset, "xmlcharrefreplace")) + sys.stdout.write(line.decode(from_charset, "replace").encode(to_charset, "xmlcharrefreplace")) except: infile.close() else: for line in sys.stdin: - sys.stdout.write(unicode(line, from_charset, "replace").encode(to_charset, "xmlcharrefreplace")) + sys.stdout.write(line.decode(from_charset, "replace").encode(to_charset, "xmlcharrefreplace")) diff --git a/bin/idna.py b/bin/idna.py index a0dabc6..fd2c8e5 100755 --- a/bin/idna.py +++ b/bin/idna.py @@ -10,12 +10,11 @@ elif (l == 3) and (sys.argv[1] == '-r'): reverse = True address = sys.argv[2] else: - print >>sys.stderr, 'Usage: %s [-r] name.domain' % sys.argv[0] - sys.exit(1) + sys.exit('Usage: %s [-r] name.domain' % sys.argv[0]) from m_lib.defenc import default_encoding if reverse: - print address.decode("idna").encode(default_encoding) + print(address.decode("idna").encode(default_encoding)) else: - print unicode(address, default_encoding).encode("idna") + print(address.decode(default_encoding).encode("idna")) diff --git a/bin/latin1_to_ascii.py b/bin/latin1_to_ascii.py index 974edd9..a2f2145 100755 --- a/bin/latin1_to_ascii.py +++ b/bin/latin1_to_ascii.py @@ -103,8 +103,8 @@ xlate = { } def latin1_to_ascii(uinput): - if not isinstance(uinput, unicode): - uinput = unicode(uinput, sys.getfilesystemencoding()) + if isinstance(uinput, bytes): + uinput = uinput.decode(sys.getfilesystemencoding()) out = [] for c in uinput: i = ord(c) diff --git a/bin/mc_type.py b/bin/mc_type.py index a9d6322..70dcc2b 100755 --- a/bin/mc_type.py +++ b/bin/mc_type.py @@ -23,10 +23,10 @@ else: if '.' in mc_version: major, minor = [int(v) for v in mc_version.split('.')[:2]] if major < 4: - print 'old' + print('old') elif (major == 4) and (minor < 6): - print 'old' + print('old') else: - print 'new' + print('new') else: - print 'new' + print('new') diff --git a/bin/recode-filenames-recursive.py b/bin/recode-filenames-recursive.py index 9afd6c7..186d500 100755 --- a/bin/recode-filenames-recursive.py +++ b/bin/recode-filenames-recursive.py @@ -29,11 +29,11 @@ for dirname, names in plist: if os.path.exists(filename) and \ os.path.isfile(filename): newname = _recode(filename) - if newname <> filename: + if newname != filename: os.rename(filename, newname) os.chdir('..') dirname = os.path.basename(dirname) newname = _recode(dirname) - if newname <> dirname: + if newname != dirname: os.rename(dirname, newname) os.chdir(save_dir) diff --git a/bin/recode_filenames.py b/bin/recode_filenames.py index b9b4672..7e25a16 100755 --- a/bin/recode_filenames.py +++ b/bin/recode_filenames.py @@ -12,7 +12,7 @@ if src_encoding == "translit": elif dst_encoding == "cp1251": from m_lib.rus.lat2rus import lat2win as _recode else: - raise NotImplementedError, "destination encoding must be koi8-r or cp1251, not `%s'" % dst_encoding + raise NotImplementedError("destination encoding must be koi8-r or cp1251, not `%s'" % dst_encoding) elif dst_encoding == "translit": if src_encoding == "koi8-r": @@ -20,7 +20,7 @@ elif dst_encoding == "translit": elif src_encoding == "cp1251": from m_lib.rus.rus2lat import win2lat as _recode else: - raise NotImplementedError, "source encoding must be koi8-r or cp1251, not `%s'" % src_encoding + raise NotImplementedError("source encoding must be koi8-r or cp1251, not `%s'" % src_encoding) from m_lib.rus.rus2lat import koi2lat_d koi2lat_d["ΓΏ"] = '' # remove apostrophs - @@ -37,7 +37,7 @@ elif src_encoding == "url": import urllib def _recode(s): s = urllib.unquote(s) - if src_encoding <> dst_encoding: + if src_encoding != dst_encoding: s = recode(s, src_encoding, dst_encoding, "replace") return s @@ -49,7 +49,7 @@ elif dst_encoding == "url": from m_lib.opstring import recode import urllib def _recode(s): - if src_encoding <> dst_encoding: + if src_encoding != dst_encoding: s = recode(s, src_encoding, dst_encoding, "replace") return urllib.quote(s, safe=";/?:@&=+$,()'") # wget treats them as safe @@ -63,5 +63,5 @@ if __name__ == "__main__": import os for filename in sys.argv[3:]: new_name = _recode(filename) - if new_name <> filename: + if new_name != filename: os.rename(filename, new_name) diff --git a/bin/unzip.py b/bin/unzip.py index 42bf4db..8fd7426 100755 --- a/bin/unzip.py +++ b/bin/unzip.py @@ -26,10 +26,10 @@ out = '.' for zinfo in zf.infolist(): path = zinfo.filename - if not isinstance(path, unicode): + if isinstance(path, bytes): path = path.decode('cp866') recoded_path = path.encode(default_encoding) - print recoded_path + print(recoded_path) if path.startswith('./'): recoded_path = recoded_path[2:] diff --git a/bin/zip.py b/bin/zip.py index eebbaeb..e99c9a8 100755 --- a/bin/zip.py +++ b/bin/zip.py @@ -22,7 +22,7 @@ if len(arguments) < 2: def addToZip(zf, path): if os.path.isfile(path): - print path + print(path) recoded_path = path.decode(default_encoding).encode('cp866') zf.write(path, recoded_path, ZIP_DEFLATED) elif os.path.isdir(path):