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