]> git.lyx.org Git - lyx.git/blob - development/tools/unicodesymbols.py
Fix typos in Python scripts
[lyx.git] / development / tools / unicodesymbols.py
1 #! /usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 # file unicodesymbols.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7
8 # author Georg Baum
9
10 # Full author contact details are available in file CREDITS
11
12 # This script reads a unicode symbol file and completes it in the given range
13
14
15 from __future__ import print_function
16 import os, re, string, sys, unicodedata
17 import io
18
19
20 def usage(prog_name):
21     return ("Usage: %s start stop inputfile outputfile\n" % prog_name +
22             "or     %s start stop <inputfile >outputfile" % prog_name)
23
24
25 def error(message):
26     sys.stderr.write(message + '\n')
27     sys.exit(1)
28
29
30 def trim_eol(line):
31     " Remove end of line char(s)."
32     if line[-1:] == '\n':
33         return line[:-1]
34     else:
35         # file with no EOL in last line
36         return line
37
38
39 def read(input):
40     " Read input file and strip lineendings."
41     lines = list()
42     while 1:
43         line = input.readline()
44         if not line:
45             break
46         line = trim_eol(line)
47         tokens = line.split()
48         char = -1
49         if len(tokens) > 0:
50             if tokens[0][0:2] == "0x":
51                 char = int(tokens[0][2:], 16)
52             elif tokens[0][0:3] == "#0x":
53                 char = int(tokens[0][3:], 16)
54         lines.append([char, line])
55     return lines
56
57
58 def write(output, lines):
59     " Write output file."
60     for line in lines:
61         output.write(line[1] + '\n')
62
63
64 def complete(lines, start, stop):
65     l = 0
66     for i in range(start, stop):
67         # This catches both comments (lines[l][0] == -1) and code points less than i
68         while l < len(lines) and lines[l][0] < i:
69 #            print(lines[l])
70             l = l + 1
71             continue
72         if l >= len(lines) or lines[l][0] != i:
73             if sys.version_info[0] < 3:
74                 c = unichr(i)
75             else:
76                 c = chr(i)
77             name = unicodedata.name(c, "")
78             if name != "":
79                 if unicodedata.combining(c):
80                     combining = "combining"
81                 else:
82                     combining = ""
83                 line = [i, '#0x%04x ""                         "" "%s" "" "" # %s' % (i, combining, name)]
84                 lines.insert(l, line)
85 #                print(lines[l])
86                 l = l + 1
87
88
89 def main(argv):
90
91     # Open files
92     if len(argv) == 3:
93         input = sys.stdin
94         output = sys.stdout
95     elif len(argv) == 5:
96         input = io.open(argv[3], 'r', encoding='utf_8')
97         output = io.open(argv[4], 'w', encoding='utf_8')
98     else:
99         error(usage(argv[0]))
100     if argv[1][:2] == "0x":
101         start = int(argv[1][2:], 16)
102     else:
103         start = int(argv[1])
104     if argv[2][:2] == "0x":
105         stop = int(argv[2][2:], 16)
106     else:
107         stop = int(argv[2])
108
109     # Do the real work
110     lines = read(input)
111     complete(lines, start, stop)
112     write(output, lines)
113
114     # Close files
115     if len(argv) == 3:
116         input.close()
117         output.close()
118
119     return 0
120
121
122 if __name__ == "__main__":
123     main(sys.argv)