yml2/yml2c.py
changeset 58 a218553807ab
parent 55 e76930ea6464
child 59 ca8b9c6eb602
--- a/yml2/yml2c.py	Wed Feb 27 14:43:35 2019 +0100
+++ b/yml2/yml2c.py	Wed Mar 18 19:20:01 2020 +0100
@@ -1,9 +1,9 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # vim: set fileencoding=utf-8 :
 
 """\
-YML 2 compiler version 5.8
-Copyleft (c), 2009-2011, Volker Birk  http://fdik.org/yml/
+YML 2 compiler version 6.2
+Copyleft (c), 2009-2019, Volker Birk  http://fdik.org/yml/
 
 """
 
@@ -17,18 +17,15 @@
 
 def printInfo(option, opt_str, value, parser):
     sys.stdout.write(__doc__)
+    sys.exit(0)
 
 def w(msg):
     if isinstance(msg, BaseException):
-        try:
-            msg = str(msg) + "\n"
-        except:
-            msg = u(msg) + u"\n"
-    if type(msg) is unicode:
+        msg = str(msg) + "\n"
+    if type(msg) is bytes:
         msg = codecs.encode(msg, sys.stderr.encoding)
     sys.stderr.write(msg)
 
-
 def main():
     optParser = OptionParser()
     optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax",
@@ -78,20 +75,21 @@
                 result = unicodedata.normalize(options.normalization, result)
 
             if options.outputFile and options.outputFile != "-":
-                outfile = open(options.outputFile, "w")
+                outfile = open(options.outputFile, "wb")
                 outfile.write(codecs.encode(result, options.encoding))
                 outfile.close()
             else:
-                print(codecs.encode(result, options.encoding))
+                sys.stdout.buffer.write(codecs.encode(result, options.encoding))
+                print()
 
     except KeyboardInterrupt:
         w("\n")
         sys.exit(1)
     except KeyError as msg:
-        w(u"not found: " + u(msg) + u"\n")
+        w("not found: " + u(msg) + "\n")
         sys.exit(4)
     except LookupError as msg:
-        w(u"not found: " + u(msg) + u"\n")
+        w("not found: " + u(msg) + "\n")
         sys.exit(4)
     except Exception as msg:
         w(msg)
@@ -100,3 +98,4 @@
 
 if __name__ == "__main__":
     sys.exit(main())
+