File: checktext.py

package info (click to toggle)
python2.2 2.2.3dfsg-2sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,920 kB
  • ctags: 69,127
  • sloc: ansic: 219,839; python: 203,969; sh: 9,690; makefile: 3,468; perl: 3,454; lisp: 3,248; xml: 2,262; cpp: 106; sed: 2
file content (39 lines) | stat: -rw-r--r-- 1,038 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""checktext - Check that a text file has macintosh-style newlines"""

import macfs
import sys
import EasyDialogs
import string

def main():
	fsspec, ok = macfs.PromptGetFile('File to check end-of-lines in:', 'TEXT')
	if not ok:
		sys.exit(0)
	pathname = fsspec.as_pathname()
	fp = open(pathname, 'rb')
	try:
		data = fp.read()
	except MemoryError:
		EasyDialogs.Message('Sorry, file is too big.')
		sys.exit(0)
	if len(data) == 0:
		EasyDialogs.Message('File is empty.')
		sys.exit(0)
	number_cr = string.count(data, '\r')
	number_lf = string.count(data, '\n')
	if number_cr == number_lf == 0:
		EasyDialogs.Message('File contains no lines.')
	if number_cr == 0:
		EasyDialogs.Message('File has unix-style line endings')
	elif number_lf == 0:
		EasyDialogs.Message('File has mac-style line endings')
	elif number_cr == number_lf:
		EasyDialogs.Message('File probably has MSDOS-style line endings')
	else:
		EasyDialogs.Message('File has no recognizable line endings (binary file?)')
	sys.exit(0)
	
if __name__ == '__main__':
	main()