Created
November 13, 2018 13:43
-
-
Save ties/0ddf732ef9958e0d106c451295da38b4 to your computer and use it in GitHub Desktop.
Uglify (mix) line endings. Creates incompatible files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Replace line endings by 'mixed' line endings. | |
Behaviour is untested | |
""" | |
import argparse | |
import random | |
from typing import TextIO | |
import sys | |
def main(f_in: TextIO, f_out: TextIO, seed: int) -> None: | |
rnd = random.Random() | |
rnd.seed(seed) | |
# For each line, replace line ending by 'new' line ending | |
for line in f_in.readlines(): | |
# Get ending | |
ending = random.choice([ | |
'\n', # Unix | |
'\r\n', # Windows | |
'\r', # OS9 | |
]) | |
# lines end with \n | |
f_out.write(line[:-1]) | |
f_out.write(ending) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Uglify some line endings') | |
parser.add_argument('input', type=argparse.FileType('r'), | |
help='Input file') | |
parser.add_argument('out', type=str, nargs='?', default=None, | |
help='Output file') | |
parser.add_argument('--seed', type=int, default=random.randint(0, 2**32), | |
help='random seed') | |
args = parser.parse_args() | |
if args.out: | |
with open(args.out, 'w', newline='') as f_out: | |
main(args.input, f_out, args.seed) | |
else: | |
main(args.input, sys.stdout, args.seed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment