KEMBAR78
Python and GObject Introspection | PDF
Python and GObject-Introspection

     Yuren Ju <yurenju@gmail.com>
About me
●
    http://about.me/yurenju
●
    Hacking Thursday
●
    KaLUG
●
    Twitter: @yurenju
GObject
D-BUS
  notify




Glade
           GTK   VTE
GNOME platform
Binding



 Library
based on GObject
Use codegen to generate GTK C Wrapper for Python




        gtk.c number of lines:

         128,469
If GTK+ add APIs in new version,
python-gtk also need to upgrade for new APIs.
Implement in Python,
Also need to re-implement in Ruby, Perl, Java,
               Javscript again.
GObject-Introspection
Original

python-gobject
   metadata




    GTK+
     GTK+
GObject-Introspection

  python-gobject
       parser


      metadata
      GTK+
       GTK+
Same metadata format



           python-gobject
                parser


metadata       metadata     metadata
GTK+GTK+      gstreamer
                     GTK+      ... GTK+
Parser + metadata = GObject-Introspection
tut-triangle

  tut.triangle
+ get_color()
+ set_color()
+ get_centroid()
+ get_centroidu()
Metadata

Comments in Source code

               g-ir-scanner

      Gir XML file

               g-ir-compiler


   Typelib binary file
Comments in Source code




http://live.gnome.org/GObjectIntrospection/Annotations
Gir XML file
Metadata

                  Comments in Source code

                                 g-ir-scanner

                        Gir XML file

                                 g-ir-compiler


libgirepository      Typelib binary file
python-gobject
PyGTK 2.24.0 has been released. This is a stable release supporting the GTK+
2.24 API. New users wishing to develop Python applications using GTK+ are
recommended to use the GObject-Introspection features available in
PyGObject. Existing authors of PyGTK applications are also recommended to
port their applications to PyGObject to take advantage of new features
appearing in GTK-3 and beyond.
gtk.c number of lines:

128,469
How to use GI?
from gi.repository import Gtk;
PEP-0302: New Import Hooks



        Append finder to sys.meta_path,
Use finder.find_module() dynamic to find module.
Load module
●
    find_module()
●
    load_module()
    ●
        Create DynamicModule
    ●
        Module._load()
        –   Create self._introspection_module
             ●
                 IntrospectionModule => repository.requre to load typelib
Object
from gi.repository import Gtk
                                                - Widget
                                                - Container
Initial all items in Gtk                        - Window


Get info from                                        ...etc.
repository.find_by_name()


If item is a object instance, Use a MetaClass
“GobjectMeta” to create a class wrapper




Finally, you have a Python Class to create Object!
Method – relative easy

      object.method()




      Call info.invoke()




   g_function_info_invoke
Summary
●
    GI Migrate metadata from language-binding to
    library
●
    python-gobject let you use GTK, GObject,
    Gstreamer, VTE, Dbus,...etc.
一人成佛,雞犬升天

Python and GObject Introspection