OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/env python |
2 | |
3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 4 # found in the LICENSE file. |
6 | 5 |
7 """Convert any unicode characters found in the input file to C literals.""" | 6 """Convert any unicode characters found in the input file to C literals.""" |
8 | 7 |
9 import codecs | 8 import codecs |
10 import optparse | 9 import optparse |
11 import os | 10 import os |
12 import sys | 11 import sys |
13 | 12 |
| 13 |
14 def main(argv): | 14 def main(argv): |
15 parser = optparse.OptionParser() | 15 parser = optparse.OptionParser() |
16 usage = 'Usage: %prog -o <output_dir> <input_file>' | 16 usage = 'Usage: %prog -o <output_dir> <input_file>' |
17 parser.set_usage(usage) | 17 parser.set_usage(usage) |
18 parser.add_option('-o', dest='output_dir') | 18 parser.add_option('-o', dest='output_dir') |
19 | 19 |
20 options, arglist = parser.parse_args(argv) | 20 options, arglist = parser.parse_args(argv) |
21 | 21 |
22 if not options.output_dir: | 22 if not options.output_dir: |
23 print "output_dir required" | 23 print "output_dir required" |
24 return 1 | 24 return 1 |
25 | 25 |
26 if len(arglist) != 2: | 26 if len(arglist) != 2: |
27 print "input_file required" | 27 print "input_file required" |
28 return 1 | 28 return 1 |
29 | 29 |
30 in_filename = arglist[1] | 30 in_filename = arglist[1] |
31 | 31 |
32 if not in_filename.endswith('.utf8'): | 32 if not in_filename.endswith('.utf8'): |
33 print "input_file should end in .utf8" | 33 print "input_file should end in .utf8" |
34 return 1 | 34 return 1 |
35 | 35 |
36 out_filename = os.path.join(options.output_dir, os.path.basename( | 36 out_filename = os.path.join(options.output_dir, os.path.basename( |
37 os.path.splitext(in_filename)[0])) | 37 os.path.splitext(in_filename)[0])) |
38 | 38 |
39 WriteEscapedFile(in_filename, out_filename) | 39 WriteEscapedFile(in_filename, out_filename) |
| 40 return 0 |
40 | 41 |
41 | 42 |
42 def WriteEscapedFile(in_filename, out_filename): | 43 def WriteEscapedFile(in_filename, out_filename): |
43 input_data = codecs.open(in_filename, 'r', 'utf8').read() | 44 input_data = codecs.open(in_filename, 'r', 'utf8').read() |
44 with codecs.open(out_filename, 'w', 'ascii') as out_file: | 45 with codecs.open(out_filename, 'w', 'ascii') as out_file: |
45 for i, char in enumerate(input_data): | 46 for i, char in enumerate(input_data): |
46 if ord(char) > 127: | 47 if ord(char) > 127: |
47 out_file.write(repr(char.encode('utf8'))[1:-1]) | 48 out_file.write(repr(char.encode('utf8'))[1:-1]) |
48 if input_data[i + 1:i + 2] in '0123456789abcdefABCDEF': | 49 if input_data[i + 1:i + 2] in '0123456789abcdefABCDEF': |
49 out_file.write('""') | 50 out_file.write('""') |
50 else: | 51 else: |
51 out_file.write(char.encode('ascii')) | 52 out_file.write(char.encode('ascii')) |
52 | 53 |
53 | 54 |
54 if __name__ == '__main__': | 55 if __name__ == '__main__': |
55 exit(main(sys.argv)) | 56 sys.exit(main(sys.argv)) |
OLD | NEW |