Mercurial > hgrepos > Python > apps > py-cutils
comparison cutils/dos2unix.py @ 72:ae2df602beb4
Make shasum.py and dos2unix sub-modules to the new "cutils" package
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sat, 26 Feb 2022 19:20:20 +0100 |
| parents | dos2unix.py@26a8d4e7c8ee |
| children | c3268f4e752f |
comparison
equal
deleted
inserted
replaced
| 71:29fb33aa639a | 72:ae2df602beb4 |
|---|---|
| 1 r""" | |
| 2 :Author: Franz Glasner | |
| 3 :Copyright: (c) 2020-2022 Franz Glasner. | |
| 4 All rights reserved. | |
| 5 :License: BSD 3-Clause "New" or "Revised" License. | |
| 6 See :ref:`LICENSE <license>` for details. | |
| 7 If you cannot find LICENSE see | |
| 8 <https://opensource.org/licenses/BSD-3-Clause> | |
| 9 :ID: @(#) $HGid$ | |
| 10 | |
| 11 """ | |
| 12 | |
| 13 from __future__ import print_function, absolute_import | |
| 14 | |
| 15 from . import (__version__, __revision__, __date__) | |
| 16 | |
| 17 import argparse | |
| 18 import io | |
| 19 import sys | |
| 20 | |
| 21 | |
| 22 def main(argv=None): | |
| 23 aparser = argparse.ArgumentParser( | |
| 24 description="Python implementation of dos2unix", | |
| 25 fromfile_prefix_chars='@') | |
| 26 aparser.add_argument( | |
| 27 "--version", "-V", action="version", | |
| 28 version="%s (rv:%s)" % (__version__, __revision__)) | |
| 29 aparser.add_argument( | |
| 30 "--keepdate", "-k", action="store_true", | |
| 31 help="Keep the date stamp of output file same as input file.") | |
| 32 aparser.add_argument( | |
| 33 "--oldfile", "-o", action="store_false", dest="newfile", default=False, | |
| 34 help="Old file mode. Convert the file and write output to it." | |
| 35 " The program defaults to run in this mode." | |
| 36 " Wildcard names may be used. ") | |
| 37 aparser.add_argument( | |
| 38 "--newfile", "-n", action="store_true", dest="newfile", default=False, | |
| 39 help="New file mode. Convert the infile and write output to outfile." | |
| 40 " File names must be given in pairs and wildcard names should" | |
| 41 " NOT be used or you WILL lose your files.") | |
| 42 aparser.add_argument( | |
| 43 "--quiet", "-q", action="store_true", | |
| 44 help="Quiet mode. Suppress all warning and messages.") | |
| 45 | |
| 46 aparser.add_argument( | |
| 47 "files", nargs="+", metavar="FILE") | |
| 48 | |
| 49 opts = aparser.parse_args(args=argv) | |
| 50 | |
| 51 if opts.keepdate: | |
| 52 raise NotImplementedError("--keepdate, -k") | |
| 53 | |
| 54 return dos2unix(opts) | |
| 55 | |
| 56 | |
| 57 def gen_opts(files=[], newfile=False, keepdate=False, quiet=True): | |
| 58 if keepdate: | |
| 59 raise NotImplementedError("--keepdate, -k") | |
| 60 | |
| 61 if newfile and (len(files) % 2): | |
| 62 raise ValueError("need pairs of files") | |
| 63 | |
| 64 opts = argparse.Namespace(files=files, | |
| 65 newfile=newfile, | |
| 66 keepdate=keepdate, | |
| 67 quiet=quiet) | |
| 68 return opts | |
| 69 | |
| 70 | |
| 71 def dos2unix(opts): | |
| 72 if opts.newfile: | |
| 73 return _convert_copy(opts) | |
| 74 else: | |
| 75 return _convert_inplace(opts) | |
| 76 | |
| 77 | |
| 78 def _convert_inplace(opts): | |
| 79 lines = [] | |
| 80 for filename in opts.files: | |
| 81 with io.open(filename, "rt", encoding="iso-8859-1") as source: | |
| 82 for line in source: | |
| 83 lines.append(line.encode("iso-8859-1")) | |
| 84 with open(filename, "wb") as dest: | |
| 85 for line in lines: | |
| 86 dest.write(line) | |
| 87 | |
| 88 | |
| 89 def _convert_copy(opts): | |
| 90 if len(opts.files) % 2: | |
| 91 print("ERROR: need pairs of files", file=sys.stderr) | |
| 92 return 64 # :manpage:`sysexits(3)` EX_USAGE | |
| 93 idx = 0 | |
| 94 while idx < len(opts.files): | |
| 95 with io.open(opts.files[idx], "rt", encoding="iso-8859-1") as source: | |
| 96 with open(opts.files[idx+1], "wb") as dest: | |
| 97 for line in source: | |
| 98 dest.write(line.encode("iso-8859-1")) | |
| 99 idx += 2 | |
| 100 | |
| 101 | |
| 102 if __name__ == "__main__": | |
| 103 sys.exit(main()) |
