yml2proc
changeset 31 d3dddb80d1f5
parent 29 6a8a7951d8e6
child 32 2b7b48758eaa
equal deleted inserted replaced
30:a1ecf17c60eb 31:d3dddb80d1f5
     1 #!/usr/bin/env python
     1 #!/usr/bin/env python3
     2 # vim: set fileencoding=utf-8 :
     2 # vim: set fileencoding=utf-8 :
     3 
     3 
     4 """\
     4 """\
     5 YML/YSLT 2 processor version 5.10
     5 YML/YSLT 2 processor version 6.0
     6 Copyleft (c), 2009-2018 Volker Birk  http://fdik.org/yml/
     6 Copyleft (c), 2009-2019 Volker Birk  http://fdik.org/yml/
     7 
     7 
     8 """
     8 """
     9 
     9 
    10 import sys, os, codecs, locale
    10 import sys, os, codecs, locale
    11 import fileinput, unicodedata
    11 import fileinput, unicodedata
    31     if isinstance(msg, BaseException):
    31     if isinstance(msg, BaseException):
    32         try:
    32         try:
    33             msg = str(msg) + "\n"
    33             msg = str(msg) + "\n"
    34         except:
    34         except:
    35             msg = u(msg) + u"\n"
    35             msg = u(msg) + u"\n"
    36     if type(msg) is unicode:
    36     if type(msg) is str:
    37         msg = codecs.encode(msg, sys.stderr.encoding)
    37         msg = codecs.encode(msg, sys.stderr.encoding)
    38     sys.stderr.write(msg)
    38     sys.stderr.write(msg)
    39 
    39 
    40 optParser = OptionParser()
    40 optParser = OptionParser()
    41 optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax",
    41 optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax",
   180         transform = etree.XSLT(xsltree)
   180         transform = etree.XSLT(xsltree)
   181         
   181         
   182         if options.params:
   182         if options.params:
   183             params = eval(options.params)
   183             params = eval(options.params)
   184             for key, value in params.iteritems():
   184             for key, value in params.iteritems():
   185                 if type(value) != unicode:
   185                 if type(value) is not str:
   186                     params[key] = u(value)
   186                     params[key] = u(value)
   187         if options.stringparams:
   187         if options.stringparams:
   188             for key, value in eval(options.stringparams).iteritems():
   188             for key, value in eval(options.stringparams).iteritems():
   189                 params[key] = u"'" + u(value) + u"'"
   189                 params[key] = u"'" + u(value) + u"'"
   190 
   190 
   201         rtext = unicodedata.normalize(options.normalization, rtext)
   201         rtext = unicodedata.normalize(options.normalization, rtext)
   202 
   202 
   203     if options.pretty:
   203     if options.pretty:
   204         plaintext = etree.tostring(etree.fromstring(rtext), pretty_print=True, xml_declaration=True, encoding=options.encoding)
   204         plaintext = etree.tostring(etree.fromstring(rtext), pretty_print=True, xml_declaration=True, encoding=options.encoding)
   205     else:
   205     else:
   206         if isinstance(rtext, unicode):
   206         if isinstance(rtext, str):
   207             plaintext = codecs.encode(rtext, options.encoding)
   207             plaintext = codecs.encode(rtext, options.encoding)
   208         else:
   208         else:
   209             plaintext = str(rtext)
   209             plaintext = rtext
   210 
   210 
   211     try:
   211     try:
   212         if plaintext[-1] == "\n":
   212         if plaintext[-1] == "\n":
   213             plaintext = plaintext[:-1]
   213             plaintext = plaintext[:-1]
   214     except: pass
   214     except: pass
   215 
   215 
   216     if options.outputFile and options.outputFile != "-":
   216     if options.outputFile and options.outputFile != "-":
   217         outfile = open(options.outputFile, "w")
   217         outfile = open(options.outputFile, "wb")
   218         outfile.write(plaintext)
   218         outfile.write(plaintext)
   219         outfile.close()
   219         outfile.close()
   220     else:
   220     else:
   221         print(plaintext)
   221         sys.stdout.buffer.write(plaintext)
       
   222         if not options.pretty:
       
   223             print()
   222 
   224 
   223 except KeyboardInterrupt:
   225 except KeyboardInterrupt:
   224     w("\n")
   226     w("\n")
   225     sys.exit(1)
   227     sys.exit(1)
   226 except YMLAssert as msg:
   228 except YMLAssert as msg: