| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,8 @@ | |||
| 10 | 10 | conflicts and keep the file nicely ordered. | |
| 11 | 11 | """ | |
| 12 | 12 | import argparse | |
| 13 | + from typing import Any | ||
| 14 | + from typing import Callable | ||
| 13 | 15 | from typing import IO | |
| 14 | 16 | from typing import Optional | |
| 15 | 17 | from typing import Sequence | |
@@ -18,9 +20,15 @@ | |||
| 18 | 20 | FAIL = 1 | |
| 19 | 21 | ||
| 20 | 22 | ||
| 21 | - def sort_file_contents(f: IO[bytes]) -> int: | ||
| 23 | + def sort_file_contents( | ||
| 24 | + f: IO[bytes], | ||
| 25 | + key : Optional[Callable[[bytes], Any]], | ||
| 26 | + ) -> int: | ||
| 22 | 27 | before = list(f) | |
| 23 | - after = sorted(line.strip(b'\n\r') for line in before if line.strip()) | ||
| 28 | + after = sorted( | ||
| 29 | + (line.strip(b'\n\r') for line in before if line.strip()), | ||
| 30 | + key=key, | ||
| 31 | + ) | ||
| 24 | 32 | ||
| 25 | 33 | before_string = b''.join(before) | |
| 26 | 34 | after_string = b'\n'.join(after) + b'\n' | |
@@ -37,13 +45,20 @@ def sort_file_contents(f: IO[bytes]) -> int: | |||
| 37 | 45 | def main(argv: Optional[Sequence[str]] = None) -> int: | |
| 38 | 46 | parser = argparse.ArgumentParser() | |
| 39 | 47 | parser.add_argument('filenames', nargs='+', help='Files to sort') | |
| 48 | + parser.add_argument( | ||
| 49 | + '--ignore-case', | ||
| 50 | + action='store_const', | ||
| 51 | + const=bytes.lower, | ||
| 52 | + default=None, | ||
| 53 | + help='fold lower case to upper case characters', | ||
| 54 | + ) | ||
| 40 | 55 | args = parser.parse_args(argv) | |
| 41 | 56 | ||
| 42 | 57 | retv = PASS | |
| 43 | 58 | ||
| 44 | 59 | for arg in args.filenames: | |
| 45 | 60 | with open(arg, 'rb+') as file_obj: | |
| 46 | - ret_for_file = sort_file_contents(file_obj) | ||
| 61 | + ret_for_file = sort_file_contents(file_obj, key=args.ignore_case) | ||
| 47 | 62 | ||
| 48 | 63 | if ret_for_file: | |
| 49 | 64 | print(f'Sorting {arg}') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,28 +6,52 @@ | |||
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | 8 | @pytest.mark.parametrize( | |
| 9 | - ('input_s', 'expected_retval', 'output'), | ||
| 9 | + ('input_s', 'argv', 'expected_retval', 'output'), | ||
| 10 | 10 | ( | |
| 11 | - (b'', FAIL, b'\n'), | ||
| 12 | - (b'lonesome\n', PASS, b'lonesome\n'), | ||
| 13 | - (b'missing_newline', FAIL, b'missing_newline\n'), | ||
| 14 | - (b'newline\nmissing', FAIL, b'missing\nnewline\n'), | ||
| 15 | - (b'missing\nnewline', FAIL, b'missing\nnewline\n'), | ||
| 16 | - (b'alpha\nbeta\n', PASS, b'alpha\nbeta\n'), | ||
| 17 | - (b'beta\nalpha\n', FAIL, b'alpha\nbeta\n'), | ||
| 18 | - (b'C\nc\n', PASS, b'C\nc\n'), | ||
| 19 | - (b'c\nC\n', FAIL, b'C\nc\n'), | ||
| 20 | - (b'mag ical \n tre vor\n', FAIL, b' tre vor\nmag ical \n'), | ||
| 21 | - (b'@\n-\n_\n#\n', FAIL, b'#\n-\n@\n_\n'), | ||
| 22 | - (b'extra\n\n\nwhitespace\n', FAIL, b'extra\nwhitespace\n'), | ||
| 23 | - (b'whitespace\n\n\nextra\n', FAIL, b'extra\nwhitespace\n'), | ||
| 11 | + (b'', [], FAIL, b'\n'), | ||
| 12 | + (b'lonesome\n', [], PASS, b'lonesome\n'), | ||
| 13 | + (b'missing_newline', [], FAIL, b'missing_newline\n'), | ||
| 14 | + (b'newline\nmissing', [], FAIL, b'missing\nnewline\n'), | ||
| 15 | + (b'missing\nnewline', [], FAIL, b'missing\nnewline\n'), | ||
| 16 | + (b'alpha\nbeta\n', [], PASS, b'alpha\nbeta\n'), | ||
| 17 | + (b'beta\nalpha\n', [], FAIL, b'alpha\nbeta\n'), | ||
| 18 | + (b'C\nc\n', [], PASS, b'C\nc\n'), | ||
| 19 | + (b'c\nC\n', [], FAIL, b'C\nc\n'), | ||
| 20 | + (b'mag ical \n tre vor\n', [], FAIL, b' tre vor\nmag ical \n'), | ||
| 21 | + (b'@\n-\n_\n#\n', [], FAIL, b'#\n-\n@\n_\n'), | ||
| 22 | + (b'extra\n\n\nwhitespace\n', [], FAIL, b'extra\nwhitespace\n'), | ||
| 23 | + (b'whitespace\n\n\nextra\n', [], FAIL, b'extra\nwhitespace\n'), | ||
| 24 | + ( | ||
| 25 | + b'fee\nFie\nFoe\nfum\n', | ||
| 26 | + [], | ||
| 27 | + FAIL, | ||
| 28 | + b'Fie\nFoe\nfee\nfum\n', | ||
| 29 | + ), | ||
| 30 | + ( | ||
| 31 | + b'Fie\nFoe\nfee\nfum\n', | ||
| 32 | + [], | ||
| 33 | + PASS, | ||
| 34 | + b'Fie\nFoe\nfee\nfum\n', | ||
| 35 | + ), | ||
| 36 | + ( | ||
| 37 | + b'fee\nFie\nFoe\nfum\n', | ||
| 38 | + ["--ignore-case"], | ||
| 39 | + PASS, | ||
| 40 | + b'fee\nFie\nFoe\nfum\n', | ||
| 41 | + ), | ||
| 42 | + ( | ||
| 43 | + b'Fie\nFoe\nfee\nfum\n', | ||
| 44 | + ["--ignore-case"], | ||
| 45 | + FAIL, | ||
| 46 | + b'fee\nFie\nFoe\nfum\n', | ||
| 47 | + ), | ||
| 24 | 48 | ), | |
| 25 | 49 | ) | |
| 26 | - def test_integration(input_s, expected_retval, output, tmpdir): | ||
| 50 | + def test_integration(input_s, argv, expected_retval, output, tmpdir): | ||
| 27 | 51 | path = tmpdir.join('file.txt') | |
| 28 | 52 | path.write_binary(input_s) | |
| 29 | 53 | ||
| 30 | - output_retval = main([str(path)]) | ||
| 54 | + output_retval = main([str(path)] + argv) | ||
| 31 | 55 | ||
| 32 | 56 | assert path.read_binary() == output | |
| 33 | 57 | assert output_retval == expected_retval | |
| Back | FazBrowse Home | New Git URL |
0 commit comments