Mercurial > hg > lxmldump
annotate lxmldump.py @ 6:34a89d61dbe7
Merge and cleanup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 10 May 2021 21:43:00 +0300 |
parents | 274b2091137c 7ce08dea935b |
children | 4b4299b62f7f |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python3 -B |
2 # coding=utf-8 | |
3 ### | |
4 | 4 ### lxmldump - Dump ISO/FDIS 1951 XML file data |
5 ### Programmed and designed by Matti 'ccr' Hämäläinen <ccr@tnsp.org> | |
6 ### (C) Copyright 2021 Tecnic Software productions (TNSP) | |
7 ### | |
8 ### Python 3.7+ required! | |
0 | 9 ### |
10 import sys | |
11 import signal | |
12 import re | |
13 from pathlib import Path | |
14 import xml.etree.ElementTree as xmlET | |
5
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
15 import unicodedata |
0 | 16 |
17 assert sys.version_info >= (3, 7) | |
18 | |
19 | |
20 ### | |
21 ### Default settings | |
22 ### | |
23 pkk_cfg = { | |
24 "dump": False, | |
5
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
25 "normalize": False, |
0 | 26 } |
27 | |
28 | |
29 ### | |
30 ### Misc. helper functions, etc | |
31 ### | |
32 def pkk_cleanup(): | |
33 return 0 | |
34 | |
35 | |
36 ## Wrapper for print() | |
5
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
37 def pkk_print(smsg): |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
38 if pkk_cfg["normalize"]: |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
39 sys.stdout.write(unicodedata.normalize("NFC", smsg)) |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
40 else: |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
41 sys.stdout.write(smsg) |
0 | 42 |
43 | |
44 ## Fatal error handler | |
45 def pkk_fatal(smsg): | |
46 print(u"ERROR: "+ smsg) | |
47 sys.exit(1) | |
48 | |
49 | |
50 ## Handler for SIGINT signals | |
51 def pkk_signal_handler(signal, frame): | |
52 pkk_cleanup() | |
53 print(u"\nQuitting due to SIGINT / Ctrl+C!") | |
54 sys.exit(1) | |
55 | |
56 | |
6 | 57 ## |
58 def pkk_dump_recursive(lnode, indent): | |
59 if lnode.tag == "Example": | |
60 stmp = "".join(lnode.itertext()).strip() | |
61 print("{}{} \"{}\"".format(" " * indent, lnode.tag, stmp)) | |
62 else: | |
63 stmp = "" | |
64 if lnode.text != None: | |
65 tmp = str(lnode.text).strip() | |
66 if tmp != "": | |
67 stmp = " \""+ tmp +"\"" | |
68 | |
69 if len(lnode.attrib) > 0: | |
70 atmp = " "+str(lnode.attrib) | |
71 else: | |
72 atmp = "" | |
73 | |
74 pkk_print("{}{}{}{}\n".format(" " * indent, lnode.tag, atmp, stmp)) | |
75 for qnode in lnode.findall("./*"): | |
76 pkk_dump_recursive(qnode, indent + 1) | |
77 | |
78 | |
79 ## | |
80 def pkk_output_node(dnode): | |
81 wlist = [] | |
82 dlist = [] | |
83 for wnode in dnode.findall("./HeadwordCtn"): | |
84 for qnode in wnode.findall("./SearchForm"): | |
85 wlist.append(str(qnode.text).strip()) | |
86 | |
87 for qnode in wnode.findall("./Definition"): | |
88 dlist.append(str(qnode.text).strip()) | |
89 | |
90 for wnode in dnode.findall("./SenseGrp"): | |
91 for qnode in wnode.findall("./Definition"): | |
92 dlist.append(str(qnode.text).strip()) | |
93 | |
94 pkk_print("{} : {}\n".format(", ".join(wlist), " ; ".join(dlist))) | |
95 | |
96 | |
0 | 97 ### |
98 ### Main program starts | |
99 ### | |
100 signal.signal(signal.SIGINT, pkk_signal_handler) | |
101 | |
102 | |
103 ### Check if we have arguments | |
104 pkk_show_help = False | |
105 pkk_filenames = [] | |
106 argc = 1 | |
107 while argc < len(sys.argv): | |
108 arg = sys.argv[argc] | |
109 | |
110 needs_param = False | |
111 if argc + 1 < len(sys.argv): | |
112 param = sys.argv[argc + 1] | |
113 else: | |
114 param = None | |
115 | |
116 # Check for option type arg | |
117 if arg[0:1] == "-": | |
118 oarg = arg | |
119 arg = arg.lstrip("-") | |
120 | |
121 if arg == "help" or arg == "h": | |
122 pkk_show_help = True | |
123 elif arg == "dump" or arg == "d": | |
124 pkk_cfg["dump"] = True | |
5
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
125 elif arg == "normalize" or arg == "n": |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
126 pkk_cfg["normalize"] = True |
0 | 127 else: |
128 pkk_fatal(u"Invalid option argument '{0}'.".format(oarg)) | |
129 | |
130 if needs_param and param == None: | |
131 pkk_fatal(u"Option '{0}' requires an argument.".format(oarg)) | |
132 else: | |
133 # Non-option argument | |
134 pkk_filenames.append(arg) | |
135 | |
136 if needs_param: | |
137 argc += 2 | |
138 else: | |
139 argc += 1 | |
140 | |
141 | |
142 ### Show help if requested | |
143 if pkk_show_help or len(pkk_filenames) == 0: | |
144 print(u"lxmldump - Dump ISO/FDIS 1951 XML file data") | |
145 print(u"Usage: {0} <options> <input xml file(s)>". | |
146 format(str(Path(sys.argv[0]).name))) | |
147 print(u"") | |
148 print(u" --help Show this help") | |
149 print(u" -d, --dump Dump mode") | |
6 | 150 print(u" -n, --normalize Output NFC normalized Unicode") |
0 | 151 print(u"") |
152 sys.exit(0) | |
153 | |
154 | |
6 | 155 ### Handle each input file |
0 | 156 for filename in pkk_filenames: |
157 # Parse XML file into element tree | |
158 try: | |
159 uxml = xmlET.parse(filename) | |
160 except Exception as e: | |
161 pkk_fatal(u"SVG/XML parsing failed: {0}".format(str(e))) | |
162 | |
163 # Dump output | |
164 try: | |
165 xroot = uxml.getroot() | |
166 for dnode in xroot.findall("./DictionaryEntry"): | |
167 if pkk_cfg["dump"]: | |
6 | 168 pkk_dump_recursive(dnode, 0) |
0 | 169 print("\n\n") |
170 else: | |
6 | 171 pkk_output_node(dnode) |
0 | 172 |
173 except (BrokenPipeError, IOError) as e: | |
174 sys.stderr.close() | |
175 sys.exit(1) | |
176 | |
177 pkk_cleanup() | |
178 sys.exit(0) |