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