annotate lxmldump.py @ 24:8c8e8e4504bb

Remove verbosity option as it is not really used.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 25 May 2021 10:50:02 +0300
parents 3ef1c5463b8f
children 8a6738f67106
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 ###
4
Matti Hamalainen <ccr@tnsp.org>
parents: 1
diff changeset
4 ### lxmldump - Dump ISO/FDIS 1951 XML file data
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
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 assert sys.version_info >= (3, 7)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22
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 ### Default settings
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 ###
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
27 pkk_str_fmap = {
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
28 "Fragment" : ["<", ">"],
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
29 }
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
30
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
31
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
32 pkk_debug_list = [
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
33 "ahas",
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
34 "ahavakkaine",
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
35 "ahavakala",
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
36 "ahavakoittuo",
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
37 "ahvaliha",
9
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
38 "aloilleh",
18
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
39 "hanjahtoakseh",
19
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
40 "akkalisto",
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
41 ]
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
42
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
43
21
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
44 # Operation modes
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
45 PKK_MODE_NORMAL = 0
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
46 PKK_MODE_DUMP = 1
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
47 PKK_MODE_XML = 2
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
48
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
49
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 ###
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 ### Misc. helper functions, etc
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 ###
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 def pkk_cleanup():
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 return 0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
57 ## 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
58 def pkk_print(smsg):
12
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
59 try:
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
60 if pkk_cfg.normalize:
12
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
61 sys.stdout.write(unicodedata.normalize("NFC", smsg))
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
62 else:
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
63 sys.stdout.write(smsg)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
64
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
65 except (BrokenPipeError, IOError) as e:
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
66 sys.stderr.close()
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
67
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
69 ## Print string with indentation
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
70 def pkk_printi(indent, smsg):
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
71 pkk_print((" " * pkk_cfg.indent * indent) + smsg)
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
72
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 ## Fatal error handler
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 def pkk_fatal(smsg):
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 print(u"ERROR: "+ smsg)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 sys.exit(1)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 ## Handler for SIGINT signals
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 def pkk_signal_handler(signal, frame):
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 pkk_cleanup()
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 print(u"\nQuitting due to SIGINT / Ctrl+C!")
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 sys.exit(1)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
87 ## Clean string by removing tabs and newlines
10
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
88 def pkk_str_clean(mstr):
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
89 return re.sub(r'[\n\r\t]', '', mstr)
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
90
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
91
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
92 ## Format "Ptr" node as text
9
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
93 def pkk_ptr_to_text(pnode):
16
285b0820d2c6 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
94 return "<PTR:{}>{}</PTR>".format(
10
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
95 pnode.attrib["{http://www.w3.org/TR/xlink}href"],
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
96 ("".join(pnode.itertext())).strip())
9
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
97
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
98
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
99 ## Get text inside a given node
16
285b0820d2c6 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
100 def pkk_node_to_text(lnode):
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
101 stmp = ""
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
102 for pnode in lnode.iter():
9
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
103 if pnode.tag == "Ptr":
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
104 stmp += pkk_ptr_to_text(pnode)
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
105 else:
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
106 if isinstance(pnode.text, str):
10
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
107 ptext = pkk_str_clean(pnode.text).strip()
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
108 if pkk_cfg.annotate and isinstance(pnode.tag, str) and pnode.tag in pkk_str_fmap:
9
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
109 stmp += pkk_str_fmap[pnode.tag][0] + ptext + pkk_str_fmap[pnode.tag][1]
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
110 else:
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
111 stmp += ptext
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
112
9
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
113 if isinstance(pnode.tail, str):
10
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
114 stmp += pkk_str_clean(pnode.tail)
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
115
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
116 return stmp.strip()
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
117
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
118
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
119 ## Simple recursive dump starting at given node
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
120 def pkk_dump_recursive(indent, lnode):
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
121 if lnode.tag in ["Example"]:
16
285b0820d2c6 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
122 stmp = pkk_node_to_text(lnode)
9
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
123 pkk_printi(indent, "{} \"{}\"\n".format(lnode.tag, stmp))
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
124 else:
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
125 if isinstance(lnode.text, str):
10
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
126 stmp = pkk_str_clean(lnode.text).strip()
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
127 if stmp != "":
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
128 stmp = " \""+ stmp +"\""
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
129 else:
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
130 stmp = ""
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
131
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
132 if len(lnode.attrib) > 0:
10
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
133 atmp = " "+ str(lnode.attrib)
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
134 else:
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
135 atmp = ""
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
136
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
137 pkk_printi(indent, "{}{}{}\n".format(lnode.tag, atmp, stmp))
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
138 for qnode in lnode.findall("./*"):
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
139 pkk_dump_recursive(indent + 1, qnode)
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
140
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
141
17
6fa24c711f86 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
142 ## Output item(s) under given node with given format string
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
143 def pkk_output_subs_fmt(indent, dnode, dsub, dname, dfmt):
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
144 for qnode in dnode.findall(dsub):
16
285b0820d2c6 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
145 pkk_printi(indent, dfmt.format(dname, pkk_node_to_text(qnode)))
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
146
17
6fa24c711f86 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
147 ## Output item(s) under given node with a prefixed name string
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
148 def pkk_output_subs_prefix(indent, dnode, dsub, dname):
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
149 pkk_output_subs_fmt(indent, dnode, dsub, dname, "{0} \"{1}\"\n")
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
150
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
151
17
6fa24c711f86 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
152 ## Output a main "Headword" or "Sense" node under it
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
153 def pkk_output_sense(indent, dnode):
17
6fa24c711f86 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
154 # Search form and definition
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
155 pkk_output_subs_prefix(indent, dnode, "./SearchForm", "srch")
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
156 pkk_output_subs_prefix(indent, dnode, "./Definition", "defn")
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
157
17
6fa24c711f86 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
158 # Examples
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
159 for wnode in dnode.findall("./ExampleBlock/ExampleCtn"):
16
285b0820d2c6 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
160 sstr = pkk_node_to_text(wnode.find("./Example"))
8
ce07bb2a247b More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
161 lstr = ""
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
162
24
8c8e8e4504bb Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
163 ltmp = []
8c8e8e4504bb Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
164 for qnode in wnode.findall("./FreeTopic[@type='levikki']/GeographicalUsage"):
8c8e8e4504bb Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
165 ltmp.append("{} [{}]".format(pkk_node_to_text(qnode), qnode.attrib["class"]))
8
ce07bb2a247b More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
166
24
8c8e8e4504bb Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
167 if len(ltmp) > 0:
8c8e8e4504bb Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
168 lstr = " ({})".format(", ".join(ltmp))
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
169
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
170 pkk_printi(indent + 1, "{} \"{}\"{}\n".format("exmp", sstr, lstr))
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
171
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
172
17
6fa24c711f86 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
173 ## Output one "DictionaryEntry" node
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
174 def pkk_output_node(indent, dnode):
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
175
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
176 for wnode in dnode.findall("./HeadwordCtn"):
18
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
177 # Create list with grammatical attributes (noun, verb, etc.)
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
178 tmpl = []
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
179 for pnode in wnode.findall("./PartOfSpeechCtn/PartOfSpeech"):
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
180 tmpl.append(pnode.attrib["freeValue"])
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
181
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
182 for pnode in wnode.findall("./GrammaticalNote"):
19
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
183 tmpl.append(pkk_node_to_text(pnode))
18
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
184
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
185 # Remove duplicates and sort the list
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
186 tmpl = list(set(tmpl))
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
187 tmpl.sort(reverse=False, key=lambda attr: (attr, len(attr)))
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
188
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
189 # Print the headword and attributes if any
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
190 pkk_output_subs_fmt(indent, wnode, "./Headword", "", "\"{1}\"")
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
191 if len(tmpl) > 0:
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
192 pkk_print(" ({})\n".format(" ; ".join(tmpl)))
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
193 else:
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
194 pkk_print("\n")
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
195
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
196 # Print main "sense"
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
197 pkk_output_sense(indent + 1, wnode)
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
198
18
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
199 # Print any other "senses"
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
200 index = 1
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
201 for wnode in dnode.findall("./SenseGrp"):
8
ce07bb2a247b More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
202 pkk_printi(indent + 1, "sense #{}\n".format(index))
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
203 pkk_output_sense(indent + 2, wnode)
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
204 index += 1
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
205
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
206
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 ###
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 ### Main program starts
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 ###
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 signal.signal(signal.SIGINT, pkk_signal_handler)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
212 optparser = argparse.ArgumentParser(
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
213 description="lxmldump - Dump ISO/FDIS 1951 XML file data",
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
214 usage="%(prog)s [options] <input xml file(s)>",
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
215 epilog="\n\n"
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
216 )
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
218 optparser.add_argument("filenames",
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
219 type=str, action="extend", nargs="*",
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
220 metavar="filename",
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
221 help="XML filename(s)")
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
223 optparser.add_argument("-d", "--dump",
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
224 dest="mode",
21
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
225 action="store_const", const=PKK_MODE_DUMP, default=PKK_MODE_NORMAL,
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
226 help="output as simple dump")
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
227
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
228 optparser.add_argument("-x", "--xml",
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
229 dest="mode",
21
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
230 action="store_const", const=PKK_MODE_XML,
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
231 help="output as XML")
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
233 optparser.add_argument("-n", "--normalize",
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
234 dest="normalize",
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
235 action="store_const", const=True, default=False,
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
236 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
237
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
238 optparser.add_argument("-a", "--annotate",
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
239 dest="annotate",
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
240 action="store_const", const=True, default=False,
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
241 help="annotate strings")
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
243 optparser.add_argument("-p", "--debug",
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
244 dest="debug", action="store_const", const=True, default=False,
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
245 help=argparse.SUPPRESS)
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
246
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
247 optparser.add_argument("-i", "--indent",
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
248 dest="indent",
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
249 type=int, choices=range(0, 32), default=4,
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
250 metavar="n",
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
251 help='indent output by <n> characters (default: %(default)s)')
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
254 ### Show help if needed
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
255 pkk_cfg = optparser.parse_args()
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
256 if len(pkk_cfg.filenames) == 0:
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
257 optparser.print_help()
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 sys.exit(0)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
261 ### 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
262 for filename in pkk_cfg.filenames:
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 # Parse XML file into element tree
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 try:
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 uxml = xmlET.parse(filename)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 except Exception as e:
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 pkk_fatal(u"SVG/XML parsing failed: {0}".format(str(e)))
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 # Dump output
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270 try:
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 xroot = uxml.getroot()
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272 for dnode in xroot.findall("./DictionaryEntry"):
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
273
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
274 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
275 continue
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
276
21
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
277 if pkk_cfg.mode == PKK_MODE_NORMAL:
19
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
278 try:
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
279 pkk_output_node(0, dnode)
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
280 except Exception as e:
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
281 pkk_dump_recursive(0, dnode)
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
282 print(str(e))
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
283 sys.exit(0)
21
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
284 elif pkk_cfg.mode == PKK_MODE_DUMP:
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
285 pkk_dump_recursive(0, dnode)
21
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
286 elif pkk_cfg.mode == PKK_MODE_XML:
10
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
287 pkk_print(str(xmlET.tostring(dnode, encoding="utf8")) + "\n")
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 else:
10
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
289 pkk_fatal("Invalid operation mode?")
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
290
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
291 print("\n")
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 except (BrokenPipeError, IOError) as e:
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 sys.stderr.close()
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 sys.exit(1)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297 pkk_cleanup()
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298 sys.exit(0)