22 |
22 |
23 from yml2.yml2 import ymlCStyle, comment, oldSyntax |
23 from yml2.yml2 import ymlCStyle, comment, oldSyntax |
24 from yml2.pyPEG import parse, u |
24 from yml2.pyPEG import parse, u |
25 import yml2.backend as backend |
25 import yml2.backend as backend |
26 |
26 |
27 YML_DEFAULT_PATH = [os.path.dirname(backend.__file__)] |
27 YML_DEFAULT_PATHS = [os.path.dirname(backend.__file__)] |
28 |
28 |
29 def printInfo(option, opt_str, value, parser): |
29 def printInfo(option, opt_str, value, parser): |
30 sys.stdout.write(__doc__) |
30 sys.stdout.write(__doc__) |
31 sys.exit(0) |
31 sys.exit(0) |
32 |
32 |
39 except: |
39 except: |
40 msg = u(msg) + "\n" |
40 msg = u(msg) + "\n" |
41 sys.stderr.write(msg) |
41 sys.stderr.write(msg) |
42 |
42 |
43 def main(): |
43 def main(): |
|
44 dirsepname = {':': 'a colon', ';': 'a semicolon'}.get(os.path.pathsep, "a '%s'" % os.path.pathsep) |
44 optParser = OptionParser() |
45 optParser = OptionParser() |
45 optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax", |
46 optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax", |
46 help="syntax of YML 2 version 1.x (compatibility mode)", default=False) |
47 help="syntax of YML 2 version 1.x (compatibility mode)", default=False) |
47 optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers", |
48 optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers", |
48 help="emit line numbers into the resulting XML for debugging purposes", default=False) |
49 help="emit line numbers into the resulting XML for debugging purposes", default=False) |
53 optParser.add_option("-e", "--xpath", dest="xpath", metavar="XPATH", |
54 optParser.add_option("-e", "--xpath", dest="xpath", metavar="XPATH", |
54 help="execute XPath expression XPATH and print result") |
55 help="execute XPath expression XPATH and print result") |
55 optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getdefaultlocale()[1], |
56 optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getdefaultlocale()[1], |
56 help="encoding of input files (default to locale)") |
57 help="encoding of input files (default to locale)") |
57 optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH", |
58 optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH", |
58 help="precede YML_PATH by a colon separated INCLUDE_PATH to search for include files") |
59 help="precede YML_PATH by %s separated INCLUDE_PATH to search for include files" % dirsepname) |
59 optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm", |
60 optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm", |
60 help="does nothing (only there for compatibility reasons)", default=False) |
61 help="does nothing (only there for compatibility reasons)", default=False) |
61 optParser.add_option("-M", "--empty-input-document", action="store_true", dest="emptyinput", |
62 optParser.add_option("-M", "--empty-input-document", action="store_true", dest="emptyinput", |
62 help="use an empty input document", default=False) |
63 help="use an empty input document", default=False) |
63 optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC", |
64 optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC", |
89 |
90 |
90 if options.emitlinenumbers: |
91 if options.emitlinenumbers: |
91 backend.emitlinenumbers = True |
92 backend.emitlinenumbers = True |
92 |
93 |
93 if options.includePathText: |
94 if options.includePathText: |
94 backend.includePath = options.includePathText.split(':') |
95 backend.includePath = options.includePathText.split(os.path.pathsep) |
95 |
96 |
96 backend.encoding = options.encoding |
97 backend.encoding = options.encoding |
97 |
98 |
98 dirs = os.environ.get('YML_PATH', '.').split(':') + YML_DEFAULT_PATH |
99 dirs = os.environ.get('YML_PATH', '.').split(os.path.pathsep) + YML_DEFAULT_PATHS |
99 backend.includePath.extend(dirs) |
100 backend.includePath.extend(dirs) |
100 |
101 |
101 if options.xml2yml: |
102 if options.xml2yml: |
102 for directory in backend.includePath: |
103 for directory in backend.includePath: |
103 name = os.path.join(directory, "xml2yml.ysl2") |
104 name = os.path.join(directory, "xml2yml.ysl2") |