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