#!/usr/bin/python -O
# vim: set fileencoding=utf-8 :

"""\
YML/YSLT 2 processor version 3.5
Copyleft (c), 2009, 2010, Volker Birk  http://fdik.org/yml/

"""

import sys, os, codecs
import fileinput
from optparse import OptionParser

try:
    from lxml import etree
except:
    sys.stderr.write("This program needs lxml, see http://codespeak.net/lxml/\n")
    sys.exit(1)

from yml2 import ymlCStyle, comment, oldSyntax
from pyPEG import parse
import backend

def printInfo(option, opt_str, value, parser):
    sys.stdout.write(__doc__)
    sys.exit(0)

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("-d", "--paramdict", dest="params", metavar="PARAMS",
        help="call X/YSLT script with dictionary PARAMS as parameters")
optParser.add_option("-e", "--xpath", dest="xpath", metavar="XPATH",
        help="execute XPath expression XPATH and print result")
optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default="UTF-8",
        help="encoding of input files (default: UTF-8)")
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("-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("-P", "--pretty", action="store_true", default=False,
        help="pretty print output adding whitespace")
optParser.add_option("-s", "--stringparamdict", dest="stringparams", metavar="STRINGPARAMS",
        help="call X/YSLT script with dictionary STRINGPARAMS as string parameters")
optParser.add_option("-x", "--xml", action="store_true", default=False,
        help="input document is XML already")
optParser.add_option("-X", "--xslt", dest="xslt", metavar="XSLTSCRIPT",
        help="execute XSLT script XSLTSCRIPT")
optParser.add_option("-y", "--yslt", dest="yslt", metavar="YSLTSCRIPT",
        help="execute YSLT script YSLTSCRIPT")
optParser.add_option("-Y", "--xml2yml", action="store_true", default=False,
        help="convert XML to normalized YML code")
optParser.add_option("--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

if options.includePathText:
    backend.includePath = options.includePathText.split(':')

backend.encoding = options.encoding

dirs = os.environ.get('YML_PATH', '.').split(':')
backend.includePath.extend(dirs)

if options.xml2yml:
    for directory in backend.includePath:
        try:
            name = directory + "/xml2yml.ysl2"
            f = open(name, "r")
            f.close()
            break
        except:
            pass

    options.yslt = name
    options.xml = True

if  (options.xslt and options.yslt) or (options.xslt and options.xpath) or (options.yslt and options.xpath):
    sys.stderr.write("Cannot combine --xpath, --xslt and --yslt params\n")
    sys.exit(1)

try:
    ymlC = ymlCStyle()

    rtext = u""
    files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding))

    if options.xml:
        for line in files:
            rtext += line
    else:
        result = parse(ymlC, files, True, comment)
        if options.parseonly:
            print result
            sys.exit(0)
        else:
            rtext = backend.finish(result)

    if not rtext:
        rtext = u"<empty/>"

    if options.xpath:
        tree = etree.fromstring(rtext)
        ltree = tree.xpath(options.xpath)
        rtext = u""
        for rtree in ltree:
            rtext += codecs.decode(etree.tostring(rtree, pretty_print=options.pretty, xml_declaration=True, encoding="UTF-8"), "utf-8")
    else:
        if options.pretty:
            rtext = codecs.decode(etree.tostring(etree.fromstring(rtext), pretty_print=True, xml_declaration=True, encoding="UTF-8"), "utf-8")

    if options.yslt or options.xslt:
        params = {}
        if options.yslt:
            backend.clearAll()
            yscript = fileinput.input(options.yslt, mode="rU", openhook=fileinput.hook_encoded(options.encoding))
            yresult = parse(ymlC, yscript, True, comment)
            ytext = backend.finish(yresult)
        else:
            yscript = fileinput.input(options.xslt, mode="rU", openhook=fileinput.hook_encoded(options.encoding))
            ytext = ""
            for line in yscript:
                ytext += line
        transform = etree.XSLT(etree.fromstring(ytext, base_url=os.path.abspath(yscript.filename())))
        if options.params:
            params = eval(options.params)
            for key, value in params.iteritems():
                if type(value) != unicode and type(value) != str:
                    params[key] = unicode(value)
        if options.stringparams:
            for key, value in eval(options.stringparams).iteritems():
                params[key] = u"'" + unicode(value) + u"'"
        rresult = transform(etree.fromstring(rtext), **params)
        if options.pretty:
            rtext = codecs.decode(etree.tostring(rresult, pretty_print=True, xml_declaration=True, encoding="UTF-8"), "utf-8")
        else:
            rtext = unicode(rresult)

    if type(rtext) != unicode and type(rtext) != str:
        rtext = unicode(rtext)
    try:
        if rtext[-1] == "\n":
            rtext = rtext[:-1]
    except: pass

    if options.outputFile and options.outputFile != "-":
        outfile = open(options.outputFile, "w")
        outfile.write(codecs.encode(rtext, options.encoding))
        outfile.close()
    else:
        print codecs.encode(rtext, options.encoding)

except KeyboardInterrupt:
    sys.stderr.write("\n")
    sys.exit(1)
except KeyError, msg:
    sys.stderr.write(u"not found: " + unicode(msg) + u"\n")
    sys.exit(4)
except LookupError, msg:
    sys.stderr.write(u"not found: " + unicode(msg) + u"\n")
    sys.exit(4)
except etree.XMLSyntaxError, e:
    log = e.error_log.filter_from_level(etree.ErrorLevels.FATAL)
    for entry in log:
        sys.stderr.write(u"XML error: " + unicode(entry.message) + u"\n")
    sys.exit(5)
except Exception, msg:
    sys.stderr.write(unicode(msg) + u"\n")
    sys.exit(5)
