yml2/yml2c.py
changeset 58 a218553807ab
parent 55 e76930ea6464
child 59 ca8b9c6eb602
equal deleted inserted replaced
57:2f4ad3800a3f 58:a218553807ab
     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 2 compiler version 5.8
     5 YML 2 compiler version 6.2
     6 Copyleft (c), 2009-2011, 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
    15 from yml2 import ymlCStyle, comment, oldSyntax
    15 from yml2 import ymlCStyle, comment, oldSyntax
    16 import backend
    16 import backend
    17 
    17 
    18 def printInfo(option, opt_str, value, parser):
    18 def printInfo(option, opt_str, value, parser):
    19     sys.stdout.write(__doc__)
    19     sys.stdout.write(__doc__)
       
    20     sys.exit(0)
    20 
    21 
    21 def w(msg):
    22 def w(msg):
    22     if isinstance(msg, BaseException):
    23     if isinstance(msg, BaseException):
    23         try:
    24         msg = str(msg) + "\n"
    24             msg = str(msg) + "\n"
    25     if type(msg) is bytes:
    25         except:
       
    26             msg = u(msg) + u"\n"
       
    27     if type(msg) is unicode:
       
    28         msg = codecs.encode(msg, sys.stderr.encoding)
    26         msg = codecs.encode(msg, sys.stderr.encoding)
    29     sys.stderr.write(msg)
    27     sys.stderr.write(msg)
    30 
       
    31 
    28 
    32 def main():
    29 def main():
    33     optParser = OptionParser()
    30     optParser = OptionParser()
    34     optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax",
    31     optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax",
    35             help="syntax of YML 2 version 1.x (compatibility mode)", default=False)
    32             help="syntax of YML 2 version 1.x (compatibility mode)", default=False)
    76             result = backend.finish(result)
    73             result = backend.finish(result)
    77             if options.normalization != "none":
    74             if options.normalization != "none":
    78                 result = unicodedata.normalize(options.normalization, result)
    75                 result = unicodedata.normalize(options.normalization, result)
    79 
    76 
    80             if options.outputFile and options.outputFile != "-":
    77             if options.outputFile and options.outputFile != "-":
    81                 outfile = open(options.outputFile, "w")
    78                 outfile = open(options.outputFile, "wb")
    82                 outfile.write(codecs.encode(result, options.encoding))
    79                 outfile.write(codecs.encode(result, options.encoding))
    83                 outfile.close()
    80                 outfile.close()
    84             else:
    81             else:
    85                 print(codecs.encode(result, options.encoding))
    82                 sys.stdout.buffer.write(codecs.encode(result, options.encoding))
       
    83                 print()
    86 
    84 
    87     except KeyboardInterrupt:
    85     except KeyboardInterrupt:
    88         w("\n")
    86         w("\n")
    89         sys.exit(1)
    87         sys.exit(1)
    90     except KeyError as msg:
    88     except KeyError as msg:
    91         w(u"not found: " + u(msg) + u"\n")
    89         w("not found: " + u(msg) + "\n")
    92         sys.exit(4)
    90         sys.exit(4)
    93     except LookupError as msg:
    91     except LookupError as msg:
    94         w(u"not found: " + u(msg) + u"\n")
    92         w("not found: " + u(msg) + "\n")
    95         sys.exit(4)
    93         sys.exit(4)
    96     except Exception as msg:
    94     except Exception as msg:
    97         w(msg)
    95         w(msg)
    98         sys.exit(5)
    96         sys.exit(5)
    99 
    97 
   100 
    98 
   101 if __name__ == "__main__":
    99 if __name__ == "__main__":
   102     sys.exit(main())
   100     sys.exit(main())
       
   101