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