S390: Use s390-64 specific ionv-modules on s390-32, too.
[glibc.git] / localedata / unicode-gen / gen_translit_font.py
blobc6c55451f34e42215d37c8549bbbdccf846810b7
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
4 # Generate a translit_font file from a UnicodeData file.
5 # Copyright (C) 2015-2016 Free Software Foundation, Inc.
6 # This file is part of the GNU C Library.
8 # The GNU C Library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
13 # The GNU C Library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with the GNU C Library; if not, see
20 # <http://www.gnu.org/licenses/>.
22 '''
23 Generate a translit_font file from UnicodeData.txt
25 To see how this script is used, call it with the “-h” option:
27 $ ./gen_translit_font -h
28 … prints usage message …
29 '''
31 import argparse
32 import time
33 import unicode_utils
35 def read_input_file(filename):
36 '''Reads the original glibc translit_font file to get the
37 original head and tail.
39 We want to replace only the part of the file between
40 “translit_start” and “translit_end”
41 '''
42 head = tail = ''
43 with open(filename, mode='r') as translit_file:
44 for line in translit_file:
45 head = head + line
46 if line.startswith('translit_start'):
47 break
48 for line in translit_file:
49 if line.startswith('translit_end'):
50 tail = line
51 break
52 for line in translit_file:
53 tail = tail + line
54 return (head, tail)
56 def output_head(translit_file, unicode_version, head=''):
57 '''Write the header of the output file, i.e. the part of the file
58 before the “translit_start” line.
59 '''
60 if ARGS.input_file and head:
61 translit_file.write(head)
62 else:
63 translit_file.write('escape_char /\n')
64 translit_file.write('comment_char %\n')
65 translit_file.write('\n')
66 translit_file.write('% Transliterations of font equivalents.\n')
67 translit_file.write('% Generated automatically from UnicodeData.txt '
68 + 'by gen_translit_font.py '
69 + 'on {:s} '.format(time.strftime('%Y-%m-%d'))
70 + 'for Unicode {:s}.\n'.format(unicode_version))
71 translit_file.write('\n')
72 translit_file.write('LC_CTYPE\n')
73 translit_file.write('\n')
74 translit_file.write('translit_start\n')
76 def output_tail(translit_file, tail=''):
77 '''Write the tail of the output file'''
78 if ARGS.input_file and tail:
79 translit_file.write(tail)
80 else:
81 translit_file.write('translit_end\n')
82 translit_file.write('\n')
83 translit_file.write('END LC_CTYPE\n')
85 def output_transliteration(translit_file):
86 '''Write the new transliteration to the output file'''
87 translit_file.write('\n')
88 for code_point in sorted(unicode_utils.UNICODE_ATTRIBUTES):
89 name = unicode_utils.UNICODE_ATTRIBUTES[code_point]['name']
90 decomposition = unicode_utils.UNICODE_ATTRIBUTES[
91 code_point]['decomposition']
92 if decomposition.startswith('<font>'):
93 decomposition = decomposition[7:]
94 decomposed_code_points = [[int(x, 16)
95 for x in decomposition.split(' ')]]
96 if decomposed_code_points[0]:
97 translit_file.write('{:s} '.format(
98 unicode_utils.ucs_symbol(code_point)))
99 for index in range(0, len(decomposed_code_points)):
100 if index > 0:
101 translit_file.write(';')
102 if len(decomposed_code_points[index]) > 1:
103 translit_file.write('"')
104 for decomposed_code_point in decomposed_code_points[index]:
105 translit_file.write('{:s}'.format(
106 unicode_utils.ucs_symbol(decomposed_code_point)))
107 if len(decomposed_code_points[index]) > 1:
108 translit_file.write('"')
109 translit_file.write(' % {:s}\n'.format(name))
110 translit_file.write('\n')
112 if __name__ == "__main__":
113 PARSER = argparse.ArgumentParser(
114 description='''
115 Generate a translit_font file from UnicodeData.txt.
116 ''')
117 PARSER.add_argument(
118 '-u', '--unicode_data_file',
119 nargs='?',
120 type=str,
121 default='UnicodeData.txt',
122 help=('The UnicodeData.txt file to read, '
123 + 'default: %(default)s'))
124 PARSER.add_argument(
125 '-i', '--input_file',
126 nargs='?',
127 type=str,
128 help=''' The original glibc/localedata/locales/translit_font
129 file.''')
130 PARSER.add_argument(
131 '-o', '--output_file',
132 nargs='?',
133 type=str,
134 default='translit_font.new',
135 help='''The new translit_font file, default: %(default)s. If the
136 original glibc/localedata/locales/translit_font file has
137 been given as an option, the header up to the
138 “translit_start” line and the tail from the “translit_end”
139 line to the end of the file will be copied unchanged into the
140 output file. ''')
141 PARSER.add_argument(
142 '--unicode_version',
143 nargs='?',
144 required=True,
145 type=str,
146 help='The Unicode version of the input files used.')
147 ARGS = PARSER.parse_args()
149 unicode_utils.fill_attributes(ARGS.unicode_data_file)
150 HEAD = TAIL = ''
151 if ARGS.input_file:
152 (HEAD, TAIL) = read_input_file(ARGS.input_file)
153 with open(ARGS.output_file, mode='w') as TRANSLIT_FILE:
154 output_head(TRANSLIT_FILE, ARGS.unicode_version, head=HEAD)
155 output_transliteration(TRANSLIT_FILE)
156 output_tail(TRANSLIT_FILE, tail=TAIL)