Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/C "defs" file - what is it?

Tags:

python

c

In the nautilus-python bindings, there is a file "nautilus.defs". It contains stanzas like

(define-interface MenuProvider
  (in-module "Nautilus")
  (c-name "NautilusMenuProvider")
  (gtype-id "NAUTILUS_TYPE_MENU_PROVIDER")
)

or

(define-method get_mime_type
  (of-object "NautilusFileInfo")
  (c-name "nautilus_file_info_get_mime_type")
  (return-type "char*")
)

Now I can see what most of these do (eg. that last one means that I can call the method "get_mime_type" on a "FileInfo" object). But I'd like to know: what is this file, exactly (ie. what do I search the web for to find out more info)? Is it a common thing to find in Python/C bindings? What is the format, and where is it documented? What program actually processes it?

(So far, I've managed to glean that it gets transformed into a C source file, and it looks a bit like lisp to me.)

like image 773
detly Avatar asked Apr 26 '10 08:04

detly


2 Answers

To answer your "What program actually processes it?" question:

From Makefile.in in the src directory, the command that translates the .defs file into C is PYGTK_CODEGEN. To find out what PYGTK_CODEGEN is, look in the top-level configure.in file, which contains these lines:

AC_MSG_CHECKING(for pygtk codegen)
PYGTK_CODEGEN="$PYTHON `$PKG_CONFIG --variable=codegendir pygtk-2.0`/codegen.py"
AC_SUBST(PYGTK_CODEGEN)
AC_MSG_RESULT($PYGTK_CODEGEN)

So the program that processes it is a Python script called codegen.py, that apparently has some link with PyGTK. Now a Google search for PyGTK codegen gives me this link as the first hit, which says:

"PyGTK-Codegen is a system for automatically generating wrappers for interfacing GTK code with Python."

and also gives some examples.

As for: "What is the format, and where is it documented?". As others have said, the code looks a lot like simple Scheme. I couldn't find any documentation at all on codegen on the PyGTK site; this looks like one of those many dark corners of open source that isn't well documented. Your best bet would probably be to download a recent tarball for PyGTK, look through the sources for the codegen.py file and see if the file itself contains sufficient documentation.

like image 132
Mark Dickinson Avatar answered Sep 23 '22 02:09

Mark Dickinson


All you need to create Python bindings for C code is to use the Python / C API. However, the API can be somewhat repetitive and redundant, and so various forms of automation may be used to create them. For example, you may have heard of swig. The LISP-like (Scheme) code that you see is simply a configuration file for PyGTK-Codegen, which is a similar automation program for creating bindings to Python.

like image 20
Michael Aaron Safyan Avatar answered Sep 26 '22 02:09

Michael Aaron Safyan