using Python with GObject Introspection

I was recently working on one small tool for Midgard Project,and had to deal with a new framework:PyGI. Strictly speaking,it’s not “totally new”,but it is:a) new for me b) just starts to get attention from application developers.

PyGI is a project,which implements dynamic bindings to GObject based libraries for Python using GObject Introspection. Initially,it was a separate project,these days it is merged into main PyGObect. If you read my previous posts,this is kinda what we want to implement for PHP in GObject for PHP project,but for Python.

For the project,I used Python 3. This choice led to the requirement of installing latest versions of software,but the good news is,that coming Ubuntu Natty has a good initial set of software. So,I had to install:

The main library,I worked with — libmidgard2 — supports GObject introspection,so I didn’t need to install anything python-related to make it work.

Ok. Here are some hints on coding using PyGI.

  # to use introspection-friendly library use import statement similar to this  from gi.repository import Midgard  # global functions are available directly in imported package  Midgard.init()  # constructors are ALWAYS called with "named" parameters  config = Midgard.Config(dbtype = "SQLite",database = "testdb")  # library errors are thrown as exceptions  import gobject  try: do_something()  except gobject.GError as e: print(e.message)  # want to know name of objects GType?  print(obj.__class__.__gtype__.name)  # want to get list of non-object properties of some GObject?  property_names = [pspec.name for pspec in obj.props if not pspec.value_type.is_classed()]  # need to get names of all classes,which inherit from your GObject's class?  # (note:Midgard.Object is the class to be replaced by your class)  child_names = [gtype.name for gtype in Midgard.Object.__gtype__.children]
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • DZone
  • FriendFeed
  • Reddit
  • Tumblr
  • Twitter
Liked this post? Follow me on twitter:@jimi_dini.

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.