yml2proc
author Volker Birk <vb@pep-project.org>
Fri, 11 Oct 2019 22:56:23 +0200
changeset 35 5414b157613a
parent 32 2b7b48758eaa
child 40 432ab62b2537
permissions -rwxr-xr-x
bug: textsectionu disappeared
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
"""\
35
5414b157613a bug: textsectionu disappeared
Volker Birk <vb@pep-project.org>
parents: 32
diff changeset
     5
YML/YSLT 2 processor version 6.1
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
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:
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:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    97
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    98
            name = directory + "/xml2yml.ysl2"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    99
            f = open(name, "r")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   100
            f.close()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   101
            break
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   102
        except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   103
            pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   104
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   105
    options.yslt = name
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   106
    options.xml = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   107
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   108
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
   109
    sys.stderr.write("Cannot combine --xpath, --xslt and --yslt params\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   110
    sys.exit(1)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   111
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   112
try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   113
    ymlC = ymlCStyle()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   114
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   115
    rtext = ""
0
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
    if not options.emptyinput:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   118
        files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   119
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   120
        if options.xml:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   121
            rtext = ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   122
            for line in files:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   123
                rtext += line
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   124
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   125
            result = parse(ymlC, files, True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   126
            if options.parseonly:
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   127
                print(result)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   128
                sys.exit(0)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   129
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   130
                rtext = backend.finish(result)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   131
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   132
    if not rtext:
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   133
        rtext = "<empty/>"
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   134
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   135
    def ymldebug(context, text):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   136
        if options.trace:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   137
            sys.stderr.write("Debug: " + codecs.encode(u(text), options.encoding) + "\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   138
        return ""
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
    def ymlassert(context, value, msg):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   141
        if options.trace:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   142
            if not value:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   143
                raise YMLAssert(msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   144
        return ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   145
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   146
    ymlns = etree.FunctionNamespace("http://fdik.org/yml")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   147
    ymlns.prefix = "yml"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   148
    ymlns['debug'] = ymldebug
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   149
    ymlns['assert'] = ymlassert
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   150
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   151
    if options.xpath:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   152
        tree = etree.fromstring(rtext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   153
        ltree = tree.xpath(codecs.decode(options.xpath, options.encoding))
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   154
        rtext = ""
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   155
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   156
            for rtree in ltree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   157
                rtext += etree.tostring(rtree, pretty_print=options.pretty, encoding=unicode)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   158
        except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   159
            rtext = ltree
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   160
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   161
    elif options.yslt or options.xslt:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   162
        params = {}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   163
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   164
        if options.yslt:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   165
            backend.clearAll()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   166
            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
   167
            yresult = parse(ymlC, yscript, True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   168
            ytext = backend.finish(yresult)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   169
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   170
            yscript = fileinput.input(options.xslt, mode="rU")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   171
            ytext = ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   172
            for line in yscript:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   173
                ytext += line
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   174
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   175
        doc = etree.fromstring(rtext)
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
        xsltree = etree.XML(ytext, base_url=os.path.abspath(yscript.filename()))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   178
        transform = etree.XSLT(xsltree)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   179
        
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   180
        if options.params:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   181
            params = eval(options.params)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   182
            for key, value in params.iteritems():
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   183
                if type(value) is not str:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   184
                    params[key] = u(value)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   185
        if options.stringparams:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   186
            for key, value in eval(options.stringparams).iteritems():
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   187
                params[key] = "'" + u(value) + "'"
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   188
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   189
        rresult = transform(doc, **params)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   190
        # lxml is somewhat buggy
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   191
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   192
            rtext = u(rresult)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   193
        except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   194
            rtext = etree.tostring(rresult, encoding=unicode)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   195
            if not rtext:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   196
                rtext = codecs.decode(str(rresult), "utf-8")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   197
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   198
    if options.normalization != "none":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   199
        rtext = unicodedata.normalize(options.normalization, rtext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   200
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   201
    if options.pretty:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   202
        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
   203
    else:
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   204
        if isinstance(rtext, str):
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   205
            plaintext = codecs.encode(rtext, options.encoding)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   206
        else:
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   207
            plaintext = rtext
0
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
    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   210
        if plaintext[-1] == "\n":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   211
            plaintext = plaintext[:-1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   212
    except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   213
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   214
    if options.outputFile and options.outputFile != "-":
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   215
        outfile = open(options.outputFile, "wb")
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   216
        outfile.write(plaintext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   217
        outfile.close()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   218
    else:
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   219
        sys.stdout.buffer.write(plaintext)
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   220
        if not options.pretty:
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   221
            print()
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:
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   227
    w("YML Assertion failed: " + u(msg) + "\n")
0
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:
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   230
    w("not found: " + u(msg) + "\n")
0
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:
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   233
    w("not found: " + u(msg) + "\n")
0
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:
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   238
        w("XML error: " + u(entry.message) + "\n")
0
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)