mallardwriter: Improve type formatting for Python
authorJasper St. Pierre <jstpierre@mecheye.net>
Wed, 9 Jan 2013 15:38:01 +0000 (10:38 -0500)
committerJasper St. Pierre <jstpierre@mecheye.net>
Wed, 9 Jan 2013 19:09:50 +0000 (14:09 -0500)
Rather than use the glib names like gint/gchar and friends, use the
Python type names. Right now, this is Python 2, but we'll soon be
introducing a special Python 3 formatter.

giscanner/mallardwriter.py
tests/doctool/DocExamples-1.0-Python-expected/DocExamples.Obj-property-example.page
tests/doctool/DocExamples-1.0-Python-expected/DocExamples.Obj-vfunc.page
tests/doctool/DocExamples-1.0-Python-expected/DocExamples.Obj.method.page
tests/doctool/DocExamples-1.0-Python-expected/DocExamples.Obj.static_method.page
tests/doctool/DocExamples-1.0-Python-expected/DocExamples.array_function.page

index 801df08..5a9f094 100644 (file)
@@ -374,6 +374,30 @@ class MallardFormatterPython(MallardFormatter):
         else:
             return parameter.argname
 
+    def format_fundamental_type(self, name):
+        fundamental_types = {
+            "utf8": "unicode",
+            "gunichar": "unicode",
+            "gchar": "str",
+            "guchar": "str",
+            "gboolean": "bool",
+            "gint": "int",
+            "guint": "int",
+            "glong": "int",
+            "gulong": "int",
+            "gint64": "int",
+            "guint64": "int",
+            "gfloat": "float",
+            "gdouble": "float",
+            "gchararray": "str",
+            "GParam": "GLib.Param",
+            "PyObject": "object",
+            "GStrv": "[str]",
+            "GVariant": "GLib.Variant",
+            }
+
+        return fundamental_types.get(name, name)
+
     def format_type(self, type_):
         if isinstance(type_, ast.Array):
             return '[' + self.format_type(type_.element_type) + ']'
@@ -383,7 +407,7 @@ class MallardFormatterPython(MallardFormatter):
         elif type_.target_giname is not None:
             return type_.target_giname
         else:
-            return type_.target_fundamental
+            return self.format_fundamental_type(type_.target_fundamental)
 
     def format_function_name(self, func):
         if func.parent is not None:
index be1e239..43f4b56 100644 (file)
@@ -10,7 +10,7 @@
   </info>
   <title>DocExamples.Obj:property-example</title>
 <synopsis><code mime="text/x-python">
-"property-example"             {utf8: gint8}                : Read / Write
+"property-example"             {unicode: gint8}                : Read / Write
 </code></synopsis>
 <p>This is an example of how to document a property.</p>
 </page>
index 4e394d2..4f56bae 100644 (file)
@@ -11,7 +11,7 @@
   </info>
   <title>DocExamples.Obj.vfunc</title>
 <synopsis><code mime="text/x-python">
-@accepts(gint)
+@accepts(int)
 @returns(none)
 def do_vfunc(self, first_arg):
 </code></synopsis>
index 8447be6..ab20921 100644 (file)
@@ -10,7 +10,7 @@
     <link type="guide" xref="DocExamples.Obj" group="method"/>
     <api:function>
       <api:returns>
-        <api:type>gboolean</api:type>
+        <api:type>bool</api:type>
       </api:returns>
       <api:name>doc_examples_obj_method</api:name>
       <api:arg>
         <api:name>self</api:name>
       </api:arg>
       <api:arg>
-        <api:type>gint</api:type>
+        <api:type>int</api:type>
         <api:name>first_arg</api:name>
       </api:arg>
       <api:arg>
-        <api:type>gfloat</api:type>
+        <api:type>float</api:type>
         <api:name>second_arg</api:name>
       </api:arg>
       <api:arg>
-        <api:type>gboolean</api:type>
+        <api:type>bool</api:type>
         <api:name>boolean_arg</api:name>
       </api:arg>
       <api:arg>
         <api:name>pointer_arg</api:name>
       </api:arg>
       <api:arg>
-        <api:type>utf8</api:type>
+        <api:type>unicode</api:type>
         <api:name>string</api:name>
       </api:arg>
     </api:function>
   </info>
   <title>method</title>
 <synopsis><code mime="text/x-python">
-@accepts(gint, gfloat, gboolean, gpointer, utf8)
-@returns(gboolean)
+@accepts(int, float, bool, gpointer, unicode)
+@returns(bool)
 def method(self, first_arg, second_arg, boolean_arg, pointer_arg, string):
     # Python wrapper for doc_examples_obj_method()
 </code></synopsis>
index 1ace519..35c6c7b 100644 (file)
     <link type="guide" xref="DocExamples.Obj" group="function"/>
     <api:function>
       <api:returns>
-        <api:type>gboolean</api:type>
+        <api:type>bool</api:type>
       </api:returns>
       <api:name>doc_examples_obj_static_method</api:name>
       <api:arg>
-        <api:type>gint</api:type>
+        <api:type>int</api:type>
         <api:name>out_arg</api:name>
       </api:arg>
     </api:function>
   </info>
   <title>static_method</title>
 <synopsis><code mime="text/x-python">
-@accepts(gint)
-@returns(gboolean)
+@accepts(int)
+@returns(bool)
 def static_method(out_arg):
     # Python wrapper for doc_examples_obj_static_method()
 </code></synopsis>
index 3a0d054..dd686f8 100644 (file)
     <link type="guide" xref="index" group="function"/>
     <api:function>
       <api:returns>
-        <api:type>[gint]</api:type>
+        <api:type>[int]</api:type>
       </api:returns>
       <api:name>doc_examples_array_function</api:name>
       <api:arg>
-        <api:type>gint</api:type>
+        <api:type>int</api:type>
         <api:name>out_len</api:name>
       </api:arg>
     </api:function>
   </info>
   <title>array_function</title>
 <synopsis><code mime="text/x-python">
-@accepts(gint)
-@returns([gint])
+@accepts(int)
+@returns([int])
 def array_function(out_len):
     # Python wrapper for doc_examples_array_function()
 </code></synopsis>