From 1170417ceeb8c49a46cda522a38eaa71c9cae30c Mon Sep 17 00:00:00 2001 From: Derrell Lipman Date: Sat, 30 Dec 2006 05:09:59 +0000 Subject: r20414: Start to make SWAT usable by others. This is just a start... (This used to be commit 26a34037a7ca6fbd05c5a6f7c2d5973e34bc6918) --- .../frontend/framework/tool/modules/textutil.py | 143 +++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100755 webapps/qooxdoo-0.6.3-sdk/frontend/framework/tool/modules/textutil.py (limited to 'webapps/qooxdoo-0.6.3-sdk/frontend/framework/tool/modules/textutil.py') diff --git a/webapps/qooxdoo-0.6.3-sdk/frontend/framework/tool/modules/textutil.py b/webapps/qooxdoo-0.6.3-sdk/frontend/framework/tool/modules/textutil.py new file mode 100755 index 0000000000..edb2a5652d --- /dev/null +++ b/webapps/qooxdoo-0.6.3-sdk/frontend/framework/tool/modules/textutil.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python + +import sys, string, re, optparse +import config, filetool, comment + + + + +def convertMac2Unix(content): + return content.replace("\r", "\n") + +def convertMac2Dos(content): + return content.replace("\r", "\r\n") + +def convertDos2Unix(content): + return content.replace("\r\n", "\n") + +def convertDos2Mac(content): + return content.replace("\r\n", "\r") + +def convertUnix2Dos(content): + return content.replace("\n", "\r\n") + +def convertUnix2Mac(content): + return content.replace("\n", "\r") + + + + +def any2Unix(content): + # DOS must be first, because it is a combination of Unix & Mac + return convertMac2Unix(convertDos2Unix(content)) + +def any2Dos(content): + # to protect old DOS breaks first, we need to convert to + # a line ending with single character first e.g. Unix + return convertUnix2Dos(any2Unix(content)) + +def any2Mac(content): + # to protect old DOS breaks first, we need to convert to + # a line ending with single character first e.g. Unix + return convertUnix2Mac(any2Unix(content)) + + + +def getLineEndingName(content): + if "\r\n" in content: + return "dos" + + if "\r" in content: + return "mac" + + # defaults to unix + return "unix" + +def getLineEndingSequence(content): + if "\r\n" in content: + return "\r\n" + + if "\r" in content: + return "\r" + + # defaults to unix + return "\n" + + + +def tab2Space(content, spaces=2): + return content.replace("\t", " " * spaces) + +def spaces2Tab(content, spaces=2): + return content.replace(" " * spaces, "\t") + + + +def removeTrailingSpaces(content): + ending = getLineEndingSequence(content) + lines = content.split(ending) + length = len(lines) + pos = 0 + + while pos < length: + lines[pos] = lines[pos].rstrip() + pos += 1 + + return ending.join(lines) + + + + + + + + + + + + + + +def main(): + allowed = [ "any2Dos", "any2Mac", "any2Unix", "convertDos2Mac", "convertDos2Unix", "convertMac2Dos", "convertMac2Unix", "convertUnix2Dos", "convertUnix2Mac", "spaces2Tab", "tab2Space" ] + + parser = optparse.OptionParser() + + parser.add_option("-q", "--quiet", action="store_false", dest="verbose", default=False, help="Quiet output mode.") + parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Verbose output mode.") + parser.add_option("-c", "--command", dest="command", default="normalize", help="Normalize a file") + parser.add_option("--encoding", dest="encoding", default="utf-8", metavar="ENCODING", help="Defines the encoding expected for input files.") + + (options, args) = parser.parse_args() + + if not options.command in allowed: + print "Unallowed command: %s" % options.command + sys.exit(1) + + if len(args) == 0: + print "Needs one or more arguments (files) to modify!" + sys.exit(1) + + for fileName in args: + if options.verbose: + print " * Running %s on: %s" % (options.command, fileName) + + origFileContent = filetool.read(fileName, options.encoding) + patchedFileContent = eval(options.command + "(origFileContent)") + + if patchedFileContent != origFileContent: + filetool.save(fileName, patchedFileContent, options.encoding) + + + + + +if __name__ == '__main__': + try: + main() + + except KeyboardInterrupt: + print + print " * Keyboard Interrupt" + sys.exit(1) + \ No newline at end of file -- cgit