]> git.phdru.name Git - dotfiles.git/blobdiff - bin/iconvx.py
.fvwmrc: Refactor many SetVolumeAndBrowser functions into one with parameters
[dotfiles.git] / bin / iconvx.py
index d3c20d10ecff2ee0e5ceaec72e2943cc8ee5b782..7ec3c96597fe40db6592b11b3c52efaa6746da81 100755 (executable)
@@ -1,37 +1,23 @@
-#! /usr/bin/env python
+#! /home/phd/.local/bin/python3
 "iconv wrapper"
 
 "iconv wrapper"
 
-import sys
-from getopt import getopt
+import os, shutil, tempfile
 
 
-options, arguments = getopt(sys.argv[1:], 'f:t:')
+from recode_filenames import parse_args
+from_encoding, to_encoding, files = parse_args(default='-')
 
 
-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")
-
-
-import tempfile, os, shutil
 tempfname = "_iconvx" + tempfile.gettempprefix() + "tmp"
 
 tempfname = "_iconvx" + tempfile.gettempprefix() + "tmp"
 
-if arguments:
-   try:
-         for file in arguments:
-            os.system("iconv.py -f '%s' -t '%s' '%s' > '%s'" % (from_charset, to_charset, file, tempfname))
+if files != ['-']:
+    try:
+        for file in files:
+            os.system(
+                "iconv.py -f '%s' -t '%s' '%s' > '%s'" % (
+                    from_encoding, to_encoding, file, tempfname))
             shutil.copy2(tempfname, file)
             shutil.copy2(tempfname, file)
-   finally:
-      os.unlink(tempfname)
+    finally:
+        os.unlink(tempfname)
 
 
-else: # filter stdin => stdout
-   os.system("iconv.py -f '%s' -t '%s'" % (from_charset, to_charset))
+else:  # filter stdin => stdout
+    os.system("iconv.py -f '%s' -t '%s'" % (from_encoding, to_encoding))