Just some stuff to get you started on a new library.
This is a complete, minimal example of an introspectable library based on the GObject reference manual example designed to be the basis of a new project.
It notably includes a working autotools build system that creates a gir and typelib to expose bindings to scripting languages.
For a complete explanation of this project, see the blog post I wrote about it.
This project contains three definitions:
-
A simple class called MamanBar that is final (i.e. cannot be derived from) and has one public method and some internal state.
-
A non-instantiable interface called MamanIbaz which specifies two methods and one property.
-
A derivable (can be subclassed) class called MamanBaz which implements the above interface, and also adds a virtual method and a signal.
Run autogen.sh
to initialize the build system and configure the project. Then run make
and the project will build in the maman
directory.
To set up a development environment for the bindings, set these environment variables:
export GI_TYPELIB_PATH=/path/to/gi-library-boilerplate/maman:$GI_TYPELIB_PATH
export LD_LIBRARY_PATH=/path/to/gi-library-boilerplate/maman/.libs:$LD_LIBRARY_PATH
Then with the PyGObject module available, the library can be accessed like this:
#!/usr/bin/env python3
from gi.repository import Maman
# Create a new object
bar = Maman.Bar()
# Say hello to bar
words = bar.speak('Hello, world!')
print(words)
# Ibaz is an interface which cannot be instantiated
# ibaz = Maman.Ibaz()
# NotImplementedError: Ibaz can not be constructed
# But we can instantiate an implementation of it
baz = Maman.Baz()
# And call methods that are defined in the interface
baz.do_action()
# And also set and get properties
baz.set_property("name","papan")
print (baz.get_property("name"))
# Connect to a signal
baz.connect("changed",lambda baz, frobs: print ("signal received from %s: %d" % (baz, frobs)))
# And make a call that will trigger the signal:
# signal received from <Baz object at 0x7f41a121f1f8 (MamanBaz at 0x26376d0)>: 42
baz.handle_frob(42)
This work is available under the GNU General Public License (See COPYING).
Copyright © 2014, Tony Crisci
(You can use your own copyright and license of course)