Jack Jansen | 024a387 | 1996-07-18 16:07:05 +0000 | [diff] [blame] | 1 | """checktext - Check that a text file has macintosh-style newlines""" |
| 2 | |
Jack Jansen | 024a387 | 1996-07-18 16:07:05 +0000 | [diff] [blame] | 3 | import sys |
| 4 | import EasyDialogs |
| 5 | import string |
| 6 | |
| 7 | def main(): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 8 | pathname = EasyDialogs.AskFileForOpen(message='File to check end-of-lines in:') |
| 9 | if not pathname: |
| 10 | sys.exit(0) |
| 11 | fp = open(pathname, 'rb') |
| 12 | try: |
| 13 | data = fp.read() |
| 14 | except MemoryError: |
| 15 | EasyDialogs.Message('Sorry, file is too big.') |
| 16 | sys.exit(0) |
| 17 | if len(data) == 0: |
| 18 | EasyDialogs.Message('File is empty.') |
| 19 | sys.exit(0) |
| 20 | number_cr = string.count(data, '\r') |
| 21 | number_lf = string.count(data, '\n') |
| 22 | if number_cr == number_lf == 0: |
| 23 | EasyDialogs.Message('File contains no lines.') |
| 24 | if number_cr == 0: |
| 25 | EasyDialogs.Message('File has unix-style line endings') |
| 26 | elif number_lf == 0: |
| 27 | EasyDialogs.Message('File has mac-style line endings') |
| 28 | elif number_cr == number_lf: |
| 29 | EasyDialogs.Message('File probably has MSDOS-style line endings') |
| 30 | else: |
| 31 | EasyDialogs.Message('File has no recognizable line endings (binary file?)') |
| 32 | sys.exit(0) |
| 33 | |
Jack Jansen | 024a387 | 1996-07-18 16:07:05 +0000 | [diff] [blame] | 34 | if __name__ == '__main__': |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 35 | main() |