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