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