vb@31: #!/usr/bin/env python3
vb@0: # vim: set fileencoding=utf-8 :
vb@0: 
vb@0: """\
vb@40: YML/YSLT 2 processor version 6.2
vb@31: Copyleft (c), 2009-2019 Volker Birk  http://fdik.org/yml/
vb@0: 
vb@0: """
vb@0: 
vb@0: import sys, os, codecs, locale
vb@0: import fileinput, unicodedata
vb@0: from optparse import OptionParser
vb@0: 
vb@0: try:
vb@0:     from lxml import etree
vb@0: except:
vb@0:     sys.stderr.write("This program needs lxml, see http://codespeak.net/lxml/\n")
vb@0:     sys.exit(1)
vb@0: 
vb@0: from yml2 import ymlCStyle, comment, oldSyntax
h@41: from yml2.pyPEG import parse, u
h@41: import yml2.backend as backend
vb@0: 
h@43: YML_DEFAULT_PATH = [os.path.dirname(backend.__file__)]
h@43: 
vb@0: def printInfo(option, opt_str, value, parser):
vb@0:     sys.stdout.write(__doc__)
vb@0:     sys.exit(0)
vb@0: 
vb@0: class YMLAssert(Exception): pass
vb@0: 
vb@0: def w(msg):
vb@0:     if isinstance(msg, BaseException):
vb@0:         try:
vb@0:             msg = str(msg) + "\n"
vb@0:         except:
vb@32:             msg = u(msg) + "\n"
vb@0:     sys.stderr.write(msg)
vb@0: 
vb@0: optParser = OptionParser()
vb@0: optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax",
vb@0:         help="syntax of YML 2 version 1.x (compatibility mode)", default=False)
vb@0: optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers",
vb@0:         help="emit line numbers into the resulting XML for debugging purposes", default=False)
vb@0: optParser.add_option("--debug", action="store_true", dest="trace",
vb@0:         help="switch on tracing to stderr", default=False)
vb@0: optParser.add_option("-d", "--paramdict", dest="params", metavar="PARAMS",
vb@0:         help="call X/YSLT script with dictionary PARAMS as parameters")
vb@0: optParser.add_option("-e", "--xpath", dest="xpath", metavar="XPATH",
vb@0:         help="execute XPath expression XPATH and print result")
vb@0: optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getdefaultlocale()[1],
vb@0:         help="encoding of input files (default to locale)")
vb@0: optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH",
vb@0:         help="precede YML_PATH by a colon separated INCLUDE_PATH to search for include files")
vb@0: optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm",
vb@0:         help="does nothing (only there for compatibility reasons)", default=False)
vb@0: optParser.add_option("-M", "--empty-input-document", action="store_true", dest="emptyinput",
vb@0:         help="use an empty input document", default=False)
vb@0: optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC",
vb@0:         help="Unicode normalization (none, NFD, NFKD, NFC, NFKC, FCD, default is NFC)")
vb@0: optParser.add_option("-o", "--output", dest="outputFile", metavar="FILE",
vb@0:         help="place output in file FILE")
vb@0: optParser.add_option("-p", "--parse-only", action="store_true", dest="parseonly",
vb@0:         help="parse only, then output pyAST as text to stdout", default=False)
vb@0: optParser.add_option("-P", "--pretty", action="store_true", default=False,
vb@0:         help="pretty print output adding whitespace")
vb@0: optParser.add_option("-s", "--stringparamdict", dest="stringparams", metavar="STRINGPARAMS",
vb@0:         help="call X/YSLT script with dictionary STRINGPARAMS as string parameters")
vb@0: optParser.add_option("-x", "--xml", action="store_true", default=False,
vb@0:         help="input document is XML already")
vb@0: optParser.add_option("-X", "--xslt", dest="xslt", metavar="XSLTSCRIPT",
vb@0:         help="execute XSLT script XSLTSCRIPT")
vb@0: optParser.add_option("-y", "--yslt", dest="yslt", metavar="YSLTSCRIPT",
vb@0:         help="execute YSLT script YSLTSCRIPT")
vb@0: optParser.add_option("-Y", "--xml2yml", action="store_true", default=False,
vb@0:         help="convert XML to normalized YML code")
vb@0: optParser.add_option("-V", "--version", action="callback", callback=printInfo, help="show version info and exit")
vb@0: (options, args) = optParser.parse_args()
vb@0: 
vb@0: if options.old_syntax:
vb@0:     oldSyntax()
vb@0: 
vb@0: if options.trace:
vb@0:     backend.enable_tracing = True
vb@0: 
vb@0: if options.emitlinenumbers:
vb@0:     backend.emitlinenumbers = True
vb@0: 
vb@0: if options.includePathText:
vb@0:     backend.includePath = options.includePathText.split(':')
vb@0: 
vb@0: backend.encoding = options.encoding
vb@0: 
h@43: dirs = os.environ.get('YML_PATH', '.').split(':') + YML_DEFAULT_PATH
vb@0: backend.includePath.extend(dirs)
vb@0: 
vb@0: if options.xml2yml:
vb@0:     for directory in backend.includePath:
h@42:         name = os.path.join(directory, "xml2yml.ysl2")
h@42:         if os.path.isfile(name):
h@42:             options.yslt = name
h@42:             options.xml = True
vb@0:             break
h@42:     else:
h@42:         sys.stderr.write("Error: Stylesheet xml2yml.ysl2 required for --xml2yml not found\n")
h@42:         sys.stderr.write("Please check your YML_PATH\n")
h@42:         sys.exit(1)
vb@0: 
vb@0: if  (options.xslt and options.yslt) or (options.xslt and options.xpath) or (options.yslt and options.xpath):
vb@0:     sys.stderr.write("Cannot combine --xpath, --xslt and --yslt params\n")
vb@0:     sys.exit(1)
vb@0: 
vb@0: try:
vb@0:     ymlC = ymlCStyle()
vb@0: 
vb@32:     rtext = ""
vb@0: 
vb@0:     if not options.emptyinput:
vb@0:         files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding))
vb@0: 
vb@0:         if options.xml:
vb@0:             rtext = ""
vb@0:             for line in files:
vb@0:                 rtext += line
vb@0:         else:
vb@0:             result = parse(ymlC, files, True, comment)
vb@0:             if options.parseonly:
vb@7:                 print(result)
vb@0:                 sys.exit(0)
vb@0:             else:
vb@0:                 rtext = backend.finish(result)
vb@0: 
vb@0:     if not rtext:
vb@32:         rtext = "<empty/>"
vb@0: 
vb@0:     def ymldebug(context, text):
vb@0:         if options.trace:
vb@0:             sys.stderr.write("Debug: " + codecs.encode(u(text), options.encoding) + "\n")
vb@0:         return ""
vb@0: 
vb@0:     def ymlassert(context, value, msg):
vb@0:         if options.trace:
vb@0:             if not value:
vb@0:                 raise YMLAssert(msg)
vb@0:         return ""
vb@0: 
vb@0:     ymlns = etree.FunctionNamespace("http://fdik.org/yml")
vb@0:     ymlns.prefix = "yml"
vb@0:     ymlns['debug'] = ymldebug
vb@0:     ymlns['assert'] = ymlassert
vb@0: 
vb@0:     if options.xpath:
vb@0:         tree = etree.fromstring(rtext)
vb@0:         ltree = tree.xpath(codecs.decode(options.xpath, options.encoding))
vb@32:         rtext = ""
vb@0:         try:
vb@0:             for rtree in ltree:
vb@0:                 rtext += etree.tostring(rtree, pretty_print=options.pretty, encoding=unicode)
vb@0:         except:
vb@0:             rtext = ltree
vb@0: 
vb@0:     elif options.yslt or options.xslt:
vb@0:         params = {}
vb@0: 
vb@0:         if options.yslt:
vb@0:             backend.clearAll()
vb@0:             yscript = fileinput.input(options.yslt, mode="rU", openhook=fileinput.hook_encoded(options.encoding))
vb@0:             yresult = parse(ymlC, yscript, True, comment)
vb@0:             ytext = backend.finish(yresult)
vb@0:         else:
vb@0:             yscript = fileinput.input(options.xslt, mode="rU")
vb@0:             ytext = ""
vb@0:             for line in yscript:
vb@0:                 ytext += line
vb@0: 
vb@0:         doc = etree.fromstring(rtext)
vb@0: 
vb@0:         xsltree = etree.XML(ytext, base_url=os.path.abspath(yscript.filename()))
vb@0:         transform = etree.XSLT(xsltree)
vb@0:         
vb@0:         if options.params:
vb@0:             params = eval(options.params)
vb@0:             for key, value in params.iteritems():
vb@31:                 if type(value) is not str:
vb@0:                     params[key] = u(value)
vb@0:         if options.stringparams:
vb@0:             for key, value in eval(options.stringparams).iteritems():
vb@32:                 params[key] = "'" + u(value) + "'"
vb@0: 
vb@0:         rresult = transform(doc, **params)
vb@0:         # lxml is somewhat buggy
vb@0:         try:
vb@0:             rtext = u(rresult)
vb@0:         except:
vb@0:             rtext = etree.tostring(rresult, encoding=unicode)
vb@0:             if not rtext:
vb@0:                 rtext = codecs.decode(str(rresult), "utf-8")
vb@0: 
vb@0:     if options.normalization != "none":
vb@0:         rtext = unicodedata.normalize(options.normalization, rtext)
vb@0: 
vb@0:     if options.pretty:
vb@0:         plaintext = etree.tostring(etree.fromstring(rtext), pretty_print=True, xml_declaration=True, encoding=options.encoding)
vb@0:     else:
vb@31:         if isinstance(rtext, str):
vb@0:             plaintext = codecs.encode(rtext, options.encoding)
vb@0:         else:
vb@31:             plaintext = rtext
vb@0: 
vb@0:     try:
vb@0:         if plaintext[-1] == "\n":
vb@0:             plaintext = plaintext[:-1]
vb@0:     except: pass
vb@0: 
vb@0:     if options.outputFile and options.outputFile != "-":
vb@31:         outfile = open(options.outputFile, "wb")
vb@0:         outfile.write(plaintext)
vb@0:         outfile.close()
vb@0:     else:
vb@31:         sys.stdout.buffer.write(plaintext)
vb@31:         if not options.pretty:
vb@31:             print()
vb@0: 
vb@0: except KeyboardInterrupt:
vb@0:     w("\n")
vb@0:     sys.exit(1)
vb@7: except YMLAssert as msg:
vb@32:     w("YML Assertion failed: " + u(msg) + "\n")
vb@0:     sys.exit(2)
vb@7: except KeyError as msg:
vb@32:     w("not found: " + u(msg) + "\n")
vb@0:     sys.exit(4)
vb@7: except LookupError as msg:
vb@32:     w("not found: " + u(msg) + "\n")
vb@0:     sys.exit(4)
vb@7: except etree.XMLSyntaxError as e:
vb@0:     log = e.error_log.filter_from_level(etree.ErrorLevels.FATAL)
vb@0:     for entry in log:
vb@32:         w("XML error: " + u(entry.message) + "\n")
vb@0:     sys.exit(5)
vb@7: except Exception as msg:
vb@0:     w(msg)
vb@0:     sys.exit(5)