author | Volker Birk <vb@pep-project.org> |
Fri, 11 Oct 2019 21:38:48 +0200 | |
changeset 32 | 2b7b48758eaa |
parent 31 | d3dddb80d1f5 |
child 34 | d9eaf22f13ea |
permissions | -rwxr-xr-x |
31 | 1 |
#!/usr/bin/env python3 |
0 | 2 |
# vim: set fileencoding=utf-8 : |
3 |
||
4 |
"""\ |
|
31 | 5 |
YML 2 compiler version 6.0 |
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 |
from pyPEG import parse, u |
|
15 |
from yml2 import ymlCStyle, comment, oldSyntax |
|
16 |
import backend |
|
17 |
||
18 |
def printInfo(option, opt_str, value, parser): |
|
19 |
sys.stdout.write(__doc__) |
|
20 |
||
21 |
def w(msg): |
|
22 |
if isinstance(msg, BaseException): |
|
32 | 23 |
msg = str(msg) + "\n" |
31 | 24 |
if type(msg) is bytes: |
0 | 25 |
msg = codecs.encode(msg, sys.stderr.encoding) |
26 |
sys.stderr.write(msg) |
|
27 |
||
28 |
optParser = OptionParser() |
|
29 |
optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax", |
|
30 |
help="syntax of YML 2 version 1.x (compatibility mode)", default=False) |
|
31 |
optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers", |
|
32 |
help="emit line numbers into the resulting XML for debugging purposes", default=False) |
|
33 |
optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getdefaultlocale()[1], |
|
34 |
help="encoding of input files (default to locale)") |
|
35 |
optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH", |
|
36 |
help="precede YML_PATH by a colon separated INCLUDE_PATH to search for include files") |
|
37 |
optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm", |
|
38 |
help="does nothing (only there for compatibility reasons)", default=False) |
|
39 |
optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC", |
|
40 |
help="Unicode normalization (none, NFD, NFKD, NFC, NFKC, FCD, default is NFC)") |
|
41 |
optParser.add_option("-o", "--output", dest="outputFile", metavar="FILE", |
|
42 |
help="place output in file FILE") |
|
43 |
optParser.add_option("-p", "--parse-only", action="store_true", dest="parseonly", |
|
44 |
help="parse only, then output pyAST as text to stdout", default=False) |
|
45 |
optParser.add_option("-V", "--version", action="callback", callback=printInfo, help="show version info") |
|
46 |
(options, args) = optParser.parse_args() |
|
47 |
||
48 |
if options.old_syntax: |
|
49 |
oldSyntax() |
|
50 |
||
51 |
if options.emitlinenumbers: |
|
52 |
backend.emitlinenumbers = True |
|
53 |
||
54 |
backend.encoding = options.encoding |
|
55 |
||
56 |
try: |
|
57 |
if options.includePathText: |
|
58 |
backend.includePath = options.includePathText.split(':') |
|
59 |
||
60 |
dirs = os.environ.get('YML_PATH', '.').split(':') |
|
61 |
backend.includePath.extend(dirs) |
|
62 |
||
63 |
files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding)) |
|
64 |
||
65 |
ymlC = ymlCStyle() |
|
66 |
result = parse(ymlC, files, True, comment, packrat=True) |
|
67 |
||
68 |
if options.parseonly: |
|
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
69 |
print(result) |
0 | 70 |
else: |
71 |
result = backend.finish(result) |
|
72 |
if options.normalization != "none": |
|
73 |
result = unicodedata.normalize(options.normalization, result) |
|
74 |
||
75 |
if options.outputFile and options.outputFile != "-": |
|
31 | 76 |
outfile = open(options.outputFile, "wb") |
0 | 77 |
outfile.write(codecs.encode(result, options.encoding)) |
78 |
outfile.close() |
|
79 |
else: |
|
31 | 80 |
sys.stdout.buffer.write(codecs.encode(result, options.encoding)) |
81 |
print() |
|
0 | 82 |
|
83 |
except KeyboardInterrupt: |
|
84 |
w("\n") |
|
85 |
sys.exit(1) |
|
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
86 |
except KeyError as msg: |
32 | 87 |
w("not found: " + u(msg) + "\n") |
0 | 88 |
sys.exit(4) |
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
89 |
except LookupError as msg: |
32 | 90 |
w("not found: " + u(msg) + "\n") |
0 | 91 |
sys.exit(4) |
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
92 |
except Exception as msg: |
0 | 93 |
w(msg) |
94 |
sys.exit(5) |