annotate lxmldump.py @ 42:508de0f6836b

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