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