author | Volker Birk <vb@pep-project.org> |
Sat, 12 Oct 2019 15:56:01 +0200 | |
changeset 37 | a5e211c3fae3 |
parent 35 | 5414b157613a |
child 40 | 432ab62b2537 |
permissions | -rwxr-xr-x |
31 | 1 |
#!/usr/bin/env python3 |
0 | 2 |
# vim: set fileencoding=utf-8 : |
3 |
||
4 |
"""\ |
|
35
5414b157613a
bug: textsectionu disappeared
Volker Birk <vb@pep-project.org>
parents:
32
diff
changeset
|
5 |
YML/YSLT 2 processor version 6.1 |
31 | 6 |
Copyleft (c), 2009-2019 Volker Birk http://fdik.org/yml/ |
0 | 7 |
|
8 |
""" |
|
9 |
||
10 |
import sys, os, codecs, locale |
|
11 |
import fileinput, unicodedata |
|
12 |
from optparse import OptionParser |
|
13 |
||
14 |
try: |
|
15 |
from lxml import etree |
|
16 |
except: |
|
17 |
sys.stderr.write("This program needs lxml, see http://codespeak.net/lxml/\n") |
|
18 |
sys.exit(1) |
|
19 |
||
20 |
from yml2 import ymlCStyle, comment, oldSyntax |
|
21 |
from pyPEG import parse, u |
|
22 |
import backend |
|
23 |
||
24 |
def printInfo(option, opt_str, value, parser): |
|
25 |
sys.stdout.write(__doc__) |
|
26 |
sys.exit(0) |
|
27 |
||
28 |
class YMLAssert(Exception): pass |
|
29 |
||
30 |
def w(msg): |
|
31 |
if isinstance(msg, BaseException): |
|
32 |
try: |
|
33 |
msg = str(msg) + "\n" |
|
34 |
except: |
|
32 | 35 |
msg = u(msg) + "\n" |
0 | 36 |
sys.stderr.write(msg) |
37 |
||
38 |
optParser = OptionParser() |
|
39 |
optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax", |
|
40 |
help="syntax of YML 2 version 1.x (compatibility mode)", default=False) |
|
41 |
optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers", |
|
42 |
help="emit line numbers into the resulting XML for debugging purposes", default=False) |
|
43 |
optParser.add_option("--debug", action="store_true", dest="trace", |
|
44 |
help="switch on tracing to stderr", default=False) |
|
45 |
optParser.add_option("-d", "--paramdict", dest="params", metavar="PARAMS", |
|
46 |
help="call X/YSLT script with dictionary PARAMS as parameters") |
|
47 |
optParser.add_option("-e", "--xpath", dest="xpath", metavar="XPATH", |
|
48 |
help="execute XPath expression XPATH and print result") |
|
49 |
optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getdefaultlocale()[1], |
|
50 |
help="encoding of input files (default to locale)") |
|
51 |
optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH", |
|
52 |
help="precede YML_PATH by a colon separated INCLUDE_PATH to search for include files") |
|
53 |
optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm", |
|
54 |
help="does nothing (only there for compatibility reasons)", default=False) |
|
55 |
optParser.add_option("-M", "--empty-input-document", action="store_true", dest="emptyinput", |
|
56 |
help="use an empty input document", default=False) |
|
57 |
optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC", |
|
58 |
help="Unicode normalization (none, NFD, NFKD, NFC, NFKC, FCD, default is NFC)") |
|
59 |
optParser.add_option("-o", "--output", dest="outputFile", metavar="FILE", |
|
60 |
help="place output in file FILE") |
|
61 |
optParser.add_option("-p", "--parse-only", action="store_true", dest="parseonly", |
|
62 |
help="parse only, then output pyAST as text to stdout", default=False) |
|
63 |
optParser.add_option("-P", "--pretty", action="store_true", default=False, |
|
64 |
help="pretty print output adding whitespace") |
|
65 |
optParser.add_option("-s", "--stringparamdict", dest="stringparams", metavar="STRINGPARAMS", |
|
66 |
help="call X/YSLT script with dictionary STRINGPARAMS as string parameters") |
|
67 |
optParser.add_option("-x", "--xml", action="store_true", default=False, |
|
68 |
help="input document is XML already") |
|
69 |
optParser.add_option("-X", "--xslt", dest="xslt", metavar="XSLTSCRIPT", |
|
70 |
help="execute XSLT script XSLTSCRIPT") |
|
71 |
optParser.add_option("-y", "--yslt", dest="yslt", metavar="YSLTSCRIPT", |
|
72 |
help="execute YSLT script YSLTSCRIPT") |
|
73 |
optParser.add_option("-Y", "--xml2yml", action="store_true", default=False, |
|
74 |
help="convert XML to normalized YML code") |
|
75 |
optParser.add_option("-V", "--version", action="callback", callback=printInfo, help="show version info and exit") |
|
76 |
(options, args) = optParser.parse_args() |
|
77 |
||
78 |
if options.old_syntax: |
|
79 |
oldSyntax() |
|
80 |
||
81 |
if options.trace: |
|
82 |
backend.enable_tracing = True |
|
83 |
||
84 |
if options.emitlinenumbers: |
|
85 |
backend.emitlinenumbers = True |
|
86 |
||
87 |
if options.includePathText: |
|
88 |
backend.includePath = options.includePathText.split(':') |
|
89 |
||
90 |
backend.encoding = options.encoding |
|
91 |
||
92 |
dirs = os.environ.get('YML_PATH', '.').split(':') |
|
93 |
backend.includePath.extend(dirs) |
|
94 |
||
95 |
if options.xml2yml: |
|
96 |
for directory in backend.includePath: |
|
97 |
try: |
|
98 |
name = directory + "/xml2yml.ysl2" |
|
99 |
f = open(name, "r") |
|
100 |
f.close() |
|
101 |
break |
|
102 |
except: |
|
103 |
pass |
|
104 |
||
105 |
options.yslt = name |
|
106 |
options.xml = True |
|
107 |
||
108 |
if (options.xslt and options.yslt) or (options.xslt and options.xpath) or (options.yslt and options.xpath): |
|
109 |
sys.stderr.write("Cannot combine --xpath, --xslt and --yslt params\n") |
|
110 |
sys.exit(1) |
|
111 |
||
112 |
try: |
|
113 |
ymlC = ymlCStyle() |
|
114 |
||
32 | 115 |
rtext = "" |
0 | 116 |
|
117 |
if not options.emptyinput: |
|
118 |
files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding)) |
|
119 |
||
120 |
if options.xml: |
|
121 |
rtext = "" |
|
122 |
for line in files: |
|
123 |
rtext += line |
|
124 |
else: |
|
125 |
result = parse(ymlC, files, True, comment) |
|
126 |
if options.parseonly: |
|
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
127 |
print(result) |
0 | 128 |
sys.exit(0) |
129 |
else: |
|
130 |
rtext = backend.finish(result) |
|
131 |
||
132 |
if not rtext: |
|
32 | 133 |
rtext = "<empty/>" |
0 | 134 |
|
135 |
def ymldebug(context, text): |
|
136 |
if options.trace: |
|
137 |
sys.stderr.write("Debug: " + codecs.encode(u(text), options.encoding) + "\n") |
|
138 |
return "" |
|
139 |
||
140 |
def ymlassert(context, value, msg): |
|
141 |
if options.trace: |
|
142 |
if not value: |
|
143 |
raise YMLAssert(msg) |
|
144 |
return "" |
|
145 |
||
146 |
ymlns = etree.FunctionNamespace("http://fdik.org/yml") |
|
147 |
ymlns.prefix = "yml" |
|
148 |
ymlns['debug'] = ymldebug |
|
149 |
ymlns['assert'] = ymlassert |
|
150 |
||
151 |
if options.xpath: |
|
152 |
tree = etree.fromstring(rtext) |
|
153 |
ltree = tree.xpath(codecs.decode(options.xpath, options.encoding)) |
|
32 | 154 |
rtext = "" |
0 | 155 |
try: |
156 |
for rtree in ltree: |
|
157 |
rtext += etree.tostring(rtree, pretty_print=options.pretty, encoding=unicode) |
|
158 |
except: |
|
159 |
rtext = ltree |
|
160 |
||
161 |
elif options.yslt or options.xslt: |
|
162 |
params = {} |
|
163 |
||
164 |
if options.yslt: |
|
165 |
backend.clearAll() |
|
166 |
yscript = fileinput.input(options.yslt, mode="rU", openhook=fileinput.hook_encoded(options.encoding)) |
|
167 |
yresult = parse(ymlC, yscript, True, comment) |
|
168 |
ytext = backend.finish(yresult) |
|
169 |
else: |
|
170 |
yscript = fileinput.input(options.xslt, mode="rU") |
|
171 |
ytext = "" |
|
172 |
for line in yscript: |
|
173 |
ytext += line |
|
174 |
||
175 |
doc = etree.fromstring(rtext) |
|
176 |
||
177 |
xsltree = etree.XML(ytext, base_url=os.path.abspath(yscript.filename())) |
|
178 |
transform = etree.XSLT(xsltree) |
|
179 |
||
180 |
if options.params: |
|
181 |
params = eval(options.params) |
|
182 |
for key, value in params.iteritems(): |
|
31 | 183 |
if type(value) is not str: |
0 | 184 |
params[key] = u(value) |
185 |
if options.stringparams: |
|
186 |
for key, value in eval(options.stringparams).iteritems(): |
|
32 | 187 |
params[key] = "'" + u(value) + "'" |
0 | 188 |
|
189 |
rresult = transform(doc, **params) |
|
190 |
# lxml is somewhat buggy |
|
191 |
try: |
|
192 |
rtext = u(rresult) |
|
193 |
except: |
|
194 |
rtext = etree.tostring(rresult, encoding=unicode) |
|
195 |
if not rtext: |
|
196 |
rtext = codecs.decode(str(rresult), "utf-8") |
|
197 |
||
198 |
if options.normalization != "none": |
|
199 |
rtext = unicodedata.normalize(options.normalization, rtext) |
|
200 |
||
201 |
if options.pretty: |
|
202 |
plaintext = etree.tostring(etree.fromstring(rtext), pretty_print=True, xml_declaration=True, encoding=options.encoding) |
|
203 |
else: |
|
31 | 204 |
if isinstance(rtext, str): |
0 | 205 |
plaintext = codecs.encode(rtext, options.encoding) |
206 |
else: |
|
31 | 207 |
plaintext = rtext |
0 | 208 |
|
209 |
try: |
|
210 |
if plaintext[-1] == "\n": |
|
211 |
plaintext = plaintext[:-1] |
|
212 |
except: pass |
|
213 |
||
214 |
if options.outputFile and options.outputFile != "-": |
|
31 | 215 |
outfile = open(options.outputFile, "wb") |
0 | 216 |
outfile.write(plaintext) |
217 |
outfile.close() |
|
218 |
else: |
|
31 | 219 |
sys.stdout.buffer.write(plaintext) |
220 |
if not options.pretty: |
|
221 |
print() |
|
0 | 222 |
|
223 |
except KeyboardInterrupt: |
|
224 |
w("\n") |
|
225 |
sys.exit(1) |
|
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
226 |
except YMLAssert as msg: |
32 | 227 |
w("YML Assertion failed: " + u(msg) + "\n") |
0 | 228 |
sys.exit(2) |
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
229 |
except KeyError as msg: |
32 | 230 |
w("not found: " + u(msg) + "\n") |
0 | 231 |
sys.exit(4) |
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
232 |
except LookupError as msg: |
32 | 233 |
w("not found: " + u(msg) + "\n") |
0 | 234 |
sys.exit(4) |
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
235 |
except etree.XMLSyntaxError as e: |
0 | 236 |
log = e.error_log.filter_from_level(etree.ErrorLevels.FATAL) |
237 |
for entry in log: |
|
32 | 238 |
w("XML error: " + u(entry.message) + "\n") |
0 | 239 |
sys.exit(5) |
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
240 |
except Exception as msg: |
0 | 241 |
w(msg) |
242 |
sys.exit(5) |