annotate lxmldump.py @ 34:73f2f98e3eef

Cosmetic.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 25 May 2021 13:04:30 +0300
parents 3dcf8ac43bda
children 5aafa87dbec2
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 ###
25
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
27 # Operation modes
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
28 PKK_MODE_NORMAL = 0
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
29 PKK_MODE_DUMP = 1
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
30 PKK_MODE_XML = 2
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
31
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
32
30
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
33 pkk_modes_list = {
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
34 PKK_MODE_NORMAL: "normal",
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
35 PKK_MODE_DUMP: "dump",
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
36 PKK_MODE_XML: "xml",
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
37 }
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
38
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
39
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
40 # Default Ptr URL format strings
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
41 pkk_ptr_url_fmt = {
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
42 PKK_MODE_NORMAL: u"<PTR:{href}>{text}</PTR>",
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
43 }
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
44
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
45
25
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
46 # Element annotation mappings
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
47 pkk_element_annotation_map = {
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
48 "Fragment" : {
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
49 PKK_MODE_NORMAL: ["<", ">"],
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
50 },
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
51 }
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
52
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
53
28
3442b8700da7 Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
54 # List of words in kks1/ useful for debugging, option -p
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
55 pkk_debug_list = [
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
56 "ahas",
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
57 "ahavakkaine",
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
58 "ahavakala",
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
59 "ahavakoittuo",
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
60 "ahvaliha",
9
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
61 "aloilleh",
18
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
62 "hanjahtoakseh",
19
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
63 "akkalisto",
33
3dcf8ac43bda Add one word to debug list.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
64 "alto-",
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
65 ]
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
66
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
67
21
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
68
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 ###
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 ### Misc. helper functions, etc
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 ###
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 def pkk_cleanup():
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 return 0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
76 ## 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
77 def pkk_print(smsg):
12
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
78 try:
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
79 if pkk_cfg.normalize:
12
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
80 sys.stdout.write(unicodedata.normalize("NFC", smsg))
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
81 else:
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
82 sys.stdout.write(smsg)
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
83
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
84 except (BrokenPipeError, IOError) as e:
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
85 sys.stderr.close()
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
86
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
88 ## Print string with indentation
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
89 def pkk_printi(indent, smsg):
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
90 pkk_print((" " * pkk_cfg.indent * indent) + smsg)
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
91
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 ## Fatal error handler
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 def pkk_fatal(smsg):
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 print(u"ERROR: "+ smsg)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 sys.exit(1)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 ## Handler for SIGINT signals
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 def pkk_signal_handler(signal, frame):
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 pkk_cleanup()
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 print(u"\nQuitting due to SIGINT / Ctrl+C!")
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 sys.exit(1)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105
28
3442b8700da7 Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
106 ## Annotate given string with prefix and suffix based on tag
26
420f13925f20 Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
107 def pkk_str_annotate(mtag, mstr):
25
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
108 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
109 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
110 mmode = pkk_cfg.mode
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
111 else:
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
112 mmode = PKK_MODE_NORMAL
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
113
26
420f13925f20 Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
114 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
115 else:
26
420f13925f20 Rename a variable.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
116 return mstr
25
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
117
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
118
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
119 ## Clean string by removing tabs and newlines
10
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
120 def pkk_str_clean(mstr):
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
121 return re.sub(r'[\n\r\t]', '', mstr)
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
122
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
123
28
3442b8700da7 Comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
124 ## Format a "Ptr" node as text
9
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
125 def pkk_ptr_to_text(pnode):
30
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
126 # If custom format set, use it
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
127 if pkk_cfg.ptr_url_fmt != None:
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
128 pfmt = pkk_cfg.ptr_url_fmt
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
129 elif pkk_cfg.mode in pkk_ptr_url_fmt:
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
130 # Else try mode-specific
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
131 pfmt = pkk_ptr_url_fmt[pkk_cfg.mode]
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
132 else:
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
133 # Last resort is normal mode format
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
134 pfmt = pkk_ptr_url_fmt[PKK_MODE_NORMAL]
27
d77ab8a300b1 Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
135
30
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
136 return pfmt.format(
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
137 text=("".join(pnode.itertext())).strip(),
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
138 href=pnode.attrib["{http://www.w3.org/TR/xlink}href"])
9
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
139
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
140
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
141 ## Get text inside a given node
16
285b0820d2c6 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
142 def pkk_node_to_text(lnode):
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
143 stmp = ""
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
144 for pnode in lnode.iter():
9
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
145 if pnode.tag == "Ptr":
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
146 stmp += pkk_ptr_to_text(pnode)
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
147 else:
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
148 if isinstance(pnode.text, str):
25
8a6738f67106 Factor string annotation into separate function.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
149 stmp += pkk_str_annotate(pnode.tag, pkk_str_clean(pnode.text).strip())
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
150
9
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
151 if isinstance(pnode.tail, str):
10
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
152 stmp += pkk_str_clean(pnode.tail)
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
153
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
154 return stmp.strip()
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
155
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
156
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
157 ## Simple recursive dump starting at given node
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
158 def pkk_dump_recursive(indent, lnode):
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
159 if lnode.tag in ["Example"]:
16
285b0820d2c6 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
160 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
161 pkk_printi(indent, f"{lnode.tag} \"{stmp}\"\n")
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
162 else:
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
163 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
164 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
165 if textstr != "":
d77ab8a300b1 Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
166 textstr = " \""+ textstr +"\""
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
167 else:
27
d77ab8a300b1 Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
168 textstr = ""
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
169
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
170 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
171 attrstr = " "+ str(lnode.attrib)
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
172 else:
27
d77ab8a300b1 Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
173 attrstr = ""
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
174
27
d77ab8a300b1 Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
175 pkk_printi(indent, f"{lnode.tag}{attrstr}{textstr}\n")
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
176 for qnode in lnode.findall("./*"):
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
177 pkk_dump_recursive(indent + 1, qnode)
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
178
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
179
17
6fa24c711f86 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
180 ## Output item(s) under given node with given format string
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
181 def pkk_output_subs_fmt(indent, dnode, dsub, dname, dfmt):
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
182 for qnode in dnode.findall(dsub):
27
d77ab8a300b1 Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
183 pkk_printi(indent, dfmt.format(nname=dname, ntext=pkk_node_to_text(qnode)))
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
184
17
6fa24c711f86 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
185 ## Output item(s) under given node with a prefixed name string
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
186 def pkk_output_subs_prefix(indent, dnode, dsub, dname):
27
d77ab8a300b1 Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
187 pkk_output_subs_fmt(indent, dnode, dsub, dname, "{nname} \"{ntext}\"\n")
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
188
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
189
17
6fa24c711f86 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
190 ## Output a main "Headword" or "Sense" node under it
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
191 def pkk_output_sense(indent, dnode):
17
6fa24c711f86 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
192 # Search form and definition
13
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
193 pkk_output_subs_prefix(indent, dnode, "./SearchForm", "srch")
3bd772fd6a50 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
194 pkk_output_subs_prefix(indent, dnode, "./Definition", "defn")
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
195
17
6fa24c711f86 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
196 # Examples
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
197 for wnode in dnode.findall("./ExampleBlock/ExampleCtn"):
16
285b0820d2c6 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
198 sstr = pkk_node_to_text(wnode.find("./Example"))
8
ce07bb2a247b More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
199 lstr = ""
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
200
24
8c8e8e4504bb Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
201 ltmp = []
8c8e8e4504bb Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
202 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
203 ltmp.append("{} [{}]".format(pkk_node_to_text(qnode), qnode.attrib["class"]))
8
ce07bb2a247b More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
204
24
8c8e8e4504bb Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
205 if len(ltmp) > 0:
8c8e8e4504bb Remove verbosity option as it is not really used.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
206 lstr = " ({})".format(", ".join(ltmp))
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
207
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
208 pkk_printi(indent + 1, "{} \"{}\"{}\n".format("exmp", sstr, lstr))
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
209
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
210
17
6fa24c711f86 Add some comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
211 ## Output one "DictionaryEntry" node
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
212 def pkk_output_node(indent, dnode):
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
213
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
214 for wnode in dnode.findall("./HeadwordCtn"):
18
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
215 # Create list with grammatical attributes (noun, verb, etc.)
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
216 tmpl = []
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
217 for pnode in wnode.findall("./PartOfSpeechCtn/PartOfSpeech"):
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
218 tmpl.append(pnode.attrib["freeValue"])
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
219
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
220 for pnode in wnode.findall("./GrammaticalNote"):
19
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
221 tmpl.append(pkk_node_to_text(pnode))
18
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
222
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
223 # Remove duplicates and sort the list
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
224 tmpl = list(set(tmpl))
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
225 tmpl.sort(reverse=False, key=lambda attr: (attr, len(attr)))
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
226
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
227 # Print the headword and attributes if any
27
d77ab8a300b1 Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
228 pkk_output_subs_fmt(indent, wnode, "./Headword", "", "\"{ntext}\"")
d77ab8a300b1 Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
229
18
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
230 if len(tmpl) > 0:
32
cfecd039506e Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
231 pkk_print(" ({nlist})".format(nlist=pkk_cfg.word_attr_sep.join(tmpl)))
27
d77ab8a300b1 Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
232
d77ab8a300b1 Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
233 pkk_print("\n")
18
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
234
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
235 # Print main "sense"
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
236 pkk_output_sense(indent + 1, wnode)
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
237
18
ff959de0f6c8 Add grammatical attributes.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
238 # Print any other "senses"
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
239 index = 1
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
240 for wnode in dnode.findall("./SenseGrp"):
27
d77ab8a300b1 Use Python 3.6/3.7 format strings where we can.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
241 pkk_printi(indent + 1, f"sense #{index}\n")
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
242 pkk_output_sense(indent + 2, wnode)
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
243 index += 1
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
244
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
245
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 ###
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 ### Main program starts
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 ###
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 signal.signal(signal.SIGINT, pkk_signal_handler)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
251 optparser = argparse.ArgumentParser(
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
252 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
253 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
254 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
255 )
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
257 optparser.add_argument("filenames",
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
258 type=str, action="extend", nargs="*",
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
259 metavar="filename",
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
260 help="XML filename(s)")
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261
31
4cbefe4c6f53 Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
262 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
263 dest="show_help",
4cbefe4c6f53 Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
264 action="store_true",
4cbefe4c6f53 Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
265 help="show this help message and exit")
4cbefe4c6f53 Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
266
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
267 optparser.add_argument("-d", "--dump",
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
268 dest="mode",
21
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
269 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
270 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
271
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
272 optparser.add_argument("-x", "--xml",
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
273 dest="mode",
21
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
274 action="store_const", const=PKK_MODE_XML,
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
275 help="output as XML")
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276
30
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
277 optparser.add_argument("--ptr-url-fmt",
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
278 dest="ptr_url_fmt",
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
279 type=str,
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
280 default=None,
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
281 metavar="str",
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
282 help='Ptr URL format string (see below)')
34755af2ea1f Make Ptr field URL formatting configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
283
32
cfecd039506e Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
284 optparser.add_argument("--attr-sep",
cfecd039506e Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
285 dest="word_attr_sep",
cfecd039506e Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
286 type=str, default=" ; ",
cfecd039506e Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
287 metavar="str",
cfecd039506e Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
288 help='word attribute separator (default: \"%(default)s\")')
cfecd039506e Make word attribute separator string configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
289
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
290 optparser.add_argument("-n", "--normalize",
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
291 dest="normalize",
29
f91ef7d7615b Use store_true feature of argparse.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
292 action="store_true",
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
293 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
294
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
295 optparser.add_argument("-a", "--annotate",
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
296 dest="annotate",
29
f91ef7d7615b Use store_true feature of argparse.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
297 action="store_true",
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
298 help="annotate strings")
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
300 optparser.add_argument("-p", "--debug",
29
f91ef7d7615b Use store_true feature of argparse.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
301 dest="debug",
f91ef7d7615b Use store_true feature of argparse.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
302 action="store_true",
22
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
303 help=argparse.SUPPRESS)
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
304
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
305 optparser.add_argument("-i", "--indent",
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
306 dest="indent",
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
307 type=int, choices=range(0, 32), default=4,
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
308 metavar="n",
76856fc4e5fa Clean up argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
309 help='indent output by <n> characters (default: %(default)s)')
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
312 ### 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
313 pkk_cfg = optparser.parse_args()
31
4cbefe4c6f53 Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
314 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
315 optparser.print_help()
31
4cbefe4c6f53 Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
316
4cbefe4c6f53 Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
317 print(u"\nDefault Ptr format strings per mode:")
4cbefe4c6f53 Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
318 for pmode in pkk_modes_list:
4cbefe4c6f53 Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
319 if pmode in pkk_ptr_url_fmt:
4cbefe4c6f53 Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
320 print(u" {:6s} : \"{}\"".format(pkk_modes_list[pmode], pkk_ptr_url_fmt[pmode]))
34
73f2f98e3eef Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
321
31
4cbefe4c6f53 Improve help, list the default Ptr format strings for each mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
322 print(u"")
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 sys.exit(0)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325
6
34a89d61dbe7 Merge and cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 5 3
diff changeset
326 ### 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
327 for filename in pkk_cfg.filenames:
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 # Parse XML file into element tree
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 try:
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 uxml = xmlET.parse(filename)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 except Exception as e:
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 pkk_fatal(u"SVG/XML parsing failed: {0}".format(str(e)))
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 # Dump output
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 try:
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336 xroot = uxml.getroot()
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 for dnode in xroot.findall("./DictionaryEntry"):
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
338
20
f274504eafd0 Use Python argparse module instead of custom self-rolled argument parser.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
339 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
340 continue
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
341
21
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
342 if pkk_cfg.mode == PKK_MODE_NORMAL:
19
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
343 try:
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
344 pkk_output_node(0, dnode)
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
345 except Exception as e:
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
346 pkk_dump_recursive(0, dnode)
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
347 print(str(e))
7c6eb57798bd Ja niin.
Matti Hamalainen <ccr@tnsp.org>
parents: 18
diff changeset
348 sys.exit(0)
21
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
349 elif pkk_cfg.mode == PKK_MODE_DUMP:
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
350 pkk_dump_recursive(0, dnode)
21
7ef08e05a5bf Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
351 elif pkk_cfg.mode == PKK_MODE_XML:
10
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
352 pkk_print(str(xmlET.tostring(dnode, encoding="utf8")) + "\n")
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 else:
10
013f0cd9e5b3 More work.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
354 pkk_fatal("Invalid operation mode?")
7
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
355
4b4299b62f7f Moar work.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
356 print("\n")
0
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 except (BrokenPipeError, IOError) as e:
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 sys.stderr.close()
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 sys.exit(1)
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 pkk_cleanup()
bddf1c283e51 Initial import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 sys.exit(0)