summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.3-sdk/frontend/framework/tool/modules/textutil.py
blob: edb2a5652d062027030f020bca141300e9d783cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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)