#!/usr/bin/env python # vim: set fileencoding=utf-8 : """\ YML 2 compiler version 5.9 Copyleft (c), 2009-2018, Volker Birk http://fdik.org/yml/ """ import sys, os, codecs, locale import fileinput, unicodedata from optparse import OptionParser from pyPEG import parse, u from yml2 import ymlCStyle, comment, oldSyntax import backend def printInfo(option, opt_str, value, parser): sys.stdout.write(__doc__) def w(msg): if isinstance(msg, BaseException): try: msg = str(msg) + "\n" except: msg = u(msg) + u"\n" if type(msg) is unicode: msg = codecs.encode(msg, sys.stderr.encoding) sys.stderr.write(msg) optParser = OptionParser() optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax", help="syntax of YML 2 version 1.x (compatibility mode)", default=False) optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers", help="emit line numbers into the resulting XML for debugging purposes", default=False) optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getdefaultlocale()[1], help="encoding of input files (default to locale)") optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH", help="precede YML_PATH by a colon separated INCLUDE_PATH to search for include files") optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm", help="does nothing (only there for compatibility reasons)", default=False) optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC", help="Unicode normalization (none, NFD, NFKD, NFC, NFKC, FCD, default is NFC)") optParser.add_option("-o", "--output", dest="outputFile", metavar="FILE", help="place output in file FILE") optParser.add_option("-p", "--parse-only", action="store_true", dest="parseonly", help="parse only, then output pyAST as text to stdout", default=False) optParser.add_option("-V", "--version", action="callback", callback=printInfo, help="show version info") (options, args) = optParser.parse_args() if options.old_syntax: oldSyntax() if options.emitlinenumbers: backend.emitlinenumbers = True backend.encoding = options.encoding try: if options.includePathText: backend.includePath = options.includePathText.split(':') dirs = os.environ.get('YML_PATH', '.').split(':') backend.includePath.extend(dirs) files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding)) ymlC = ymlCStyle() result = parse(ymlC, files, True, comment, packrat=True) if options.parseonly: print(result) else: result = backend.finish(result) if options.normalization != "none": result = unicodedata.normalize(options.normalization, result) if options.outputFile and options.outputFile != "-": outfile = open(options.outputFile, "w") outfile.write(codecs.encode(result, options.encoding)) outfile.close() else: print(codecs.encode(result, options.encoding)) except KeyboardInterrupt: w("\n") sys.exit(1) except KeyError as msg: w(u"not found: " + u(msg) + u"\n") sys.exit(4) except LookupError as msg: w(u"not found: " + u(msg) + u"\n") sys.exit(4) except Exception as msg: w(msg) sys.exit(5)