yml2proc
changeset 52 b4a9a3122abb
parent 22 3a2bd70c01df
child 53 b94d4c5b9496
equal deleted inserted replaced
22:3a2bd70c01df 52:b4a9a3122abb
     1 #!/usr/bin/env python
       
     2 # vim: set fileencoding=utf-8 :
       
     3 
       
     4 """\
       
     5 YML/YSLT 2 processor version 5.8
       
     6 Copyleft (c), 2009-2011 Volker Birk  http://fdik.org/yml/
       
     7 
       
     8 """
       
     9 
       
    10 import sys, os, codecs, locale
       
    11 import fileinput, unicodedata
       
    12 from optparse import OptionParser
       
    13 
       
    14 try:
       
    15     from lxml import etree
       
    16 except:
       
    17     sys.stderr.write("This program needs lxml, see http://codespeak.net/lxml/\n")
       
    18     sys.exit(1)
       
    19 
       
    20 from yml2 import ymlCStyle, comment, oldSyntax
       
    21 from pyPEG import parse, u
       
    22 import backend
       
    23 
       
    24 def printInfo(option, opt_str, value, parser):
       
    25     sys.stdout.write(__doc__)
       
    26     sys.exit(0)
       
    27 
       
    28 class YMLAssert(Exception): pass
       
    29 
       
    30 def w(msg):
       
    31     if isinstance(msg, BaseException):
       
    32         try:
       
    33             msg = str(msg) + "\n"
       
    34         except:
       
    35             msg = u(msg) + u"\n"
       
    36     if type(msg) is unicode:
       
    37         msg = codecs.encode(msg, sys.stderr.encoding)
       
    38     sys.stderr.write(msg)
       
    39 
       
    40 optParser = OptionParser()
       
    41 optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax",
       
    42         help="syntax of YML 2 version 1.x (compatibility mode)", default=False)
       
    43 optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers",
       
    44         help="emit line numbers into the resulting XML for debugging purposes", default=False)
       
    45 optParser.add_option("--debug", action="store_true", dest="trace",
       
    46         help="switch on tracing to stderr", default=False)
       
    47 optParser.add_option("-d", "--paramdict", dest="params", metavar="PARAMS",
       
    48         help="call X/YSLT script with dictionary PARAMS as parameters")
       
    49 optParser.add_option("-e", "--xpath", dest="xpath", metavar="XPATH",
       
    50         help="execute XPath expression XPATH and print result")
       
    51 optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getdefaultlocale()[1],
       
    52         help="encoding of input files (default to locale)")
       
    53 optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH",
       
    54         help="precede YML_PATH by a colon separated INCLUDE_PATH to search for include files")
       
    55 optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm",
       
    56         help="does nothing (only there for compatibility reasons)", default=False)
       
    57 optParser.add_option("-M", "--empty-input-document", action="store_true", dest="emptyinput",
       
    58         help="use an empty input document", default=False)
       
    59 optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC",
       
    60         help="Unicode normalization (none, NFD, NFKD, NFC, NFKC, FCD, default is NFC)")
       
    61 optParser.add_option("-o", "--output", dest="outputFile", metavar="FILE",
       
    62         help="place output in file FILE")
       
    63 optParser.add_option("-p", "--parse-only", action="store_true", dest="parseonly",
       
    64         help="parse only, then output pyAST as text to stdout", default=False)
       
    65 optParser.add_option("-P", "--pretty", action="store_true", default=False,
       
    66         help="pretty print output adding whitespace")
       
    67 optParser.add_option("-s", "--stringparamdict", dest="stringparams", metavar="STRINGPARAMS",
       
    68         help="call X/YSLT script with dictionary STRINGPARAMS as string parameters")
       
    69 optParser.add_option("-x", "--xml", action="store_true", default=False,
       
    70         help="input document is XML already")
       
    71 optParser.add_option("-X", "--xslt", dest="xslt", metavar="XSLTSCRIPT",
       
    72         help="execute XSLT script XSLTSCRIPT")
       
    73 optParser.add_option("-y", "--yslt", dest="yslt", metavar="YSLTSCRIPT",
       
    74         help="execute YSLT script YSLTSCRIPT")
       
    75 optParser.add_option("-Y", "--xml2yml", action="store_true", default=False,
       
    76         help="convert XML to normalized YML code")
       
    77 optParser.add_option("-V", "--version", action="callback", callback=printInfo, help="show version info and exit")
       
    78 (options, args) = optParser.parse_args()
       
    79 
       
    80 if options.old_syntax:
       
    81     oldSyntax()
       
    82 
       
    83 if options.trace:
       
    84     backend.enable_tracing = True
       
    85 
       
    86 if options.emitlinenumbers:
       
    87     backend.emitlinenumbers = True
       
    88 
       
    89 if options.includePathText:
       
    90     backend.includePath = options.includePathText.split(':')
       
    91 
       
    92 backend.encoding = options.encoding
       
    93 
       
    94 dirs = os.environ.get('YML_PATH', '.').split(':')
       
    95 backend.includePath.extend(dirs)
       
    96 
       
    97 if options.xml2yml:
       
    98     for directory in backend.includePath:
       
    99         try:
       
   100             name = directory + "/xml2yml.ysl2"
       
   101             f = open(name, "r")
       
   102             f.close()
       
   103             break
       
   104         except:
       
   105             pass
       
   106 
       
   107     options.yslt = name
       
   108     options.xml = True
       
   109 
       
   110 if  (options.xslt and options.yslt) or (options.xslt and options.xpath) or (options.yslt and options.xpath):
       
   111     sys.stderr.write("Cannot combine --xpath, --xslt and --yslt params\n")
       
   112     sys.exit(1)
       
   113 
       
   114 try:
       
   115     ymlC = ymlCStyle()
       
   116 
       
   117     rtext = u""
       
   118 
       
   119     if not options.emptyinput:
       
   120         files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding))
       
   121 
       
   122         if options.xml:
       
   123             rtext = ""
       
   124             for line in files:
       
   125                 rtext += line
       
   126         else:
       
   127             result = parse(ymlC, files, True, comment)
       
   128             if options.parseonly:
       
   129                 print(result)
       
   130                 sys.exit(0)
       
   131             else:
       
   132                 rtext = backend.finish(result)
       
   133 
       
   134     if not rtext:
       
   135         rtext = u"<empty/>"
       
   136 
       
   137     def ymldebug(context, text):
       
   138         if options.trace:
       
   139             sys.stderr.write("Debug: " + codecs.encode(u(text), options.encoding) + "\n")
       
   140         return ""
       
   141 
       
   142     def ymlassert(context, value, msg):
       
   143         if options.trace:
       
   144             if not value:
       
   145                 raise YMLAssert(msg)
       
   146         return ""
       
   147 
       
   148     ymlns = etree.FunctionNamespace("http://fdik.org/yml")
       
   149     ymlns.prefix = "yml"
       
   150     ymlns['debug'] = ymldebug
       
   151     ymlns['assert'] = ymlassert
       
   152 
       
   153     if options.xpath:
       
   154         tree = etree.fromstring(rtext)
       
   155         ltree = tree.xpath(codecs.decode(options.xpath, options.encoding))
       
   156         rtext = u""
       
   157         try:
       
   158             for rtree in ltree:
       
   159                 rtext += etree.tostring(rtree, pretty_print=options.pretty, encoding=unicode)
       
   160         except:
       
   161             rtext = ltree
       
   162 
       
   163     elif options.yslt or options.xslt:
       
   164         params = {}
       
   165 
       
   166         if options.yslt:
       
   167             backend.clearAll()
       
   168             yscript = fileinput.input(options.yslt, mode="rU", openhook=fileinput.hook_encoded(options.encoding))
       
   169             yresult = parse(ymlC, yscript, True, comment)
       
   170             ytext = backend.finish(yresult)
       
   171         else:
       
   172             yscript = fileinput.input(options.xslt, mode="rU")
       
   173             ytext = ""
       
   174             for line in yscript:
       
   175                 ytext += line
       
   176 
       
   177         doc = etree.fromstring(rtext)
       
   178 
       
   179         xsltree = etree.XML(ytext, base_url=os.path.abspath(yscript.filename()))
       
   180         transform = etree.XSLT(xsltree)
       
   181         
       
   182         if options.params:
       
   183             params = eval(options.params)
       
   184             for key, value in params.iteritems():
       
   185                 if type(value) != unicode:
       
   186                     params[key] = u(value)
       
   187         if options.stringparams:
       
   188             for key, value in eval(options.stringparams).iteritems():
       
   189                 params[key] = u"'" + u(value) + u"'"
       
   190 
       
   191         rresult = transform(doc, **params)
       
   192         # lxml is somewhat buggy
       
   193         try:
       
   194             rtext = u(rresult)
       
   195         except:
       
   196             rtext = etree.tostring(rresult, encoding=unicode)
       
   197             if not rtext:
       
   198                 rtext = codecs.decode(str(rresult), "utf-8")
       
   199 
       
   200     if options.normalization != "none":
       
   201         rtext = unicodedata.normalize(options.normalization, rtext)
       
   202 
       
   203     if options.pretty:
       
   204         plaintext = etree.tostring(etree.fromstring(rtext), pretty_print=True, xml_declaration=True, encoding=options.encoding)
       
   205     else:
       
   206         if isinstance(rtext, unicode):
       
   207             plaintext = codecs.encode(rtext, options.encoding)
       
   208         else:
       
   209             plaintext = str(rtext)
       
   210 
       
   211     try:
       
   212         if plaintext[-1] == "\n":
       
   213             plaintext = plaintext[:-1]
       
   214     except: pass
       
   215 
       
   216     if options.outputFile and options.outputFile != "-":
       
   217         outfile = open(options.outputFile, "w")
       
   218         outfile.write(plaintext)
       
   219         outfile.close()
       
   220     else:
       
   221         print(plaintext)
       
   222 
       
   223 except KeyboardInterrupt:
       
   224     w("\n")
       
   225     sys.exit(1)
       
   226 except YMLAssert as msg:
       
   227     w(u"YML Assertion failed: " + u(msg) + u"\n")
       
   228     sys.exit(2)
       
   229 except KeyError as msg:
       
   230     w(u"not found: " + u(msg) + u"\n")
       
   231     sys.exit(4)
       
   232 except LookupError as msg:
       
   233     w(u"not found: " + u(msg) + u"\n")
       
   234     sys.exit(4)
       
   235 except etree.XMLSyntaxError as e:
       
   236     log = e.error_log.filter_from_level(etree.ErrorLevels.FATAL)
       
   237     for entry in log:
       
   238         w(u"XML error: " + u(entry.message) + u"\n")
       
   239     sys.exit(5)
       
   240 except Exception as msg:
       
   241     w(msg)
       
   242     sys.exit(5)