-
Notifications
You must be signed in to change notification settings - Fork 4
/
picu_multi.py
executable file
·69 lines (54 loc) · 2.41 KB
/
picu_multi.py
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
#!/usr/bin/env python
import sys
import os
from picu.loader import icu_load
from picu.constants import U_UNICODE_CHAR_NAME, U_SHORT_PROPERTY_NAME
__version__ = '0.1'
__author__ = 'Wil Tan <wil@cloudregistry.net>'
def main():
import argparse
import logging
parser = argparse.ArgumentParser(description='Test script working with multiple versions of ICU')
parser.add_argument('-v', '--verbose', action='store_true', help='be verbose')
parser.add_argument('--libs', action='append', default=[])
parser.add_argument('--version', action='version', version='%(prog)s {}'.format(__version__))
parser.add_argument('codepoint', metavar='CP')
parser.add_argument('property', metavar='PROP')
args = parser.parse_args()
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG if args.verbose else logging.INFO)
script_name = sys.argv[0]
if script_name.endswith('.py'):
script_name = script_name[:-3]
logger = logging.getLogger(script_name)
if not args.libs:
libpath = os.getenv('PICU_ICU_LIB_PATH', '/usr/local/Cellar/icu4c/52.1/lib/libicuuc.52.1.dylib')
i18n_libpath = os.getenv('PICU_ICU_I18N_LIB_PATH', '/usr/local/Cellar/icu4c/52.1/lib/libicui18n.52.1.dylib')
libver = os.getenv('PICU_ICU_LIB_VER', '52')
args.libs.append('{}#{}#{}'.format(libpath, i18n_libpath, libver))
icu_libs = []
for libs in args.libs:
libpath, i18n_libpath, libver = libs.split('#')
icu = icu_load(libpath, i18n_libpath, libver)
icu_libs.append(icu)
logger.info("Library '%s' loaded. Unicode version: %s", libpath, icu.getUnicodeVersion())
# call the main workhorse function
if not icu_libs:
logging.error("No --libs given")
sys.exit(1)
cp = int(args.codepoint, 16)
prop = args.property
for icu in icu_libs:
logger.info("Unicode version: %s", icu.getUnicodeVersion())
print("U+%04X (%s)" % (cp, icu.charName(cp, U_UNICODE_CHAR_NAME)))
if prop.lower() == 'script_extensions':
print(icu.get_script_extensions(cp))
elif prop.lower() == 'script':
print(icu.get_script(cp))
elif prop.lower() == 'script_alpha4':
print(icu.get_script(cp, U_SHORT_PROPERTY_NAME))
elif prop.lower() == 'age':
print(icu.charAge(cp))
else:
print(icu.get_prop_value(cp, prop))
if __name__ == '__main__':
main()