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 |
# fileinput suffer from two nasty bugs :
|
|
68 |
# - ignoring open hook with stdin
|
|
69 |
# - iterator requires ctrl-D to be pressed twice on some platform
|
|
70 |
if args in [[],['-']] :
|
|
71 |
files._files=[]
|
|
72 |
files._buffer=[unicode(line, options.encoding) for line in sys.stdin.readlines()]
|
|
73 |
|
|
74 |
ymlC = ymlCStyle()
|
|
75 |
result = parse(ymlC, files, True, comment, packrat=True)
|
|
76 |
|
|
77 |
if options.parseonly:
|
|
78 |
print result
|
|
79 |
else:
|
|
80 |
result = backend.finish(result)
|
|
81 |
if options.normalization != "none":
|
|
82 |
result = unicodedata.normalize(options.normalization, result)
|
|
83 |
|
|
84 |
if options.outputFile and options.outputFile != "-":
|
|
85 |
outfile = open(options.outputFile, "w")
|
|
86 |
outfile.write(codecs.encode(result, options.encoding))
|
|
87 |
outfile.close()
|
|
88 |
else:
|
|
89 |
print codecs.encode(result, options.encoding)
|
|
90 |
|
|
91 |
except KeyboardInterrupt:
|
|
92 |
w("\n")
|
|
93 |
sys.exit(1)
|
|
94 |
except KeyError, msg:
|
|
95 |
w(u"not found: " + u(msg) + u"\n")
|
|
96 |
sys.exit(4)
|
|
97 |
except LookupError, msg:
|
|
98 |
w(u"not found: " + u(msg) + u"\n")
|
|
99 |
sys.exit(4)
|
|
100 |
except Exception, msg:
|
|
101 |
w(msg)
|
|
102 |
sys.exit(5)
|