r24958: This is the final text, and the final version. I'll send the release
[jelmer/samba4-debian.git] / webapps / qooxdoo-0.6.5-sdk / frontend / framework / tool / modules / textutil.py
1 #!/usr/bin/env python
2 ################################################################################
3 #
4 #  qooxdoo - the new era of web development
5 #
6 #  http://qooxdoo.org
7 #
8 #  Copyright:
9 #    2006-2007 1&1 Internet AG, Germany, http://www.1and1.org
10 #
11 #  License:
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.
15 #
16 #  Authors:
17 #    * Sebastian Werner (wpbasti)
18 #
19 ################################################################################
20
21 import sys, string, re, optparse
22 import config, filetool, comment
23
24
25
26
27 def convertMac2Unix(content):
28   return content.replace("\r", "\n")
29
30 def convertMac2Dos(content):
31   return content.replace("\r", "\r\n")
32
33 def convertDos2Unix(content):
34   return content.replace("\r\n", "\n")
35
36 def convertDos2Mac(content):
37   return content.replace("\r\n", "\r")
38
39 def convertUnix2Dos(content):
40   return content.replace("\n", "\r\n")
41
42 def convertUnix2Mac(content):
43   return content.replace("\n", "\r")
44
45
46
47
48 def any2Unix(content):
49   # DOS must be first, because it is a combination of Unix & Mac
50   return convertMac2Unix(convertDos2Unix(content))
51
52 def any2Dos(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))
56
57 def any2Mac(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))
61
62
63
64 def getLineEndingName(content):
65   if "\r\n" in content:
66     return "dos"
67
68   if "\r" in content:
69     return "mac"
70
71   # defaults to unix
72   return "unix"
73
74 def getLineEndingSequence(content):
75   if "\r\n" in content:
76     return "\r\n"
77
78   if "\r" in content:
79     return "\r"
80
81   # defaults to unix
82   return "\n"
83
84
85
86 def tab2Space(content, spaces=2):
87   return content.replace("\t", " " * spaces)
88
89 def spaces2Tab(content, spaces=2):
90   return content.replace(" " * spaces, "\t")
91
92
93
94 def removeTrailingSpaces(content):
95   ending = getLineEndingSequence(content)
96   lines = content.split(ending)
97   length = len(lines)
98   pos = 0
99
100   while pos < length:
101     lines[pos] = lines[pos].rstrip()
102     pos += 1
103
104   return ending.join(lines)
105
106
107 def toRegExp(text):
108   return re.compile("^(" + text.replace('.', '\\.').replace('*', '.*').replace('?', '.?') + ")$")
109
110
111
112
113
114
115
116
117
118
119
120 def main():
121   allowed = [ "any2Dos", "any2Mac", "any2Unix", "convertDos2Mac", "convertDos2Unix", "convertMac2Dos", "convertMac2Unix", "convertUnix2Dos", "convertUnix2Mac", "spaces2Tab", "tab2Space" ]
122   
123   parser = optparse.OptionParser()
124
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.")
129
130   (options, args) = parser.parse_args()
131   
132   if not options.command in allowed:
133     print "Unallowed command: %s" % options.command
134     sys.exit(1)
135
136   if len(args) == 0:
137     print "Needs one or more arguments (files) to modify!"
138     sys.exit(1)
139     
140   for fileName in args:
141     if options.verbose:
142       print "  * Running %s on: %s" % (options.command, fileName)
143     
144     origFileContent = filetool.read(fileName, options.encoding)
145     patchedFileContent = eval(options.command + "(origFileContent)")
146     
147     if patchedFileContent != origFileContent:
148       filetool.save(fileName, patchedFileContent, options.encoding)
149
150
151
152
153
154 if __name__ == '__main__':
155   try:
156     main()
157
158   except KeyboardInterrupt:
159     print
160     print "  * Keyboard Interrupt"
161     sys.exit(1)
162