diff dos2unix.py @ 36:1de48e84a5fb

Implemented a dos2unix command. BUGS: Option --keepdate not yet supported
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 08 Dec 2020 09:29:39 +0100
parents
children d856432a1cbb
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dos2unix.py	Tue Dec 08 09:29:39 2020 +0100
@@ -0,0 +1,93 @@
+r"""
+:Author:    Franz Glasner
+:Copyright: (c) 2020 Franz Glasner.
+            All rights reserved.
+:License:   BSD 3-Clause "New" or "Revised" License.
+            See :ref:`LICENSE <license>` for details.
+            If you cannot find LICENSE see
+            <https://opensource.org/licenses/BSD-3-Clause>
+:ID:        @(#) $HGid$
+
+"""
+
+from __future__ import print_function
+
+
+try:
+    from _cutils import __version__
+except ImportError:
+    __version__ = "unknown"
+
+__revision__ = "|VCSRevision|"
+__date__ = "|VCSJustDate|"
+
+
+import argparse
+import io
+import sys
+
+
+def main(argv=None):
+    aparser = argparse.ArgumentParser(
+        description="Python implementation of dos2unix",
+        fromfile_prefix_chars='@')
+    aparser.add_argument(
+        "--version", "-V", action="version",
+        version="%s (rv:%s)" % (__version__, __revision__))
+    aparser.add_argument(
+        "--keepdate", "-k", action="store_true",
+        help="Keep the date stamp of output file same as input file.")
+    aparser.add_argument(
+        "--oldfile", "-o", action="store_false", dest="newfile", default=False,
+        help="Old file mode. Convert the file and write output to it."
+             " The program defaults to run in this mode."
+             " Wildcard names may be used. ")
+    aparser.add_argument(
+        "--newfile", "-n", action="store_true", dest="newfile", default=False,
+        help="New file mode. Convert the infile and write output to outfile."
+             " File names must be given in pairs and wildcard names should"
+             " NOT be used or you WILL lose your files.")
+    aparser.add_argument(
+        "--quiet", "-q", action="store_true",
+        help="Quiet mode. Suppress all warning and messages.")
+
+    aparser.add_argument(
+        "files", nargs="+", metavar="FILE")
+
+    opts = aparser.parse_args(args=argv)
+
+    if opts.keepdate:
+        raise NotImplementedError("--keepdate, -k")
+
+    if opts.newfile:
+        return(_convert_copy(opts))
+    else:
+        return(_convert_inplace(opts))
+
+
+def _convert_inplace(opts):
+    lines = []
+    for filename in opts.files:
+        with io.open(filename, "rt", encoding="iso-8859-1") as source:
+            for line in source:
+                lines.append(line.encode("iso-8859-1"))
+        with open(filename, "wb") as dest:
+            for line in lines:
+                dest.write(line)
+
+
+def _convert_copy(opts):
+    if len(opts.files) % 2:
+        print("ERROR: need pairs of files", file=sys.stderr)
+        return 64  # :manpage:`sysexits(3)` EX_USAGE
+    idx = 0
+    while idx < len(opts.files):
+        with io.open(opts.files[idx], "rt", encoding="iso-8859-1") as source:
+            with open(opts.files[idx+1], "wb") as dest:
+                for line in source:
+                    dest.write(line.encode("iso-8859-1"))
+        idx += 2
+
+
+if __name__ == "__main__":
+    sys.exit(main())