yml2proc
author Hartmut Goebel <h.goebel@crazy-compilers.com>
Thu, 27 Oct 2016 13:33:48 +0200
changeset 44 4e5f42d51cc4
parent 43 fb35b9db9ca1
child 50 963c1b542610
permissions -rwxr-xr-x
docs: Include files are always searched at standard location, too.

This removes the need to specify the location where yml2 was unpacked or
installed as YML_PATH. So this now works:

YML_PATH=/foo ./yml2proc --xml2yml

The standard system location <prefix>/share/yml2 is now always appended to
the include search path. If this path does not exist, the directory one
above the `backend.py` file is added, allowing the same behaviour for
uninstalled yml2.

In the Makefile enforce the .ysl2 files to be searched in the current
directory first.
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
43
fb35b9db9ca1 Move data files into the Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 42
diff changeset
    24
YML_DEFAULT_PATH = [os.path.dirname(backend.__file__)]
fb35b9db9ca1 Move data files into the Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 42
diff changeset
    25
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    26
def printInfo(option, opt_str, value, parser):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    27
    sys.stdout.write(__doc__)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    28
    sys.exit(0)
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
class YMLAssert(Exception): pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    31
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    32
def w(msg):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    33
    if isinstance(msg, BaseException):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    34
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    35
            msg = str(msg) + "\n"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    36
        except:
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
    37
            msg = u(msg) + "\n"
0
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
43
fb35b9db9ca1 Move data files into the Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 42
diff changeset
    94
dirs = os.environ.get('YML_PATH', '.').split(':') + YML_DEFAULT_PATH
0
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:
42
700f4d003349 Catch missing xml2yml.ysl2 early.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 41
diff changeset
    99
        name = os.path.join(directory, "xml2yml.ysl2")
700f4d003349 Catch missing xml2yml.ysl2 early.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 41
diff changeset
   100
        if os.path.isfile(name):
700f4d003349 Catch missing xml2yml.ysl2 early.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 41
diff changeset
   101
            options.yslt = name
700f4d003349 Catch missing xml2yml.ysl2 early.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 41
diff changeset
   102
            options.xml = True
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   103
            break
42
700f4d003349 Catch missing xml2yml.ysl2 early.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 41
diff changeset
   104
    else:
700f4d003349 Catch missing xml2yml.ysl2 early.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 41
diff changeset
   105
        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
   106
        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
   107
        sys.exit(1)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   108
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   109
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
   110
    sys.stderr.write("Cannot combine --xpath, --xslt and --yslt params\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   111
    sys.exit(1)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   112
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   113
try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   114
    ymlC = ymlCStyle()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   115
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   116
    rtext = ""
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   117
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   118
    if not options.emptyinput:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   119
        files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   120
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   121
        if options.xml:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   122
            rtext = ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   123
            for line in files:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   124
                rtext += line
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   125
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   126
            result = parse(ymlC, files, True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   127
            if options.parseonly:
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   128
                print(result)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   129
                sys.exit(0)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   130
            else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   131
                rtext = backend.finish(result)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   132
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   133
    if not rtext:
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   134
        rtext = "<empty/>"
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   135
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   136
    def ymldebug(context, text):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   137
        if options.trace:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   138
            sys.stderr.write("Debug: " + codecs.encode(u(text), options.encoding) + "\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   139
        return ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   140
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   141
    def ymlassert(context, value, msg):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   142
        if options.trace:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   143
            if not value:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   144
                raise YMLAssert(msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   145
        return ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   146
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   147
    ymlns = etree.FunctionNamespace("http://fdik.org/yml")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   148
    ymlns.prefix = "yml"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   149
    ymlns['debug'] = ymldebug
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   150
    ymlns['assert'] = ymlassert
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   151
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   152
    if options.xpath:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   153
        tree = etree.fromstring(rtext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   154
        ltree = tree.xpath(codecs.decode(options.xpath, options.encoding))
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   155
        rtext = ""
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   156
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   157
            for rtree in ltree:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   158
                rtext += etree.tostring(rtree, pretty_print=options.pretty, encoding=unicode)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   159
        except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   160
            rtext = ltree
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   161
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   162
    elif options.yslt or options.xslt:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   163
        params = {}
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   164
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   165
        if options.yslt:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   166
            backend.clearAll()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   167
            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
   168
            yresult = parse(ymlC, yscript, True, comment)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   169
            ytext = backend.finish(yresult)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   170
        else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   171
            yscript = fileinput.input(options.xslt, mode="rU")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   172
            ytext = ""
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   173
            for line in yscript:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   174
                ytext += line
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
        doc = etree.fromstring(rtext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   177
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   178
        xsltree = etree.XML(ytext, base_url=os.path.abspath(yscript.filename()))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   179
        transform = etree.XSLT(xsltree)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   180
        
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   181
        if options.params:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   182
            params = eval(options.params)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   183
            for key, value in params.iteritems():
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   184
                if type(value) is not str:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   185
                    params[key] = u(value)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   186
        if options.stringparams:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   187
            for key, value in eval(options.stringparams).iteritems():
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   188
                params[key] = "'" + u(value) + "'"
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   189
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   190
        rresult = transform(doc, **params)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   191
        # lxml is somewhat buggy
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   192
        try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   193
            rtext = u(rresult)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   194
        except:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   195
            rtext = etree.tostring(rresult, encoding=unicode)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   196
            if not rtext:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   197
                rtext = codecs.decode(str(rresult), "utf-8")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   198
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   199
    if options.normalization != "none":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   200
        rtext = unicodedata.normalize(options.normalization, rtext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   201
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   202
    if options.pretty:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   203
        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
   204
    else:
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   205
        if isinstance(rtext, str):
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   206
            plaintext = codecs.encode(rtext, options.encoding)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   207
        else:
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   208
            plaintext = rtext
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   209
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   210
    try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   211
        if plaintext[-1] == "\n":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   212
            plaintext = plaintext[:-1]
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   213
    except: pass
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   214
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   215
    if options.outputFile and options.outputFile != "-":
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   216
        outfile = open(options.outputFile, "wb")
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   217
        outfile.write(plaintext)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   218
        outfile.close()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   219
    else:
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   220
        sys.stdout.buffer.write(plaintext)
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   221
        if not options.pretty:
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
   222
            print()
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   223
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   224
except KeyboardInterrupt:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   225
    w("\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   226
    sys.exit(1)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   227
except YMLAssert as msg:
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   228
    w("YML Assertion failed: " + u(msg) + "\n")
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   229
    sys.exit(2)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   230
except KeyError as msg:
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   231
    w("not found: " + u(msg) + "\n")
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   232
    sys.exit(4)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   233
except LookupError as msg:
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   234
    w("not found: " + u(msg) + "\n")
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   235
    sys.exit(4)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   236
except etree.XMLSyntaxError as e:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   237
    log = e.error_log.filter_from_level(etree.ErrorLevels.FATAL)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   238
    for entry in log:
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
   239
        w("XML error: " + u(entry.message) + "\n")
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   240
    sys.exit(5)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
   241
except Exception as msg:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   242
    w(msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
   243
    sys.exit(5)