Mercurial > hg > lxmldump
annotate lxmldump.py @ 33:3dcf8ac43bda
Add one word to debug list.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 25 May 2021 12:57:34 +0300 |
parents | cfecd039506e |
children | 73f2f98e3eef |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python3 -B |
2 # coding=utf-8 | |
3 ### | |
4 | 4 ### lxmldump - Dump ISO/FDIS 1951 XML file data |
5 ### Programmed and designed by Matti 'ccr' Hämäläinen <ccr@tnsp.org> | |
6 ### (C) Copyright 2021 Tecnic Software productions (TNSP) | |
7 ### | |
23 | 8 ### Released / distributed under 3-clause BSD license |
9 ### (see file "COPYING" for more information) | |
10 ### | |
4 | 11 ### Python 3.7+ required! |
0 | 12 ### |
13 import sys | |
14 import signal | |
15 import re | |
16 from pathlib import Path | |
17 import xml.etree.ElementTree as xmlET | |
5
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
18 import unicodedata |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
19 import argparse |
0 | 20 |
21 assert sys.version_info >= (3, 7) | |
22 | |
23 | |
24 ### | |
25 ### Default settings | |
26 ### | |
25
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
27 # Operation modes |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
28 PKK_MODE_NORMAL = 0 |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
29 PKK_MODE_DUMP = 1 |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
30 PKK_MODE_XML = 2 |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
31 |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
32 |
30
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
33 pkk_modes_list = { |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
34 PKK_MODE_NORMAL: "normal", |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
35 PKK_MODE_DUMP: "dump", |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
36 PKK_MODE_XML: "xml", |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
37 } |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
38 |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
39 |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
40 # Default Ptr URL format strings |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
41 pkk_ptr_url_fmt = { |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
42 PKK_MODE_NORMAL: u"<PTR:{href}>{text}</PTR>", |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
43 } |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
44 |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
45 |
25
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
46 # Element annotation mappings |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
47 pkk_element_annotation_map = { |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
48 "Fragment" : { |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
49 PKK_MODE_NORMAL: ["<", ">"], |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
50 }, |
7 | 51 } |
52 | |
53 | |
28 | 54 # List of words in kks1/ useful for debugging, option -p |
7 | 55 pkk_debug_list = [ |
56 "ahas", | |
57 "ahavakkaine", | |
58 "ahavakala", | |
59 "ahavakoittuo", | |
60 "ahvaliha", | |
9 | 61 "aloilleh", |
18 | 62 "hanjahtoakseh", |
19 | 63 "akkalisto", |
33 | 64 "alto-", |
7 | 65 ] |
66 | |
67 | |
21 | 68 |
0 | 69 ### |
70 ### Misc. helper functions, etc | |
71 ### | |
72 def pkk_cleanup(): | |
73 return 0 | |
74 | |
75 | |
13 | 76 ## Print string to stdout using normalized Unicode if enabled |
5
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
77 def pkk_print(smsg): |
12 | 78 try: |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
79 if pkk_cfg.normalize: |
12 | 80 sys.stdout.write(unicodedata.normalize("NFC", smsg)) |
81 else: | |
82 sys.stdout.write(smsg) | |
83 | |
84 except (BrokenPipeError, IOError) as e: | |
85 sys.stderr.close() | |
86 | |
0 | 87 |
13 | 88 ## Print string with indentation |
7 | 89 def pkk_printi(indent, smsg): |
22 | 90 pkk_print((" " * pkk_cfg.indent * indent) + smsg) |
7 | 91 |
0 | 92 |
93 ## Fatal error handler | |
94 def pkk_fatal(smsg): | |
95 print(u"ERROR: "+ smsg) | |
96 sys.exit(1) | |
97 | |
98 | |
99 ## Handler for SIGINT signals | |
100 def pkk_signal_handler(signal, frame): | |
101 pkk_cleanup() | |
102 print(u"\nQuitting due to SIGINT / Ctrl+C!") | |
103 sys.exit(1) | |
104 | |
105 | |
28 | 106 ## Annotate given string with prefix and suffix based on tag |
26 | 107 def pkk_str_annotate(mtag, mstr): |
25
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
108 if pkk_cfg.annotate and mtag in pkk_element_annotation_map: |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
109 if pkk_cfg.mode in pkk_element_annotation_map[mtag]: |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
110 mmode = pkk_cfg.mode |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
111 else: |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
112 mmode = PKK_MODE_NORMAL |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
113 |
26 | 114 return pkk_element_annotation_map[mtag][mmode][0] + mstr + pkk_element_annotation_map[mtag][mmode][1] |
25
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
115 else: |
26 | 116 return mstr |
25
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
117 |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
118 |
13 | 119 ## Clean string by removing tabs and newlines |
10 | 120 def pkk_str_clean(mstr): |
121 return re.sub(r'[\n\r\t]', '', mstr) | |
122 | |
123 | |
28 | 124 ## Format a "Ptr" node as text |
9 | 125 def pkk_ptr_to_text(pnode): |
30
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
126 # If custom format set, use it |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
127 if pkk_cfg.ptr_url_fmt != None: |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
128 pfmt = pkk_cfg.ptr_url_fmt |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
129 elif pkk_cfg.mode in pkk_ptr_url_fmt: |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
130 # Else try mode-specific |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
131 pfmt = pkk_ptr_url_fmt[pkk_cfg.mode] |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
132 else: |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
133 # Last resort is normal mode format |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
134 pfmt = pkk_ptr_url_fmt[PKK_MODE_NORMAL] |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
135 |
30
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
136 return pfmt.format( |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
137 text=("".join(pnode.itertext())).strip(), |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
138 href=pnode.attrib["{http://www.w3.org/TR/xlink}href"]) |
9 | 139 |
140 | |
13 | 141 ## Get text inside a given node |
16 | 142 def pkk_node_to_text(lnode): |
7 | 143 stmp = "" |
144 for pnode in lnode.iter(): | |
9 | 145 if pnode.tag == "Ptr": |
146 stmp += pkk_ptr_to_text(pnode) | |
147 else: | |
148 if isinstance(pnode.text, str): | |
25
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
149 stmp += pkk_str_annotate(pnode.tag, pkk_str_clean(pnode.text).strip()) |
7 | 150 |
9 | 151 if isinstance(pnode.tail, str): |
10 | 152 stmp += pkk_str_clean(pnode.tail) |
7 | 153 |
154 return stmp.strip() | |
155 | |
156 | |
13 | 157 ## Simple recursive dump starting at given node |
7 | 158 def pkk_dump_recursive(indent, lnode): |
159 if lnode.tag in ["Example"]: | |
16 | 160 stmp = pkk_node_to_text(lnode) |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
161 pkk_printi(indent, f"{lnode.tag} \"{stmp}\"\n") |
6 | 162 else: |
7 | 163 if isinstance(lnode.text, str): |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
164 textstr = pkk_str_clean(lnode.text).strip() |
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
165 if textstr != "": |
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
166 textstr = " \""+ textstr +"\"" |
7 | 167 else: |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
168 textstr = "" |
6 | 169 |
170 if len(lnode.attrib) > 0: | |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
171 attrstr = " "+ str(lnode.attrib) |
6 | 172 else: |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
173 attrstr = "" |
6 | 174 |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
175 pkk_printi(indent, f"{lnode.tag}{attrstr}{textstr}\n") |
6 | 176 for qnode in lnode.findall("./*"): |
7 | 177 pkk_dump_recursive(indent + 1, qnode) |
6 | 178 |
179 | |
17 | 180 ## Output item(s) under given node with given format string |
13 | 181 def pkk_output_subs_fmt(indent, dnode, dsub, dname, dfmt): |
7 | 182 for qnode in dnode.findall(dsub): |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
183 pkk_printi(indent, dfmt.format(nname=dname, ntext=pkk_node_to_text(qnode))) |
7 | 184 |
17 | 185 ## Output item(s) under given node with a prefixed name string |
13 | 186 def pkk_output_subs_prefix(indent, dnode, dsub, dname): |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
187 pkk_output_subs_fmt(indent, dnode, dsub, dname, "{nname} \"{ntext}\"\n") |
7 | 188 |
189 | |
17 | 190 ## Output a main "Headword" or "Sense" node under it |
7 | 191 def pkk_output_sense(indent, dnode): |
17 | 192 # Search form and definition |
13 | 193 pkk_output_subs_prefix(indent, dnode, "./SearchForm", "srch") |
194 pkk_output_subs_prefix(indent, dnode, "./Definition", "defn") | |
7 | 195 |
17 | 196 # Examples |
7 | 197 for wnode in dnode.findall("./ExampleBlock/ExampleCtn"): |
16 | 198 sstr = pkk_node_to_text(wnode.find("./Example")) |
8 | 199 lstr = "" |
6 | 200 |
24
8c8e8e4504bb
Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
201 ltmp = [] |
8c8e8e4504bb
Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
202 for qnode in wnode.findall("./FreeTopic[@type='levikki']/GeographicalUsage"): |
8c8e8e4504bb
Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
203 ltmp.append("{} [{}]".format(pkk_node_to_text(qnode), qnode.attrib["class"])) |
8 | 204 |
24
8c8e8e4504bb
Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
205 if len(ltmp) > 0: |
8c8e8e4504bb
Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
206 lstr = " ({})".format(", ".join(ltmp)) |
7 | 207 |
208 pkk_printi(indent + 1, "{} \"{}\"{}\n".format("exmp", sstr, lstr)) | |
209 | |
210 | |
17 | 211 ## Output one "DictionaryEntry" node |
7 | 212 def pkk_output_node(indent, dnode): |
6 | 213 |
7 | 214 for wnode in dnode.findall("./HeadwordCtn"): |
18 | 215 # Create list with grammatical attributes (noun, verb, etc.) |
216 tmpl = [] | |
217 for pnode in wnode.findall("./PartOfSpeechCtn/PartOfSpeech"): | |
218 tmpl.append(pnode.attrib["freeValue"]) | |
219 | |
220 for pnode in wnode.findall("./GrammaticalNote"): | |
19 | 221 tmpl.append(pkk_node_to_text(pnode)) |
18 | 222 |
223 # Remove duplicates and sort the list | |
224 tmpl = list(set(tmpl)) | |
225 tmpl.sort(reverse=False, key=lambda attr: (attr, len(attr))) | |
226 | |
227 # Print the headword and attributes if any | |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
228 pkk_output_subs_fmt(indent, wnode, "./Headword", "", "\"{ntext}\"") |
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
229 |
18 | 230 if len(tmpl) > 0: |
32
cfecd039506e
Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
231 pkk_print(" ({nlist})".format(nlist=pkk_cfg.word_attr_sep.join(tmpl))) |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
232 |
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
233 pkk_print("\n") |
18 | 234 |
235 # Print main "sense" | |
7 | 236 pkk_output_sense(indent + 1, wnode) |
6 | 237 |
18 | 238 # Print any other "senses" |
7 | 239 index = 1 |
240 for wnode in dnode.findall("./SenseGrp"): | |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
241 pkk_printi(indent + 1, f"sense #{index}\n") |
7 | 242 pkk_output_sense(indent + 2, wnode) |
243 index += 1 | |
6 | 244 |
245 | |
0 | 246 ### |
247 ### Main program starts | |
248 ### | |
249 signal.signal(signal.SIGINT, pkk_signal_handler) | |
250 | |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
251 optparser = argparse.ArgumentParser( |
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
252 description="lxmldump - Dump ISO/FDIS 1951 XML file data", |
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
253 usage="%(prog)s [options] <input xml file(s)>", |
31
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
254 add_help=False |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
255 ) |
0 | 256 |
22 | 257 optparser.add_argument("filenames", |
258 type=str, action="extend", nargs="*", | |
259 metavar="filename", | |
260 help="XML filename(s)") | |
0 | 261 |
31
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
262 optparser.add_argument("-h", "--help", |
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
263 dest="show_help", |
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
264 action="store_true", |
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
265 help="show this help message and exit") |
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
266 |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
267 optparser.add_argument("-d", "--dump", |
22 | 268 dest="mode", |
21 | 269 action="store_const", const=PKK_MODE_DUMP, default=PKK_MODE_NORMAL, |
22 | 270 help="output as simple dump") |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
271 |
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
272 optparser.add_argument("-x", "--xml", |
22 | 273 dest="mode", |
21 | 274 action="store_const", const=PKK_MODE_XML, |
22 | 275 help="output as XML") |
0 | 276 |
30
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
277 optparser.add_argument("--ptr-url-fmt", |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
278 dest="ptr_url_fmt", |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
279 type=str, |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
280 default=None, |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
281 metavar="str", |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
282 help='Ptr URL format string (see below)') |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
283 |
32
cfecd039506e
Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
284 optparser.add_argument("--attr-sep", |
cfecd039506e
Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
285 dest="word_attr_sep", |
cfecd039506e
Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
286 type=str, default=" ; ", |
cfecd039506e
Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
287 metavar="str", |
cfecd039506e
Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
288 help='word attribute separator (default: \"%(default)s\")') |
cfecd039506e
Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
289 |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
290 optparser.add_argument("-n", "--normalize", |
22 | 291 dest="normalize", |
29
f91ef7d7615b
Use store_true feature of argparse.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
292 action="store_true", |
22 | 293 help="output NFC normalized Unicode") |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
294 |
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
295 optparser.add_argument("-a", "--annotate", |
22 | 296 dest="annotate", |
29
f91ef7d7615b
Use store_true feature of argparse.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
297 action="store_true", |
22 | 298 help="annotate strings") |
0 | 299 |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
300 optparser.add_argument("-p", "--debug", |
29
f91ef7d7615b
Use store_true feature of argparse.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
301 dest="debug", |
f91ef7d7615b
Use store_true feature of argparse.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
302 action="store_true", |
22 | 303 help=argparse.SUPPRESS) |
304 | |
305 optparser.add_argument("-i", "--indent", | |
306 dest="indent", | |
307 type=int, choices=range(0, 32), default=4, | |
308 metavar="n", | |
309 help='indent output by <n> characters (default: %(default)s)') | |
0 | 310 |
311 | |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
312 ### Show help if needed |
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
313 pkk_cfg = optparser.parse_args() |
31
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
314 if len(pkk_cfg.filenames) == 0 or pkk_cfg.show_help: |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
315 optparser.print_help() |
31
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
316 |
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
317 print(u"\nDefault Ptr format strings per mode:") |
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
318 for pmode in pkk_modes_list: |
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
319 if pmode in pkk_ptr_url_fmt: |
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
320 print(u" {:6s} : \"{}\"".format(pkk_modes_list[pmode], pkk_ptr_url_fmt[pmode])) |
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
321 print(u"") |
0 | 322 sys.exit(0) |
323 | |
324 | |
6 | 325 ### Handle each input file |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
326 for filename in pkk_cfg.filenames: |
0 | 327 # Parse XML file into element tree |
328 try: | |
329 uxml = xmlET.parse(filename) | |
330 except Exception as e: | |
331 pkk_fatal(u"SVG/XML parsing failed: {0}".format(str(e))) | |
332 | |
333 # Dump output | |
334 try: | |
335 xroot = uxml.getroot() | |
336 for dnode in xroot.findall("./DictionaryEntry"): | |
7 | 337 |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
338 if pkk_cfg.debug and dnode.attrib["identifier"] not in pkk_debug_list: |
7 | 339 continue |
340 | |
21 | 341 if pkk_cfg.mode == PKK_MODE_NORMAL: |
19 | 342 try: |
343 pkk_output_node(0, dnode) | |
344 except Exception as e: | |
345 pkk_dump_recursive(0, dnode) | |
346 print(str(e)) | |
347 sys.exit(0) | |
21 | 348 elif pkk_cfg.mode == PKK_MODE_DUMP: |
7 | 349 pkk_dump_recursive(0, dnode) |
21 | 350 elif pkk_cfg.mode == PKK_MODE_XML: |
10 | 351 pkk_print(str(xmlET.tostring(dnode, encoding="utf8")) + "\n") |
0 | 352 else: |
10 | 353 pkk_fatal("Invalid operation mode?") |
7 | 354 |
355 print("\n") | |
0 | 356 |
357 except (BrokenPipeError, IOError) as e: | |
358 sys.stderr.close() | |
359 sys.exit(1) | |
360 | |
361 pkk_cleanup() | |
362 sys.exit(0) |