Imported Upstream version 1.47.1
[platform/upstream/gobject-introspection.git] / giscanner / annotationmain.py
index b69577e..b82ff81 100644 (file)
 # 02110-1301, USA.
 #
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+from __future__ import unicode_literals
+
+import sys
 import optparse
+import codecs
+from contextlib import contextmanager
 
-from giscanner.annotationparser import AnnotationParser
+from giscanner import message
+from giscanner.annotationparser import GtkDocCommentBlockParser, GtkDocCommentBlockWriter
 from giscanner.scannermain import (get_preprocessor_option_group,
                                    create_source_scanner,
                                    process_packages)
 
+
+@contextmanager
+def encode_stdout(encoding):
+    """Force stdout into a specific encoding."""
+    # Python 2 does not encode stdout writes so wrap it with 'encoding' encoded writer.
+    # Python 3 uses a io.TextIOBase wrapped stdout with the system default encoding.
+    # Re-wrap the underlying buffer with a new writer with the given 'encoding'.
+    # See: https://docs.python.org/3/library/sys.html#sys.stdout
+    old_stdout = sys.stdout
+    if sys.version_info.major < 3:
+        binary_stdout = sys.stdout
+    else:
+        binary_stdout = sys.stdout.buffer
+
+    sys.stdout = codecs.getwriter(encoding)(binary_stdout)
+    yield
+    sys.stdout = old_stdout
+
+
 def annotation_main(args):
     parser = optparse.OptionParser('%prog [options] sources')
 
@@ -51,21 +79,26 @@ def annotation_main(args):
     if options.packages:
         process_packages(options, options.packages)
 
+    logger = message.MessageLogger.get(namespace=None)
+
     ss = create_source_scanner(options, args)
 
     if options.extract:
-        ap = AnnotationParser()
-        blocks = ap.parse(ss.get_comments())
-        print '/' + ('*' * 60) + '/'
-        print '/* THIS FILE IS GENERATED DO NOT EDIT */'
-        print '/' + ('*' * 60) + '/'
-        print
-        for block in blocks.values():
-            print block.to_gtk_doc()
-            print
-        print
-        print '/' + ('*' * 60) + '/'
-        print '/* THIS FILE IS GENERATED DO NOT EDIT */'
-        print '/' + ('*' * 60) + '/'
+        parser = GtkDocCommentBlockParser()
+        writer = GtkDocCommentBlockWriter(indent=False)
+        blocks = parser.parse_comment_blocks(ss.get_comments())
+
+        with encode_stdout('utf-8'):
+            print('/' + ('*' * 60) + '/')
+            print('/* THIS FILE IS GENERATED DO NOT EDIT */')
+            print('/' + ('*' * 60) + '/')
+            print('')
+            for block in sorted(blocks.values()):
+                print(writer.write(block))
+                print('')
+            print('')
+            print('/' + ('*' * 60) + '/')
+            print('/* THIS FILE IS GENERATED DO NOT EDIT */')
+            print('/' + ('*' * 60) + '/')
 
     return 0