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