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