Mercurial > hg > lxmldump
annotate lxmldump.py @ 43:8ed576574712
More improvements to output flexibility.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 26 May 2021 13:12:02 +0300 |
parents | 508de0f6836b |
children | d7b4b2fb0214 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python3 -B |
2 # coding=utf-8 | |
3 ### | |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
4 ### lxmldump - Convert and dump ISO/FDIS 1951 XML file data |
4 | 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 |
39
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
20 import textwrap |
0 | 21 |
22 assert sys.version_info >= (3, 7) | |
23 | |
24 | |
25 ### | |
26 ### Default settings | |
27 ### | |
25
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
28 # Operation modes |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
29 PKK_MODE_NORMAL = 0 |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
30 PKK_MODE_DUMP = 1 |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
31 PKK_MODE_XML = 2 |
35
5aafa87dbec2
Some Anki-related stuff, not finalized and thus hidden.
Matti Hamalainen <ccr@tnsp.org>
parents:
34
diff
changeset
|
32 PKK_MODE_ANKI = 3 |
25
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
33 |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
34 |
30
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
35 pkk_modes_list = { |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
36 PKK_MODE_NORMAL: "normal", |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
37 PKK_MODE_DUMP: "dump", |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
38 PKK_MODE_XML: "xml", |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
39 PKK_MODE_ANKI: "anki", |
30
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 |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
43 pkk_mode_defaults = { |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
44 # Default Ptr URL format strings |
39
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
45 "ptr_fmt": { |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
46 PKK_MODE_NORMAL: u"<PTR:{href}>{text}</PTR>", |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
47 PKK_MODE_ANKI: u"<a href='https://kaino.kotus.fi/cgi-bin/kks/karjala.cgi?a={href}'>{text}</a>", |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
48 }, |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
49 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
50 "word_fmt": { |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
51 PKK_MODE_NORMAL: "\"{word}\"{search}{attr}\n", |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
52 PKK_MODE_ANKI: "{word}{attr}\n", |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
53 }, |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
54 "word_attr_list": { |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
55 PKK_MODE_NORMAL: " ({alist}) ", |
39
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
56 }, |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
57 "word_attr_list_empty": { |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
58 PKK_MODE_NORMAL: " ", |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
59 }, |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
60 "word_attr_list_item": { |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
61 PKK_MODE_NORMAL: "{text}", |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
62 }, |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
63 "word_attr_list_sep": { |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
64 PKK_MODE_NORMAL: " ; ", |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
65 PKK_MODE_ANKI: " : ", |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
66 }, |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
67 |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
68 "search_list": { |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
69 PKK_MODE_NORMAL: ", {alist}", |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
70 }, |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
71 "search_list_empty": { |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
72 PKK_MODE_NORMAL: "", |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
73 }, |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
74 "search_list_item": { |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
75 PKK_MODE_NORMAL: "\"{text}\"", |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
76 }, |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
77 "search_list_sep": { |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
78 PKK_MODE_NORMAL: ", ", |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
79 }, |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
80 |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
81 "sense_index": { |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
82 PKK_MODE_NORMAL: "{indent}sense #{index}\n", |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
83 PKK_MODE_ANKI: "[{index}]:\n", |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
84 }, |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
85 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
86 "definition_fmt": { |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
87 PKK_MODE_NORMAL: "{indent}defn \"{text}\"\n", |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
88 PKK_MODE_ANKI: "? {text}\n", |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
89 }, |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
90 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
91 "example_fmt": { |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
92 PKK_MODE_NORMAL: "{indent}exmp \"{text}\"{geostr}\n", |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
93 PKK_MODE_ANKI: "- {text}{geostr}\n", |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
94 }, |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
95 "example_geo_list": { |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
96 PKK_MODE_NORMAL: " ({alist})", |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
97 }, |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
98 "example_geo_list_empty": { |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
99 PKK_MODE_NORMAL: "", |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
100 }, |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
101 "example_geo_list_item": { |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
102 PKK_MODE_NORMAL: "{text} [{tclass}]", |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
103 }, |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
104 "example_geo_list_sep": { |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
105 PKK_MODE_NORMAL: ", ", |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
106 }, |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
107 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
108 "word_end": { |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
109 PKK_MODE_NORMAL: "\n", |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
110 }, |
30
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
111 } |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
112 |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
113 |
25
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
114 # Element annotation mappings |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
115 pkk_element_annotation_map = { |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
116 "Fragment" : { |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
117 PKK_MODE_NORMAL: ["<", ">"], |
35
5aafa87dbec2
Some Anki-related stuff, not finalized and thus hidden.
Matti Hamalainen <ccr@tnsp.org>
parents:
34
diff
changeset
|
118 PKK_MODE_ANKI: ["<", ">"], |
25
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
119 }, |
7 | 120 } |
121 | |
122 | |
28 | 123 # List of words in kks1/ useful for debugging, option -p |
7 | 124 pkk_debug_list = [ |
125 "ahas", | |
126 "ahavakkaine", | |
127 "ahavakala", | |
128 "ahavakoittuo", | |
129 "ahvaliha", | |
9 | 130 "aloilleh", |
18 | 131 "hanjahtoakseh", |
19 | 132 "akkalisto", |
33 | 133 "alto-", |
7 | 134 ] |
135 | |
136 | |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
137 pkk_settings = {} |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
138 |
21 | 139 |
0 | 140 ### |
141 ### Misc. helper functions, etc | |
142 ### | |
143 def pkk_cleanup(): | |
144 return 0 | |
145 | |
146 | |
13 | 147 ## 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
|
148 def pkk_print(smsg): |
12 | 149 try: |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
150 if pkk_cfg.normalize: |
12 | 151 sys.stdout.write(unicodedata.normalize("NFC", smsg)) |
152 else: | |
153 sys.stdout.write(smsg) | |
154 | |
155 except (BrokenPipeError, IOError) as e: | |
156 sys.stderr.close() | |
157 | |
0 | 158 |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
159 ## Get indentation string |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
160 def pkk_geti(indent): |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
161 return " " * pkk_cfg.indent * indent |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
162 |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
163 |
13 | 164 ## Print string with indentation |
7 | 165 def pkk_printi(indent, smsg): |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
166 pkk_print(pkk_geti(indent) + smsg) |
7 | 167 |
0 | 168 |
169 ## Fatal error handler | |
170 def pkk_fatal(smsg): | |
171 print(u"ERROR: "+ smsg) | |
172 sys.exit(1) | |
173 | |
174 | |
175 ## Handler for SIGINT signals | |
176 def pkk_signal_handler(signal, frame): | |
177 pkk_cleanup() | |
178 print(u"\nQuitting due to SIGINT / Ctrl+C!") | |
179 sys.exit(1) | |
180 | |
181 | |
39
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
182 class pkk_set_mode(argparse.Action): |
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
183 def __call__(self, parser, namespace, value, option_string=None): |
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
184 lvalue = value.strip().lower() |
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
185 for mode in pkk_modes_list: |
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
186 if pkk_modes_list[mode] == lvalue: |
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
187 setattr(namespace, self.dest, mode) |
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
188 return |
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
189 |
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
190 pkk_fatal(f"Invalid output mode '{lvalue}'.") |
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
191 |
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
192 |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
193 ## Value handling |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
194 class pkk_set_value(argparse.Action): |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
195 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
196 rexpr = re.compile(r'\s*(\w+)\s*=\s*(.*)\s*') |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
197 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
198 def __call__(self, parser, namespace, values, option_string=None): |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
199 rmatch = re.match(self.rexpr, values) |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
200 if rmatch: |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
201 rid = rmatch.group(1).lower().replace("-", "_") |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
202 rval = rmatch.group(2) |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
203 if rid in pkk_mode_defaults: |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
204 pkk_settings[rid] = rval |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
205 else: |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
206 pkk_fatal(f"Invalid option '{option_string} {values}': No such ID '{rid}'.") |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
207 else: |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
208 pkk_fatal(f"Invalid option '{option_string} {values}': Expected id=value.") |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
209 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
210 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
211 ## Get mode if it exists |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
212 def pkk_test_value(mid): |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
213 if mid in pkk_mode_defaults: |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
214 if pkk_cfg.mode in pkk_mode_defaults[mid]: |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
215 mmode = pkk_cfg.mode |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
216 else: |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
217 mmode = PKK_MODE_NORMAL |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
218 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
219 if mmode in pkk_mode_defaults[mid]: |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
220 return mmode |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
221 else: |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
222 return None |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
223 else: |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
224 return None |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
225 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
226 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
227 ## Get default value per mode |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
228 def pkk_get_value(mid): |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
229 if mid in pkk_settings and pkk_settings[mid] != None: |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
230 return pkk_settings[mid] |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
231 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
232 mmode = pkk_test_value(mid) |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
233 if mmode == None: |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
234 pkk_fatal(f"Internal error: No mode for ID '{mid}'.") |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
235 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
236 return pkk_mode_defaults[mid][mmode] |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
237 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
238 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
239 def pkk_get_fmt(mid): |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
240 return pkk_get_value(mid).replace("\\n", "\n") |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
241 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
242 |
28 | 243 ## Annotate given string with prefix and suffix based on tag |
26 | 244 def pkk_str_annotate(mtag, mstr): |
25
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
245 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
|
246 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
|
247 mmode = pkk_cfg.mode |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
248 else: |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
249 mmode = PKK_MODE_NORMAL |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
250 |
26 | 251 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
|
252 else: |
26 | 253 return mstr |
25
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
254 |
8a6738f67106
Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
255 |
13 | 256 ## Clean string by removing tabs and newlines |
10 | 257 def pkk_str_clean(mstr): |
258 return re.sub(r'[\n\r\t]', '', mstr) | |
259 | |
260 | |
28 | 261 ## Format a "Ptr" node as text |
9 | 262 def pkk_ptr_to_text(pnode): |
39
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
263 pfmt = pkk_get_fmt("ptr_fmt") |
30
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
264 return pfmt.format( |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
265 text=("".join(pnode.itertext())).strip(), |
34755af2ea1f
Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
266 href=pnode.attrib["{http://www.w3.org/TR/xlink}href"]) |
9 | 267 |
268 | |
13 | 269 ## Get text inside a given node |
16 | 270 def pkk_node_to_text(lnode): |
7 | 271 stmp = "" |
272 for pnode in lnode.iter(): | |
9 | 273 if pnode.tag == "Ptr": |
274 stmp += pkk_ptr_to_text(pnode) | |
275 else: | |
276 if isinstance(pnode.text, str): | |
37
e176fcfc0235
Fix overstripping of concatenated strings.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
277 stmp += pkk_str_annotate(pnode.tag, pkk_str_clean(pnode.text)) |
7 | 278 |
9 | 279 if isinstance(pnode.tail, str): |
10 | 280 stmp += pkk_str_clean(pnode.tail) |
7 | 281 |
282 return stmp.strip() | |
283 | |
284 | |
13 | 285 ## Simple recursive dump starting at given node |
7 | 286 def pkk_dump_recursive(indent, lnode): |
287 if lnode.tag in ["Example"]: | |
16 | 288 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
|
289 pkk_printi(indent, f"{lnode.tag} \"{stmp}\"\n") |
6 | 290 else: |
7 | 291 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
|
292 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
|
293 if textstr != "": |
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
294 textstr = " \""+ textstr +"\"" |
7 | 295 else: |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
296 textstr = "" |
6 | 297 |
298 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
|
299 attrstr = " "+ str(lnode.attrib) |
6 | 300 else: |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
301 attrstr = "" |
6 | 302 |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
303 pkk_printi(indent, f"{lnode.tag}{attrstr}{textstr}\n") |
6 | 304 for qnode in lnode.findall("./*"): |
7 | 305 pkk_dump_recursive(indent + 1, qnode) |
6 | 306 |
307 | |
17 | 308 ## Output item(s) under given node with given format string |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
309 def pkk_output_subs(indent, dnode, dsub, dfmtname): |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
310 dfmt = pkk_get_fmt(dfmtname) |
7 | 311 for qnode in dnode.findall(dsub): |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
312 pkk_print(dfmt.format( |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
313 text=pkk_node_to_text(qnode), |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
314 indent=pkk_geti(indent))) |
7 | 315 |
316 | |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
317 def pkk_get_list_str(dlist, dprefix, dfilter): |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
318 if len(dlist) > 0: |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
319 if dfilter: |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
320 tfmt = pkk_get_fmt(dprefix + "_list_item") |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
321 tlist = [tfmt.format(text=i) for i in dlist] |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
322 else: |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
323 tlist = dlist |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
324 |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
325 return pkk_get_fmt(dprefix + "_list").format( |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
326 alist=pkk_get_fmt(dprefix + "_list_sep").join(tlist)) |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
327 else: |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
328 return pkk_get_fmt(dprefix + "_list_empty") |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
329 |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
330 |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
331 ## Output a main "Headword" or "Sense" node |
7 | 332 def pkk_output_sense(indent, dnode): |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
333 # Definition for this sense |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
334 pkk_output_subs(indent, dnode, "./Definition", "definition_fmt") |
7 | 335 |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
336 # Examples for this sense |
7 | 337 for wnode in dnode.findall("./ExampleBlock/ExampleCtn"): |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
338 geolist = [] |
24
8c8e8e4504bb
Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
339 for qnode in wnode.findall("./FreeTopic[@type='levikki']/GeographicalUsage"): |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
340 geolist.append(pkk_get_fmt("example_geo_list_item").format( |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
341 text=pkk_node_to_text(qnode), |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
342 tclass=qnode.attrib["class"])) |
7 | 343 |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
344 pkk_print(pkk_get_fmt("example_fmt").format( |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
345 text=pkk_node_to_text(wnode.find("./Example")), |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
346 geostr=pkk_get_list_str(geolist, "example_geo", False), |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
347 indent=pkk_geti(indent + 1))) |
7 | 348 |
349 | |
17 | 350 ## Output one "DictionaryEntry" node |
7 | 351 def pkk_output_node(indent, dnode): |
6 | 352 |
7 | 353 for wnode in dnode.findall("./HeadwordCtn"): |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
354 # Get head word |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
355 headword = pkk_node_to_text(wnode.find("./Headword")) |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
356 |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
357 # Collect search forms |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
358 srchlist = [] |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
359 for qnode in wnode.findall("./SearchForm"): |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
360 srchlist.append(pkk_node_to_text(qnode)) |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
361 |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
362 # Remove dupe if headword is also in srchlist |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
363 if headword in srchlist: |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
364 srchlist.remove(headword) |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
365 |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
366 # Remove other duplicates and sort |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
367 srchlist = list(set(srchlist)) |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
368 srchlist.sort(reverse=False, key=lambda attr: (attr, len(attr))) |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
369 |
18 | 370 # Create list with grammatical attributes (noun, verb, etc.) |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
371 attrlist = [] |
18 | 372 for pnode in wnode.findall("./PartOfSpeechCtn/PartOfSpeech"): |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
373 attrlist.append(pnode.attrib["freeValue"]) |
18 | 374 |
375 for pnode in wnode.findall("./GrammaticalNote"): | |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
376 attrlist.append(pkk_node_to_text(pnode)) |
18 | 377 |
378 # Remove duplicates and sort the list | |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
379 attrlist = list(set(attrlist)) |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
380 attrlist.sort(reverse=False, key=lambda attr: (attr, len(attr))) |
27
d77ab8a300b1
Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
381 |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
382 # Print the headword and attributes if any |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
383 pkk_print(pkk_get_fmt("word_fmt").format( |
43
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
384 word=headword, |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
385 attr=pkk_get_list_str(attrlist, "word_attr", True), |
8ed576574712
More improvements to output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
42
diff
changeset
|
386 search=pkk_get_list_str(srchlist, "search", True), |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
387 indent=pkk_geti(indent))) |
18 | 388 |
389 # Print main "sense" | |
7 | 390 pkk_output_sense(indent + 1, wnode) |
6 | 391 |
18 | 392 # Print any other "senses" |
7 | 393 index = 1 |
394 for wnode in dnode.findall("./SenseGrp"): | |
40
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
395 pkk_print(pkk_get_fmt("sense_index").format( |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
396 index=index, |
bc8d8ef4a248
More work on output flexibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
39
diff
changeset
|
397 indent=pkk_geti(indent + 1))) |
7 | 398 pkk_output_sense(indent + 2, wnode) |
399 index += 1 | |
6 | 400 |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
401 pkk_print(pkk_get_fmt("word_end")) |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
402 |
6 | 403 |
0 | 404 ### |
405 ### Main program starts | |
406 ### | |
407 signal.signal(signal.SIGINT, pkk_signal_handler) | |
408 | |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
409 optparser = argparse.ArgumentParser( |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
410 description="lxmldump - Convert and dump ISO/FDIS 1951 XML file data", |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
411 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
|
412 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
|
413 ) |
0 | 414 |
22 | 415 optparser.add_argument("filenames", |
416 type=str, action="extend", nargs="*", | |
417 metavar="filename", | |
418 help="XML filename(s)") | |
0 | 419 |
31
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
420 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
|
421 dest="show_help", |
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
422 action="store_true", |
39
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
423 help="show this help message") |
31
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
424 |
39
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
425 optparser.add_argument("-m", "--mode", |
22 | 426 dest="mode", |
39
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
427 action=pkk_set_mode, |
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
428 default=PKK_MODE_NORMAL, |
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
429 help="set output mode (see below)") |
35
5aafa87dbec2
Some Anki-related stuff, not finalized and thus hidden.
Matti Hamalainen <ccr@tnsp.org>
parents:
34
diff
changeset
|
430 |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
431 optparser.add_argument("-s", "--set", |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
432 action=pkk_set_value, |
38
0e586b4ab62c
Cosmetic adjustments to help.
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
433 metavar="ID=STR", |
0e586b4ab62c
Cosmetic adjustments to help.
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
434 help='set format string (see below)') |
32
cfecd039506e
Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
435 |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
436 optparser.add_argument("-n", "--normalize", |
22 | 437 dest="normalize", |
29
f91ef7d7615b
Use store_true feature of argparse.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
438 action="store_true", |
22 | 439 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
|
440 |
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
441 optparser.add_argument("-a", "--annotate", |
22 | 442 dest="annotate", |
29
f91ef7d7615b
Use store_true feature of argparse.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
443 action="store_true", |
22 | 444 help="annotate strings") |
0 | 445 |
22 | 446 optparser.add_argument("-i", "--indent", |
447 dest="indent", | |
448 type=int, choices=range(0, 32), default=4, | |
38
0e586b4ab62c
Cosmetic adjustments to help.
Matti Hamalainen <ccr@tnsp.org>
parents:
37
diff
changeset
|
449 metavar="N", |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
450 help='set indentation level (default: %(default)s)') |
0 | 451 |
35
5aafa87dbec2
Some Anki-related stuff, not finalized and thus hidden.
Matti Hamalainen <ccr@tnsp.org>
parents:
34
diff
changeset
|
452 optparser.add_argument("-p", "--debug", |
5aafa87dbec2
Some Anki-related stuff, not finalized and thus hidden.
Matti Hamalainen <ccr@tnsp.org>
parents:
34
diff
changeset
|
453 dest="debug", |
5aafa87dbec2
Some Anki-related stuff, not finalized and thus hidden.
Matti Hamalainen <ccr@tnsp.org>
parents:
34
diff
changeset
|
454 action="store_true", |
5aafa87dbec2
Some Anki-related stuff, not finalized and thus hidden.
Matti Hamalainen <ccr@tnsp.org>
parents:
34
diff
changeset
|
455 help=argparse.SUPPRESS) |
5aafa87dbec2
Some Anki-related stuff, not finalized and thus hidden.
Matti Hamalainen <ccr@tnsp.org>
parents:
34
diff
changeset
|
456 |
0 | 457 |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
458 ### Parse arguments |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
459 pkk_cfg = optparser.parse_args() |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
460 |
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
461 |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
462 ### Show help if needed |
31
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
463 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
|
464 optparser.print_help() |
39
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
465 print(u"\nAvailable output modes:") |
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
466 print(" " + ", ".join(pkk_modes_list.values())) |
31
4cbefe4c6f53
Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
467 |
41 | 468 if pkk_cfg.mode not in [PKK_MODE_NORMAL, PKK_MODE_ANKI]: |
469 pkk_cfg.mode = PKK_MODE_NORMAL | |
470 | |
471 print(u"\nAvailable format strings and values (mode '{}'):".format( | |
472 pkk_modes_list[pkk_cfg.mode])) | |
473 | |
39
f53ea742b57f
Refactor output mode handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
38
diff
changeset
|
474 for mid in pkk_mode_defaults: |
41 | 475 stmp = pkk_get_value(mid).replace("\\", "\\\\").replace("\n", "\\n") |
42 | 476 print(u" {:22s} : '{}'".format(mid, stmp)) |
41 | 477 |
0 | 478 sys.exit(0) |
479 | |
480 | |
6 | 481 ### 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
|
482 for filename in pkk_cfg.filenames: |
0 | 483 # Parse XML file into element tree |
484 try: | |
485 uxml = xmlET.parse(filename) | |
486 except Exception as e: | |
487 pkk_fatal(u"SVG/XML parsing failed: {0}".format(str(e))) | |
488 | |
489 # Dump output | |
490 try: | |
491 xroot = uxml.getroot() | |
492 for dnode in xroot.findall("./DictionaryEntry"): | |
7 | 493 |
20
f274504eafd0
Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
494 if pkk_cfg.debug and dnode.attrib["identifier"] not in pkk_debug_list: |
7 | 495 continue |
496 | |
35
5aafa87dbec2
Some Anki-related stuff, not finalized and thus hidden.
Matti Hamalainen <ccr@tnsp.org>
parents:
34
diff
changeset
|
497 if pkk_cfg.mode in [PKK_MODE_NORMAL, PKK_MODE_ANKI]: |
19 | 498 try: |
499 pkk_output_node(0, dnode) | |
500 except Exception as e: | |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
501 print("") |
19 | 502 pkk_dump_recursive(0, dnode) |
503 print(str(e)) | |
504 sys.exit(0) | |
21 | 505 elif pkk_cfg.mode == PKK_MODE_DUMP: |
7 | 506 pkk_dump_recursive(0, dnode) |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
507 print("") |
21 | 508 elif pkk_cfg.mode == PKK_MODE_XML: |
36
4c8aafff8c5f
Refactor output handling to be (mostly) configurable.
Matti Hamalainen <ccr@tnsp.org>
parents:
35
diff
changeset
|
509 pkk_print(str(xmlET.tostring(dnode, encoding="utf8")) + "\n\n") |
0 | 510 else: |
10 | 511 pkk_fatal("Invalid operation mode?") |
7 | 512 |
0 | 513 except (BrokenPipeError, IOError) as e: |
514 sys.stderr.close() | |
515 sys.exit(1) | |
516 | |
517 pkk_cleanup() | |
518 sys.exit(0) |