yml2proc
author Volker Birk <vb@pep.foundation>
Tue, 25 Apr 2017 20:46:59 +0200
changeset 9 0250d70228bc
parent 7 f81a4471bc28
child 11 7026ccc7a60e
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
"""\
4
e7b49a65cf3a Version 5.5
Volker Birk <vb@pep.foundation>
parents: 0
diff changeset
     5
YML/YSLT 2 processor version 5.5
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     6
Copyleft (c), 2009-2011 Volker Birk  http://fdik.org/yml/
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
        # fileinput suffer from two nasty bugs :
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   122
        # - ignoring open hook with stdin
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   123
        # - iterator requires ctrl-D to be pressed twice on siome platform
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   124
        if args in [[],['-']] :
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   125
            files._files=[]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   126
            files._buffer=[unicode(line, options.encoding) for line in sys.stdin.readlines()]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   127
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   128
        if options.xml:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   129
            rtext = ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   130
            for line in files:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   131
                rtext += line
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   132
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   133
            result = parse(ymlC, files, True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   134
            if options.parseonly:
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   135
                print(result)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   136
                sys.exit(0)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   137
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   138
                rtext = backend.finish(result)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   139
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   140
    if not rtext:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   141
        rtext = u"<empty/>"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   142
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   143
    def ymldebug(context, text):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   144
        if options.trace:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   145
            sys.stderr.write("Debug: " + codecs.encode(u(text), options.encoding) + "\n")
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
    def ymlassert(context, value, msg):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   149
        if options.trace:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   150
            if not value:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   151
                raise YMLAssert(msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   152
        return ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   153
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   154
    ymlns = etree.FunctionNamespace("http://fdik.org/yml")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   155
    ymlns.prefix = "yml"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   156
    ymlns['debug'] = ymldebug
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   157
    ymlns['assert'] = ymlassert
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   158
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   159
    if options.xpath:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   160
        tree = etree.fromstring(rtext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   161
        ltree = tree.xpath(codecs.decode(options.xpath, options.encoding))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   162
        rtext = u""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   163
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   164
            for rtree in ltree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   165
                rtext += etree.tostring(rtree, pretty_print=options.pretty, encoding=unicode)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   166
        except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   167
            rtext = ltree
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   168
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   169
    elif options.yslt or options.xslt:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   170
        params = {}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   171
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   172
        if options.yslt:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   173
            backend.clearAll()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   174
            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
   175
            yresult = parse(ymlC, yscript, True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   176
            ytext = backend.finish(yresult)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   177
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   178
            yscript = fileinput.input(options.xslt, mode="rU")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   179
            ytext = ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   180
            for line in yscript:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   181
                ytext += line
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   182
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   183
        doc = etree.fromstring(rtext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   184
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   185
        xsltree = etree.XML(ytext, base_url=os.path.abspath(yscript.filename()))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   186
        transform = etree.XSLT(xsltree)
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
        if options.params:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   189
            params = eval(options.params)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   190
            for key, value in params.iteritems():
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   191
                if type(value) != unicode:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   192
                    params[key] = u(value)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   193
        if options.stringparams:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   194
            for key, value in eval(options.stringparams).iteritems():
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   195
                params[key] = u"'" + u(value) + u"'"
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
        rresult = transform(doc, **params)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   198
        # lxml is somewhat buggy
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   199
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   200
            rtext = u(rresult)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   201
        except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   202
            rtext = etree.tostring(rresult, encoding=unicode)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   203
            if not rtext:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   204
                rtext = codecs.decode(str(rresult), "utf-8")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   205
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   206
    if options.normalization != "none":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   207
        rtext = unicodedata.normalize(options.normalization, rtext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   208
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   209
    if options.pretty:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   210
        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
   211
    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   212
        if isinstance(rtext, unicode):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   213
            plaintext = codecs.encode(rtext, options.encoding)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   214
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   215
            plaintext = str(rtext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   216
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   217
    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   218
        if plaintext[-1] == "\n":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   219
            plaintext = plaintext[:-1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   220
    except: pass
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
    if options.outputFile and options.outputFile != "-":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   223
        outfile = open(options.outputFile, "w")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   224
        outfile.write(plaintext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   225
        outfile.close()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   226
    else:
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   227
        print(plaintext)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   228
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   229
except KeyboardInterrupt:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   230
    w("\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   231
    sys.exit(1)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   232
except YMLAssert as msg:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   233
    w(u"YML Assertion failed: " + u(msg) + u"\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   234
    sys.exit(2)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   235
except KeyError as msg:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   236
    w(u"not found: " + u(msg) + u"\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   237
    sys.exit(4)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   238
except LookupError as msg:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   239
    w(u"not found: " + u(msg) + u"\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   240
    sys.exit(4)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   241
except etree.XMLSyntaxError as e:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   242
    log = e.error_log.filter_from_level(etree.ErrorLevels.FATAL)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   243
    for entry in log:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   244
        w(u"XML error: " + u(entry.message) + u"\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   245
    sys.exit(5)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   246
except Exception as msg:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   247
    w(msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   248
    sys.exit(5)