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