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