2 ################################################################################
4 # qooxdoo - the new era of web development
9 # 2006-2007 1&1 Internet AG, Germany, http://www.1and1.org
12 # LGPL: http://www.gnu.org/licenses/lgpl.html
13 # EPL: http://www.eclipse.org/org/documents/epl-v10.php
14 # See the LICENSE file in the project's top-level directory for details.
17 # * Sebastian Werner (wpbasti)
19 ################################################################################
21 import sys, string, re, optparse
22 import config, filetool, comment
27 def convertMac2Unix(content):
28 return content.replace("\r", "\n")
30 def convertMac2Dos(content):
31 return content.replace("\r", "\r\n")
33 def convertDos2Unix(content):
34 return content.replace("\r\n", "\n")
36 def convertDos2Mac(content):
37 return content.replace("\r\n", "\r")
39 def convertUnix2Dos(content):
40 return content.replace("\n", "\r\n")
42 def convertUnix2Mac(content):
43 return content.replace("\n", "\r")
48 def any2Unix(content):
49 # DOS must be first, because it is a combination of Unix & Mac
50 return convertMac2Unix(convertDos2Unix(content))
53 # to protect old DOS breaks first, we need to convert to
54 # a line ending with single character first e.g. Unix
55 return convertUnix2Dos(any2Unix(content))
58 # to protect old DOS breaks first, we need to convert to
59 # a line ending with single character first e.g. Unix
60 return convertUnix2Mac(any2Unix(content))
64 def getLineEndingName(content):
74 def getLineEndingSequence(content):
86 def tab2Space(content, spaces=2):
87 return content.replace("\t", " " * spaces)
89 def spaces2Tab(content, spaces=2):
90 return content.replace(" " * spaces, "\t")
94 def removeTrailingSpaces(content):
95 ending = getLineEndingSequence(content)
96 lines = content.split(ending)
101 lines[pos] = lines[pos].rstrip()
104 return ending.join(lines)
108 return re.compile("^(" + text.replace('.', '\\.').replace('*', '.*').replace('?', '.?') + ")$")
121 allowed = [ "any2Dos", "any2Mac", "any2Unix", "convertDos2Mac", "convertDos2Unix", "convertMac2Dos", "convertMac2Unix", "convertUnix2Dos", "convertUnix2Mac", "spaces2Tab", "tab2Space" ]
123 parser = optparse.OptionParser()
125 parser.add_option("-q", "--quiet", action="store_false", dest="verbose", default=False, help="Quiet output mode.")
126 parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Verbose output mode.")
127 parser.add_option("-c", "--command", dest="command", default="normalize", help="Normalize a file")
128 parser.add_option("--encoding", dest="encoding", default="utf-8", metavar="ENCODING", help="Defines the encoding expected for input files.")
130 (options, args) = parser.parse_args()
132 if not options.command in allowed:
133 print "Unallowed command: %s" % options.command
137 print "Needs one or more arguments (files) to modify!"
140 for fileName in args:
142 print " * Running %s on: %s" % (options.command, fileName)
144 origFileContent = filetool.read(fileName, options.encoding)
145 patchedFileContent = eval(options.command + "(origFileContent)")
147 if patchedFileContent != origFileContent:
148 filetool.save(fileName, patchedFileContent, options.encoding)
154 if __name__ == '__main__':
158 except KeyboardInterrupt:
160 print " * Keyboard Interrupt"