yml2c
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 2 compiler 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
41
98a53c3282c3 Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 40
diff changeset
    14
from yml2.pyPEG import parse, u
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    15
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
    16
import yml2.backend as backend
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    17
43
fb35b9db9ca1 Move data files into the Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 41
diff changeset
    18
YML_DEFAULT_PATH = [os.path.dirname(backend.__file__)]
fb35b9db9ca1 Move data files into the Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 41
diff changeset
    19
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    20
def printInfo(option, opt_str, value, parser):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    21
    sys.stdout.write(__doc__)
34
d9eaf22f13ea exit on version string
Volker Birk <vb@pep-project.org>
parents: 32
diff changeset
    22
    sys.exit(0)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    23
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    24
def w(msg):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    25
    if isinstance(msg, BaseException):
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
    26
        msg = str(msg) + "\n"
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
    27
    if type(msg) is bytes:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    28
        msg = codecs.encode(msg, sys.stderr.encoding)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    29
    sys.stderr.write(msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    30
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    31
optParser = OptionParser()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    32
optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    33
        help="syntax of YML 2 version 1.x (compatibility mode)", default=False)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    34
optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    35
        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
    36
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
    37
        help="encoding of input files (default to locale)")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    38
optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    39
        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
    40
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
    41
        help="does nothing (only there for compatibility reasons)", default=False)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    42
optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    43
        help="Unicode normalization (none, NFD, NFKD, NFC, NFKC, FCD, default is NFC)")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    44
optParser.add_option("-o", "--output", dest="outputFile", metavar="FILE",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    45
        help="place output in file FILE")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    46
optParser.add_option("-p", "--parse-only", action="store_true", dest="parseonly",
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    47
        help="parse only, then output pyAST as text to stdout", default=False)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    48
optParser.add_option("-V", "--version", action="callback", callback=printInfo, help="show version info")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    49
(options, args) = optParser.parse_args()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    50
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    51
if options.old_syntax:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    52
    oldSyntax()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    53
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    54
if options.emitlinenumbers:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    55
    backend.emitlinenumbers = True
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    56
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    57
backend.encoding = options.encoding
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    58
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    59
try:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    60
    if options.includePathText:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    61
        backend.includePath = options.includePathText.split(':')
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    62
43
fb35b9db9ca1 Move data files into the Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 41
diff changeset
    63
    dirs = os.environ.get('YML_PATH', '.').split(':') + YML_DEFAULT_PATH
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    64
    backend.includePath.extend(dirs)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    65
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    66
    files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    67
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    68
    ymlC = ymlCStyle()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    69
    result = parse(ymlC, files, True, comment, packrat=True)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    70
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    71
    if options.parseonly:
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
    72
        print(result)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    73
    else:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    74
        result = backend.finish(result)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    75
        if options.normalization != "none":
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    76
            result = unicodedata.normalize(options.normalization, result)
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.outputFile and options.outputFile != "-":
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
    79
            outfile = open(options.outputFile, "wb")
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    80
            outfile.write(codecs.encode(result, options.encoding))
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    81
            outfile.close()
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    82
        else:
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
    83
            sys.stdout.buffer.write(codecs.encode(result, options.encoding))
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
    84
            print()
0
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
except KeyboardInterrupt:
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    87
    w("\n")
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    88
    sys.exit(1)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
    89
except KeyError as msg:
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
    90
    w("not found: " + u(msg) + "\n")
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    91
    sys.exit(4)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
    92
except LookupError as msg:
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
    93
    w("not found: " + u(msg) + "\n")
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    94
    sys.exit(4)
7
f81a4471bc28 beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents: 4
diff changeset
    95
except Exception as msg:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    96
    w(msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    97
    sys.exit(5)