-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunisearch
More file actions
executable file
·99 lines (69 loc) · 2.99 KB
/
Copy pathunisearch
File metadata and controls
executable file
·99 lines (69 loc) · 2.99 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
#!/bin/env python3
import argparse
import collections
import importlib
import os
import re
import subprocess
import sys
SELFPATH = os.path.dirname(os.path.realpath(sys.argv[0]))
def load_unidecode():
global unidecode
path = os.path.join(SELFPATH, "unidecode")
loader = importlib.machinery.SourceFileLoader("unidecode", path)
spec = importlib.util.spec_from_file_location("unidecode", loader=loader)
if spec is None:
raise ModuleNotFoundError(f"No module named 'unidecode'")
unidecode = importlib.util.module_from_spec(spec)
sys.modules["unidecode"] = unidecode
spec.loader.exec_module(unidecode)
def load_data(path):
retval = []
with open(path) as fp:
for line in fp:
line = line.split(";")
codepoint = int(line[0], 16)
name = line[1]
if line[1] == "<control>":
name = line[10]
yield (name, codepoint)
def match_names(names, strings, regexes):
strings = [s.upper() for s in strings]
regexes = [re.compile(r, re.IGNORECASE) for r in regexes]
args = collections.namedtuple("Args", "normalize")(None)
first = True
for name, cp in names:
if any(s in name for s in strings) or any(r.search(name) for r in regexes):
unidecode.print_string(args, chr(cp), print_header=first)
first = False
def main():
"""Main function. Parses the arguments, search the UnicodeData.txt file for
code points matching the description."""
parser = argparse.ArgumentParser(description="Search unicode code point "
"matching the description and print its "
"informations.")
parser.add_argument("stringarg", nargs="?",
help="Substring or pattern to look for.")
parser.add_argument("--unicode-data", "-u", metavar="file",
help="Path to UnicodeData.txt to parse and search.")
parser.add_argument("--regex", "-r", metavar="REGEX", action="append",
default=[],
help="Search this regular expression. Can be supplied "
"multiple times, finds entries matching any pattern.")
parser.add_argument("--string", "-s", metavar="STRING", action="append",
default=[],
help="Search this substring. Can be supplied "
"multiple times, finds entries matching any string.")
args = parser.parse_args()
if not args.regex and not args.string and not args.stringarg:
print("Must supply at least one search pattern.", file=sys.stderr)
return 1
if args.stringarg is not None:
args.string.append(args.stringarg)
if args.unicode_data is None:
args.unicode_data = os.path.join(SELFPATH, "UnicodeData.txt")
load_unidecode()
names = load_data(args.unicode_data)
match_names(names, args.string, args.regex)
if __name__ == "__main__":
sys.exit(main())