-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanXCompose
More file actions
executable file
·99 lines (77 loc) · 2.94 KB
/
cleanXCompose
File metadata and controls
executable file
·99 lines (77 loc) · 2.94 KB
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
#!/usr/bin/env python3
from typing import Callable
from argparse import ArgumentParser
import toXComposeTools as toXC
from functools import partial
import logging
import sys
def main(infile: str, outfile: str) -> None:
process_line = partial(process_line_using, toXC.char2xc)
with open(infile) as inXC:
with open(outfile, 'w') as outXC:
for each in (process_line(line) for line in inXC):
outXC.write(each + '\n')
def process_line_using(usr_char2xc: Callable[[str, str], str],
line: str) -> str:
"""
process lines of form
<SOME> <KEYS> : "C" # BLAH
returning the line linted using "C" as the key for unicode lookup.
"""
if not line or line == "\n":
return "\n"
if is_comment(line) or is_include(line):
return line.strip()
if not line.count(':') == 1 and line.count('"') >= 2:
logging.warning("Ignoring line with unrecognized format:\n\t" + line)
return line.strip()
(keys_char_uhx, _, name) = line.partition('#')
(keys, _, char_uhx) = keys_char_uhx.partition(':')
(_, char, uhx) = char_uhx.strip().split('"')
char = char.strip()
uhx = uhx.strip().upper()
name = name.strip()
if uhx:
charLup = toXC.lookup_char(char)
uhexLup = toXC.lookup_uhex(uhx)
if charLup != uhexLup:
if prompt_charT_uhexF(
'Conflict in XCompose: ("' + char + '", ' + uhx + ').\n'
+ '\t[c] Character lookup gives: '
+ '"' + charLup[0] + '", ' +
charLup[1] + ', ' + charLup[2] + '\n'
+ '\t[h] Hex codepoint lookup gives: '
+ '"' + uhexLup[0] + '", ' +
uhexLup[1] + ', ' + uhexLup[2] + '\n'
+ 'Which is correct?'):
(_, uhx, name) = charLup
else:
(char, uhx, name) = uhexLup
return usr_char2xc(char, ' '.join(keys.split()))
def is_include(line: str) -> bool:
if "include" in line:
return True
else:
return False
def is_comment(line: str) -> bool:
if line.strip()[0] == '#':
return True
else:
return False
def prompt_charT_uhexF(question: str) -> bool:
reply = input(question + ' ([c]har/[h]ex): ').lower().strip()
if reply in ["c", "ch", "chr", "char"]:
return True
elif reply in ["h", "he", "hx", "hex"]:
return False
else:
return prompt_charT_uhexF("Invalid response. Please enter")
if __name__ == '__main__':
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
args = ArgumentParser(
description="Removes extraneous spaces, verifies hex codes, \
inserts Unicode names from Unicode database.")
args.add_argument('infile', type=str, help='Path to XCompose file')
args.add_argument('outfile', type=str, help='Path to output file')
args = args.parse_args()
main(args.infile, args.outfile)