detly
detly

Reputation: 30342

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

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.)

Upvotes: 5

Views: 971

Answers (2)

Mark Dickinson
Mark Dickinson

Reputation: 30601

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.

Upvotes: 3

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95589

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.

Upvotes: 3

Related Questions