Enrico Franchi Homepage

Questo sito è obsoleto. La nuova versione di questa pagina è disponibile presso akropolix

This site is old. The new version of this page is available on akropolix

This is a very simple Python script I use to convert text files with Mac line endings in text files with unix line endings. It needs some Python libraries available only on MacOS, but it should not be difficult to make a portable version (without GUI or with another GUI toolkit).
This one is meant to be packaged as an applet with BuildApplet, included in MacPython.

unixify.py (download)
# Enrico Franchi 2004(c)
# unixify.py
# Version 0.5
# This is released under Python Software Foundation Licence Version 2

# Usage: pythonw unixify.py
# You may build an applet.

import re
import os
import sys
import MacOS
import EasyDialogs

extensions= [".txt", ".text", ".mac", ".unix", ".dos", 
		".c", ".cc", ".cpp", ".cxx", ".hxx", ".h", ".hpp",
		".htm", ".html", ".xml", ".xsl", ".sgml", ".tex"
		".py", ".pl", ".cgi", ".m", ".mm", ".texinfo" ]

def convert(file):
	"""Creates a backup copy and converts \
	fileending from everything to unix"""
	try:
		fh = open(file,"r")
	except IOError:
		EasyDialogs.Message("Could not open %s" % file)
		return
	str = fh.read()
	fh.close()

	if str.find("\r") != -1:
		oldfile = file + ".old"
		try:
			fho = open(oldfile, "w+")
			fho.write(str)
			fho.close()
			del str
		except IOError:
			EasyDialogs.Message\
			     ("Could not open %s for writing" % oldfile)
		try:
			fh = open(file,"rU")
			str = fh.read()
			fh.close()
		except IOError:
			EasyDialogs.Message("Could not open %s" % file)
			os.remove(oldfile)
			return
		try:
			fh = open(file,"w")
			fh.write(str)
			fh.close()
		except IOError:
			EasyDialogs.Message\
			     ("Could not open %s for writing" % file)
			try:
				os.remove(file)
			except:
				pass
			os.rename(oldfile, file)
			return

def loop(dir, extensions):
	""" this calls convert recursively on all text files """
	goodFileList = []
	fileList = [ os.path.normcase(f) for f in os.listdir(dir) \
	             if os.path.isfile(os.path.join(dir,f))]
	dirList = [ os.path.normcase(d) for d in os.listdir(dir) \
	            if os.path.isdir(os.path.join(dir,d))]
	for f in fileList:
		# now we are evalutating wether 
		# the file is likely to be ascii
		(root, ext) = os.path.splitext(f)
		try:
			is_there = extensions.index(ext.lower())

			if is_there:
				goodFileList.append(f)
				continue
		except ValueError:
			pass
		# checks TYPE
		#(cr, ty) = MacOS.GetCreatorAndType(f)
		#if ty=="ttxt":
		#	goodFileList.append(f)
	if len(goodFileList) > 0:
		(path, name) = os.path.split(dir)
		print "\n%s: " % name
	for f in goodFileList:
		convert(f)
	for d in dirList:
		loop(os.path.join(dir,d), extensions)

if __name__ == "__main__":
	if len(sys.argv) == 1:
		args = EasyDialogs.GetArgv()
	else:
		args = sys.argv[1:]
	for file in args:
		if os.path.isfile(os.path.normcase(file)):
			convert(file)
		elif os.path.isdir(os.path.normcase(file)):
			if EasyDialogs.AskYesNoCancel\
			         ("Do you want to process all files\
					 inside the directory %s?" % file,
					cancel=""):
				loop(file, extensions)

back

xhtml 1.1 CSS 2.1 RSS 2.0
Made with a Mac Made with BBEdit Made with Brain

All documentation is under FDL and all source code is under BSD, unless differently stated.

24-mar-06