On Wed, 15 Mar 2006 09:52:13 -0500
solar <[EMAIL PROTECTED]> wrote:

> On Wed, 2006-03-15 at 10:51 +0100, Paul de Vrieze wrote:
> 
> [snip]
> 
> > Actually, C++ being strongly related to C, can just use the C
> > python api's. As such it could directly interface with python, and
> > use the python portage api.
> 
> If you could demonstrate an 'import portage' and a simple
> printf("PORTDIR=%s\n", PORTDIR); in C I'd be highly interested.

Try attached :)  Had a go 'coz I was curious.  Compile with:

gcc -o getportdir getportdir.c -lpython2.4

Does the equivalent of:

#!/usr/bin/portage
import portage;
print portage.settings["PORTDIR"];

(more or less).  Docs on the API itself (which comes with Python) are at
http://docs.python.org/api/api.html

-- 
Kevin F. Quinn
#include <python2.4/Python.h>

#include <stdio.h>

int main(int argc, char **argv) {
	PyObject *portage, *portage_const, *portdir, *portage_dict, *portage_const_dict, portage_db;
	PyObject *portage_settings, *portage_settings_dict;

	PyObject *key, *value;
	int pos;

	Py_Initialize();

	portage = PyImport_ImportModule ("portage");

	portage_dict = PyModule_GetDict (portage);
	
	portage_settings = PyDict_GetItemString (portage_dict, "settings");
	// it's an instance of a class

	portdir = PyObject_GetItem (portage_settings, PyString_FromString("PORTDIR"));
	if (portdir == NULL) {
		fprintf(stderr, "Failed to find portdir\n");
	} else {
		fprintf(stdout, "PORTDIR=\"%s\"\n", PyString_AsString(portdir)); fflush(stdout);
	}

	Py_Finalize();
}

Attachment: signature.asc
Description: PGP signature

Reply via email to