]> git.phdru.name Git - dotfiles.git/commitdiff
Adapt Python scripts to Python 3
authorOleg Broytman <phd@phdru.name>
Sat, 9 Nov 2019 23:01:39 +0000 (02:01 +0300)
committerOleg Broytman <phd@phdru.name>
Sat, 9 Nov 2019 23:01:39 +0000 (02:01 +0300)
Fix some `flake8` errors.

14 files changed:
bin/abspath.py
bin/decode-URLs.py
bin/get_html_encoding.py
bin/get_xml_encoding.py
bin/iconv.py
bin/iconvx.py
bin/iconvxml.py
bin/idna.py
bin/latin1_to_ascii.py
bin/mc_type.py
bin/recode-filenames-recursive.py
bin/recode_filenames.py
bin/unzip.py
bin/zip.py

index fe57b54bae0d6245b54c2f7921a8dd001283f110..9dbfebe2678c526c5694ed0901ad4a45295378b1 100755 (executable)
@@ -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]))
index 70400abb29df900a8c3adc3bbd8475340cf25493..4d581938b9139df977b8bc2b7e0cae0b7fb4ecf0 100755 (executable)
@@ -9,4 +9,4 @@ while True:
    unqoted = urllib.unquote(url.strip())
    if unqoted.startswith('file://'):
        unqoted = unqoted[len('file://'):]
-   print unqoted
+   print(unqoted)
index 8fd9656a2bc5087af7163135305b563661071da8..4ffb44998588cfc87549c99c295eb103d8701725 100755 (executable)
@@ -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
index 500797165eead9f1ffa90d96ef2a030d33d43c71..ec79c1a010d03cb827e5a3ac964d2e6a78b84b44 100755 (executable)
@@ -15,4 +15,4 @@ if __name__ == '__main__':
       parser.ParseFile(open(sys.argv[1], 'r'))
    except:
       pass
-   if charset: print charset
+   if charset: print(charset)
index 41631fea5bd449cd6128f4a33adbe5835c8bb8ac..83de413dc373ac5093e3cedfd971870676dbe6d9 100755 (executable)
@@ -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"))
index 9352e73bd5f2e6829f8a162028ba06a7a379de0c..d3c20d10ecff2ee0e5ceaec72e2943cc8ee5b782 100755 (executable)
@@ -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
index 86eb66dbad906fc388f6c85ccc92d5dd8a446b2c..2892a919d70195bcebb0f62ad55277f915f844a3 100755 (executable)
@@ -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"))
index a0dabc60dc26f9e81d47f304c8f04c4f63298c9b..fd2c8e5984d2783554863a93a646f5efe8079241 100755 (executable)
@@ -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"))
index 974edd9b01614f7bd6defb95d1bd1603c7ded0ce..a2f21455e252bb141d7d0d8d9980c8b1b2d70347 100755 (executable)
@@ -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)
index a9d6322e8c17dce4a86cc50ce858e1a8a2f374dd..70dcc2b6a56f062086fc4bd0471aeb2cd7361a53 100755 (executable)
@@ -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')
index 9afd6c7a361dd9cc24c13edf58a96b43aba8c2e3..186d5001370155bc46a9778e835ab368f1112cfe 100755 (executable)
@@ -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)
index b9b467266528b4e1922f927c3c248229d25ddfee..7e25a1637f77e6ea266116aec67ede5ae62069f4 100755 (executable)
@@ -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)
index 42bf4db32de6ab56f6e9a72ceabcc40704afc09f..8fd74266b4980068adb4028afd45d2dd8f38fbdd 100755 (executable)
@@ -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:]
index eebbaeb736a04c2421c9d47b5111cab424ce7948..e99c9a8283ed6829e824388994eee5a043a51652 100755 (executable)
@@ -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):