Mercurial > hg > lxmldump
annotate lxmldump.py @ 5:274b2091137c
Some more work on cleaning this up.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 10 May 2021 21:38:11 +0300 |
parents | 60b789dfee32 |
children | 34a89d61dbe7 |
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 | |
57 ### | |
58 ### Main program starts | |
59 ### | |
60 signal.signal(signal.SIGINT, pkk_signal_handler) | |
61 | |
62 | |
63 ### Check if we have arguments | |
64 pkk_show_help = False | |
65 pkk_filenames = [] | |
66 argc = 1 | |
67 while argc < len(sys.argv): | |
68 arg = sys.argv[argc] | |
69 | |
70 needs_param = False | |
71 if argc + 1 < len(sys.argv): | |
72 param = sys.argv[argc + 1] | |
73 else: | |
74 param = None | |
75 | |
76 # Check for option type arg | |
77 if arg[0:1] == "-": | |
78 oarg = arg | |
79 arg = arg.lstrip("-") | |
80 | |
81 if arg == "help" or arg == "h": | |
82 pkk_show_help = True | |
83 elif arg == "dump" or arg == "d": | |
84 pkk_cfg["dump"] = True | |
5
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
85 elif arg == "normalize" or arg == "n": |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
86 pkk_cfg["normalize"] = True |
0 | 87 else: |
88 pkk_fatal(u"Invalid option argument '{0}'.".format(oarg)) | |
89 | |
90 if needs_param and param == None: | |
91 pkk_fatal(u"Option '{0}' requires an argument.".format(oarg)) | |
92 else: | |
93 # Non-option argument | |
94 pkk_filenames.append(arg) | |
95 | |
96 if needs_param: | |
97 argc += 2 | |
98 else: | |
99 argc += 1 | |
100 | |
101 | |
102 ### Show help if requested | |
103 if pkk_show_help or len(pkk_filenames) == 0: | |
104 print(u"lxmldump - Dump ISO/FDIS 1951 XML file data") | |
105 print(u"Usage: {0} <options> <input xml file(s)>". | |
106 format(str(Path(sys.argv[0]).name))) | |
107 print(u"") | |
108 print(u" --help Show this help") | |
109 print(u" -d, --dump Dump mode") | |
110 print(u"") | |
111 sys.exit(0) | |
112 | |
113 | |
114 ### | |
115 ### Main | |
116 ### | |
5
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
117 def pkk_dump_simple_node(lnode, indent): |
1 | 118 stmp = "" |
0 | 119 if lnode.text != None: |
120 tmp = str(lnode.text).strip() | |
121 if tmp != "": | |
1 | 122 stmp = " \""+ tmp +"\"" |
0 | 123 |
5
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
124 pkk_print("{}{} {}{}".format(" " * indent, lnode.tag, lnode.attrib, stmp)) |
0 | 125 for qnode in lnode.findall("./*"): |
5
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
126 pkk_dump_simple_node(qnode, indent + 1) |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
127 |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
128 |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
129 def pkk_dump_node(dnode): |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
130 wlist = [] |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
131 dlist = [] |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
132 for wnode in dnode.findall("./HeadwordCtn"): |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
133 for qnode in wnode.findall("./SearchForm"): |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
134 wlist.append(str(qnode.text).strip()) |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
135 |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
136 for qnode in wnode.findall("./Definition"): |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
137 dlist.append(str(qnode.text).strip()) |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
138 |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
139 for wnode in dnode.findall("./SenseGrp"): |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
140 for qnode in wnode.findall("./Definition"): |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
141 dlist.append(str(qnode.text).strip()) |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
142 |
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
143 pkk_print("{} : {}".format(", ".join(wlist), " ; ".join(dlist))) |
0 | 144 |
145 | |
146 for filename in pkk_filenames: | |
147 # Parse XML file into element tree | |
148 try: | |
149 uxml = xmlET.parse(filename) | |
150 except Exception as e: | |
151 pkk_fatal(u"SVG/XML parsing failed: {0}".format(str(e))) | |
152 | |
153 # Dump output | |
154 try: | |
155 xroot = uxml.getroot() | |
156 for dnode in xroot.findall("./DictionaryEntry"): | |
157 if pkk_cfg["dump"]: | |
5
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
158 pkk_dump_simple_node(dnode, 0) |
0 | 159 print("\n\n") |
160 else: | |
5
274b2091137c
Some more work on cleaning this up.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
161 pkk_dump_node(dnode) |
0 | 162 |
163 except (BrokenPipeError, IOError) as e: | |
164 sys.stderr.close() | |
165 sys.exit(1) | |
166 | |
167 pkk_cleanup() | |
168 sys.exit(0) |