#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <skunk_query.h>

#include "pyskunk.h"

PyObject *skunk_open(PyObject *self, PyObject *args)
{
	sdbObject *ob;
	char *fn;
	int fd;

	if (!PyArg_ParseTuple(args, "s", &fn)) {
		return NULL;
	}

	if ( (fd=open(fn, O_RDONLY))<0 ) {
		return NULL;
	}

	if ( !(ob=PyObject_NEW(sdbObject, &skunkdbType)) ) {
		close(fd);
		return NULL;
	}
	
	if ( !sdbq_init(&ob->sdb, fd) ) {
		close(fd);
		Py_DECREF(ob);
		return NULL;
	}
	
	return (PyObject *)ob;
}

PyMethodDef SkunkMethods[]={
	{"open", (PyCFunction)skunk_open, METH_VARARGS, "Open a database"},
	{NULL, NULL, 0, NULL}
};

void initskunk(void)
{
	Py_InitModule("skunk", SkunkMethods);
	skunkdbType.ob_type=&PyType_Type;
}
