yml2proc
author Volker Birk <vb@pep-project.org>
Tue, 29 Jan 2019 11:23:36 +0100
changeset 27 0cdfe84ef643
parent 23 f2e8837da4e9
child 25 cb4a7f8b230d
child 29 6a8a7951d8e6
permissions -rwxr-xr-x
...
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     1
#!/usr/bin/env python
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     2
# vim: set fileencoding=utf-8 :
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     3
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     4
"""\
23
f2e8837da4e9 allowing || in line quotes
Volker Birk <vb@pep.foundation>
parents: 20
diff changeset
     5
YML/YSLT 2 processor version 5.9
f2e8837da4e9 allowing || in line quotes
Volker Birk <vb@pep.foundation>
parents: 20
diff changeset
     6
Copyleft (c), 2009-2018 Volker Birk  http://fdik.org/yml/
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     7
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     8
"""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     9
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    10
import sys, os, codecs, locale
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    11
import fileinput, unicodedata
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    12
from optparse import OptionParser
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    13
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    14
try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    15
    from lxml import etree
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    16
except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    17
    sys.stderr.write("This program needs lxml, see http://codespeak.net/lxml/\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    18
    sys.exit(1)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    19
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    20
from yml2 import ymlCStyle, comment, oldSyntax
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    21
from pyPEG import parse, u
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    22
import backend
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    23
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    24
def printInfo(option, opt_str, value, parser):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    25
    sys.stdout.write(__doc__)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    26
    sys.exit(0)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    27
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    28
class YMLAssert(Exception): pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    29
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    30
def w(msg):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    31
    if isinstance(msg, BaseException):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    32
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    33
            msg = str(msg) + "\n"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    34
        except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    35
            msg = u(msg) + u"\n"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    36
    if type(msg) is unicode:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    37
        msg = codecs.encode(msg, sys.stderr.encoding)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    38
    sys.stderr.write(msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    39
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    40
optParser = OptionParser()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    41
optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    42
        help="syntax of YML 2 version 1.x (compatibility mode)", default=False)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    43
optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    44
        help="emit line numbers into the resulting XML for debugging purposes", default=False)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    45
optParser.add_option("--debug", action="store_true", dest="trace",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    46
        help="switch on tracing to stderr", default=False)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    47
optParser.add_option("-d", "--paramdict", dest="params", metavar="PARAMS",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    48
        help="call X/YSLT script with dictionary PARAMS as parameters")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    49
optParser.add_option("-e", "--xpath", dest="xpath", metavar="XPATH",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    50
        help="execute XPath expression XPATH and print result")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    51
optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getdefaultlocale()[1],
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    52
        help="encoding of input files (default to locale)")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    53
optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    54
        help="precede YML_PATH by a colon separated INCLUDE_PATH to search for include files")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    55
optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    56
        help="does nothing (only there for compatibility reasons)", default=False)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    57
optParser.add_option("-M", "--empty-input-document", action="store_true", dest="emptyinput",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    58
        help="use an empty input document", default=False)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    59
optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    60
        help="Unicode normalization (none, NFD, NFKD, NFC, NFKC, FCD, default is NFC)")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    61
optParser.add_option("-o", "--output", dest="outputFile", metavar="FILE",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    62
        help="place output in file FILE")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    63
optParser.add_option("-p", "--parse-only", action="store_true", dest="parseonly",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    64
        help="parse only, then output pyAST as text to stdout", default=False)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    65
optParser.add_option("-P", "--pretty", action="store_true", default=False,
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    66
        help="pretty print output adding whitespace")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    67
optParser.add_option("-s", "--stringparamdict", dest="stringparams", metavar="STRINGPARAMS",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    68
        help="call X/YSLT script with dictionary STRINGPARAMS as string parameters")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    69
optParser.add_option("-x", "--xml", action="store_true", default=False,
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    70
        help="input document is XML already")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    71
optParser.add_option("-X", "--xslt", dest="xslt", metavar="XSLTSCRIPT",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    72
        help="execute XSLT script XSLTSCRIPT")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    73
optParser.add_option("-y", "--yslt", dest="yslt", metavar="YSLTSCRIPT",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    74
        help="execute YSLT script YSLTSCRIPT")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    75
optParser.add_option("-Y", "--xml2yml", action="store_true", default=False,
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    76
        help="convert XML to normalized YML code")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    77
