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