28 msg = codecs.encode(msg, sys.stderr.encoding) |
28 msg = codecs.encode(msg, sys.stderr.encoding) |
29 sys.stderr.write(msg) |
29 sys.stderr.write(msg) |
30 |
30 |
31 |
31 |
32 def main(): |
32 def main(): |
33 pass |
33 optParser = OptionParser() |
|
34 optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax", |
|
35 help="syntax of YML 2 version 1.x (compatibility mode)", default=False) |
|
36 optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers", |
|
37 help="emit line numbers into the resulting XML for debugging purposes", default=False) |
|
38 optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getdefaultlocale()[1], |
|
39 help="encoding of input files (default to locale)") |
|
40 optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH", |
|
41 help="precede YML_PATH by a colon separated INCLUDE_PATH to search for include files") |
|
42 optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm", |
|
43 help="does nothing (only there for compatibility reasons)", default=False) |
|
44 optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC", |
|
45 help="Unicode normalization (none, NFD, NFKD, NFC, NFKC, FCD, default is NFC)") |
|
46 optParser.add_option("-o", "--output", dest="outputFile", metavar="FILE", |
|
47 help="place output in file FILE") |
|
48 optParser.add_option("-p", "--parse-only", action="store_true", dest="parseonly", |
|
49 help="parse only, then output pyAST as text to stdout", default=False) |
|
50 optParser.add_option("-V", "--version", action="callback", callback=printInfo, help="show version info") |
|
51 (options, args) = optParser.parse_args() |
34 |
52 |
35 optParser = OptionParser() |
53 if options.old_syntax: |
36 optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax", |
54 oldSyntax() |
37 help="syntax of YML 2 version 1.x (compatibility mode)", default=False) |
|
38 optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers", |
|
39 help="emit line numbers into the resulting XML for debugging purposes", default=False) |
|
40 optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getdefaultlocale()[1], |
|
41 help="encoding of input files (default to locale)") |
|
42 optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH", |
|
43 help="precede YML_PATH by a colon separated INCLUDE_PATH to search for include files") |
|
44 optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm", |
|
45 help="does nothing (only there for compatibility reasons)", default=False) |
|
46 optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC", |
|
47 help="Unicode normalization (none, NFD, NFKD, NFC, NFKC, FCD, default is NFC)") |
|
48 optParser.add_option("-o", "--output", dest="outputFile", metavar="FILE", |
|
49 help="place output in file FILE") |
|
50 optParser.add_option("-p", "--parse-only", action="store_true", dest="parseonly", |
|
51 help="parse only, then output pyAST as text to stdout", default=False) |
|
52 optParser.add_option("-V", "--version", action="callback", callback=printInfo, help="show version info") |
|
53 (options, args) = optParser.parse_args() |
|
54 |
55 |
55 if options.old_syntax: |
56 if options.emitlinenumbers: |
56 oldSyntax() |
57 backend.emitlinenumbers = True |
57 |
58 |
58 if options.emitlinenumbers: |
59 backend.encoding = options.encoding |
59 backend.emitlinenumbers = True |
|
60 |
60 |
61 backend.encoding = options.encoding |
61 try: |
|
62 if options.includePathText: |
|
63 backend.includePath = options.includePathText.split(':') |
62 |
64 |
63 try: |
65 dirs = os.environ.get('YML_PATH', '.').split(':') |
64 if options.includePathText: |
66 backend.includePath.extend(dirs) |
65 backend.includePath = options.includePathText.split(':') |
|
66 |
67 |
67 dirs = os.environ.get('YML_PATH', '.').split(':') |
68 files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding)) |
68 backend.includePath.extend(dirs) |
|
69 |
69 |
70 files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding)) |
70 ymlC = ymlCStyle() |
|
71 result = parse(ymlC, files, True, comment, packrat=True) |
71 |
72 |
72 ymlC = ymlCStyle() |
73 if options.parseonly: |
73 result = parse(ymlC, files, True, comment, packrat=True) |
74 print(result) |
|
75 else: |
|
76 result = backend.finish(result) |
|
77 if options.normalization != "none": |
|
78 result = unicodedata.normalize(options.normalization, result) |
74 |
79 |
75 if options.parseonly: |
80 if options.outputFile and options.outputFile != "-": |
76 print(result) |
81 outfile = open(options.outputFile, "w") |
77 else: |
82 outfile.write(codecs.encode(result, options.encoding)) |
78 result = backend.finish(result) |
83 outfile.close() |
79 if options.normalization != "none": |
84 else: |
80 result = unicodedata.normalize(options.normalization, result) |
85 print(codecs.encode(result, options.encoding)) |
81 |
86 |
82 if options.outputFile and options.outputFile != "-": |
87 except KeyboardInterrupt: |
83 outfile = open(options.outputFile, "w") |
88 w("\n") |
84 outfile.write(codecs.encode(result, options.encoding)) |
89 sys.exit(1) |
85 outfile.close() |
90 except KeyError as msg: |
86 else: |
91 w(u"not found: " + u(msg) + u"\n") |
87 print(codecs.encode(result, options.encoding)) |
92 sys.exit(4) |
88 |
93 except LookupError as msg: |
89 except KeyboardInterrupt: |
94 w(u"not found: " + u(msg) + u"\n") |
90 w("\n") |
95 sys.exit(4) |
91 sys.exit(1) |
96 except Exception as msg: |
92 except KeyError as msg: |
97 w(msg) |
93 w(u"not found: " + u(msg) + u"\n") |
98 sys.exit(5) |
94 sys.exit(4) |
|
95 except LookupError as msg: |
|
96 w(u"not found: " + u(msg) + u"\n") |
|
97 sys.exit(4) |
|
98 except Exception as msg: |
|
99 w(msg) |
|
100 sys.exit(5) |
|
101 |
99 |
102 |
100 |
103 if __name__ == "__main__": |
101 if __name__ == "__main__": |
104 sys.exit(main()) |
102 sys.exit(main()) |