optParser.add_option("-V", "--version", action="callback", callback=printInfo, help="show version info and exit")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    78
(options, args) = optParser.parse_args()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    79
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    80
if options.old_syntax:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    81
    oldSyntax()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    82
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    83
if options.trace:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    84
    backend.enable_tracing = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    85
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    86
if options.emitlinenumbers:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    87
    backend.emitlinenumbers = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    88
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    89
if options.includePathText:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    90
    backend.includePath = options.includePathText.split(':')
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    91
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    92
backend.encoding = options.encoding
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    93
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    94
dirs = os.environ.get('YML_PATH', '.').split(':')
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    95
backend.includePath.extend(dirs)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    96
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    97
if options.xml2yml:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    98
    for directory in backend.includePath:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    99
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   100
            name = directory + "/xml2yml.ysl2"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   101
            f = open(name, "r")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   102
            f.close()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   103
            break
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   104
        except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   105
            pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   106
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   107
    options.yslt = name
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   108
    options.xml = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   109
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   110
if  (options.xslt and options.yslt) or (options.xslt and options.xpath) or (options.yslt and options.xpath):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   111
    sys.stderr.write("Cannot combine --xpath, --xslt and --yslt params\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   112
    sys.exit(1)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   113
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   114
try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   115
    ymlC = ymlCStyle()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   116
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   117
    rtext = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   118
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   119
    if not options.emptyinput:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   120
        files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   121
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   122
        if options.xml:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   123
            rtext = ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   124
            for line in files:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   125
                rtext += line
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   126
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   127
            result = parse(ymlC, files, True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   128
            if options.parseonly:
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   129
                print(result)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   130
                sys.exit(0)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   131
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   132
                rtext = backend.finish(result)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   133
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   134
    if not rtext:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   135
        rtext = u"<empty/>"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   136
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   137
    def ymldebug(context, text):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   138
        if options.trace:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   139
            sys.stderr.write("Debug: " + codecs.encode(u(text), options.encoding) + "\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   140
        return ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   141
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   142
    def ymlassert(context, value, msg):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   143
        if options.trace:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   144
            if not value:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   145
                raise YMLAssert(msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   146
        return ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   147
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   148
    ymlns = etree.FunctionNamespace("http://fdik.org/yml")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   149
    ymlns.prefix = "yml"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   150
    ymlns['debug'] = ymldebug
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   151
    ymlns['assert'] = ymlassert
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   152
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   153
    if options.xpath:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   154
        tree = etree.fromstring(rtext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   155
        ltree = tree.xpath(codecs.decode(options.xpath, options.encoding))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   156
        rtext = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   157
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   158
            for rtree in ltree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   159
                rtext += etree.tostring(rtree, pretty_print=options.pretty, encoding=unicode)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   160
        except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   161
            rtext = ltree
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   162
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   163
    elif options.yslt or options.xslt:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   164
        params = {}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   165
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   166
        if options.yslt:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   167
            backend.clearAll()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   168
            yscript = fileinput.input(options.yslt, mode="rU", openhook=fileinput.hook_encoded(options.encoding))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   169
            yresult = parse(ymlC, yscript, True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   170
            ytext = backend.finish(yresult)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   171
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   172
            yscript = fileinput.input(options.xslt, mode="rU")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   173
            ytext = ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   174
            for line in yscript:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   175
                ytext += line
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   176
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   177
        doc = etree.fromstring(rtext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   178
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   179
        xsltree = etree.XML(ytext, base_url=os.path.abspath(yscript.filename()))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   180
        transform = etree.XSLT(xsltree)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   181
        
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   182
        if options.params:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   183
            params = eval(options.params)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   184
            for key, value in params.iteritems():
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   185
                if type(value) != unicode:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   186
                    params[key] = u(value)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   187
        if options.stringparams:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   188
            for key, value in eval(options.stringparams).iteritems():
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   189
                params[key] = u"'" + u(value) + u"'"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   190
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   191
        rresult = transform(doc, **params)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   192
        # lxml is somewhat buggy
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   193
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   194
            rtext = u(rresult)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   195
        except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   196
            rtext = etree.tostring(rresult, encoding=unicode)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   197
            if not rtext:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   198
                rtext = codecs.decode(str(rresult), "utf-8")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   199
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   200
    if options.normalization != "none":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   201
        rtext = unicodedata.normalize(options.normalization, rtext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   202
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   203
    if options.pretty:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   204
        plaintext = etree.tostring(etree.fromstring(rtext), pretty_print=True, xml_declaration=True, encoding=options.encoding)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   205
    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   206
        if isinstance(rtext, unicode):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   207
            plaintext = codecs.encode(rtext, options.encoding)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   208
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   209
            plaintext = str(rtext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   210
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   211
    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   212
        if plaintext[-1] == "\n":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   213
            plaintext = plaintext[:-1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   214
    except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   215
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   216
    if options.outputFile and options.outputFile != "-":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   217
        outfile = open(options.outputFile, "w")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   218
        outfile.write(plaintext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   219
        outfile.close()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   220
    else:
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   221
        print(plaintext)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   222
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   223
except KeyboardInterrupt:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   224
    w("\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   225
    sys.exit(1)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   226
except YMLAssert as msg:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   227
    w(u"YML Assertion failed: " + u(msg) + u"\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   228
    sys.exit(2)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   229
except KeyError as msg:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   230
    w(u"not found: " + u(msg) + u"\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   231
    sys.exit(4)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   232
except LookupError as msg:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   233
    w(u"not found: " + u(msg) + u"\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   234
    sys.exit(4)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   235
except etree.XMLSyntaxError as e:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   236
    log = e.error_log.filter_from_level(etree.ErrorLevels.FATAL)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   237
    for entry in log:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   238
        w(u"XML error: " + u(entry.message) + u"\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   239
    sys.exit(5)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   240
except Exception as msg:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   241
    w(msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   242
    sys.exit(5)