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