/Mac/Demo/example0/checktext.py

http://unladen-swallow.googlecode.com/ · Python · 35 lines · 31 code · 3 blank · 1 comment · 10 complexity · e134c5d0ce429c2ce02a5b66b6a80add MD5 · raw file

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