From 7d823fc736cc3280c04c47888a2a2908a3eb2ba3 Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Fri, 8 Feb 2013 00:30:57 +0100 Subject: [PATCH] Initial import for the Python bindings (incomplete) --- Makefile | 4 ++ src/project.conf | 2 +- src/python/Makefile | 52 ++++++++++++++++++++++++ src/python/libDatabase.c | 85 +++++++++++++++++++++++++++++++++++++++ src/python/libDatabase.py | 25 ++++++++++++ src/python/project.conf | 15 +++++++ 6 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 src/python/Makefile create mode 100644 src/python/libDatabase.c create mode 100644 src/python/libDatabase.py create mode 100644 src/python/project.conf diff --git a/Makefile b/Makefile index f07b3d5..08ab90c 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,10 @@ dist: $(PACKAGE)-$(VERSION)/include/Database/project.conf \ $(PACKAGE)-$(VERSION)/src/database.c \ $(PACKAGE)-$(VERSION)/src/Makefile \ + $(PACKAGE)-$(VERSION)/src/python/project.conf \ + $(PACKAGE)-$(VERSION)/src/python/Makefile \ + $(PACKAGE)-$(VERSION)/src/python/libDatabase.c \ + $(PACKAGE)-$(VERSION)/src/python/libDatabase.py \ $(PACKAGE)-$(VERSION)/src/project.conf \ $(PACKAGE)-$(VERSION)/src/engines/pdo.c \ $(PACKAGE)-$(VERSION)/src/engines/pgsql.c \ diff --git a/src/project.conf b/src/project.conf index 8062a84..0d0f53a 100644 --- a/src/project.conf +++ b/src/project.conf @@ -3,7 +3,7 @@ targets=libDatabase cppflags_force=-I ../include cflags_force=-W -fPIC cflags=-Wall -g -O2 -pedantic -dist=Makefile +dist=Makefile,python/project.conf,python/Makefile,python/libDatabase.c,python/libDatabase.py [libDatabase] type=library diff --git a/src/python/Makefile b/src/python/Makefile new file mode 100644 index 0000000..c8b5bfb --- /dev/null +++ b/src/python/Makefile @@ -0,0 +1,52 @@ +MKDIR = mkdir -m 0755 -p +INSTALL = install +RM = rm -f +TARGETS = _libDatabase.so +PREFIX = /usr/local +DESTDIR = +LIBDIR = $(PREFIX)/lib +CC = cc +CPPFLAGSF= -I ../../include +CPPFLAGS= +CFLAGSF = -W `pkg-config --cflags python-2.7 libDatabase` -fPIC +CFLAGS = -Wall -g -O2 +LDFLAGSF= `pkg-config --libs python-2.7 libDatabase` +LDFLAGS = -L.. -L$(LIBDIR) -Wl,-rpath,$(LIBDIR) +AR = ar +RANLIB = ranlib +CCSHARED= $(CC) -shared +RM = rm -f +LN = ln -f +MKDIR = mkdir -m 0755 -p +INSTALL = install + + +all: $(TARGETS) + +_libDatabase_OBJS = libDatabase.o +_libDatabase_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) +_libDatabase_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) + +_libDatabase.so: $(_libDatabase_OBJS) + $(CCSHARED) -o _libDatabase.so $(_libDatabase_OBJS) $(_libDatabase_LDFLAGS) + +libDatabase.o: libDatabase.c + $(CC) $(_libDatabase_CFLAGS) -c libDatabase.c + +clean: + $(RM) -- $(_libDatabase_OBJS) + +distclean: clean + $(RM) -- $(TARGETS) + +install: $(TARGETS) + $(MKDIR) $(DESTDIR)$(PREFIX)/lib/python2.7/site-packages + $(INSTALL) -m 0644 -- _libDatabase.so $(DESTDIR)$(PREFIX)/lib/python2.7/site-packages/_libDatabase.so + $(MKDIR) $(DESTDIR)$(PREFIX)/lib/python2.7/site-packages + $(INSTALL) -m 0644 -- libDatabase.py $(DESTDIR)$(PREFIX)/lib/python2.7/site-packages/libDatabase.py + +uninstall: + $(RM) -- $(DESTDIR)$(PREFIX)/lib/python2.7/site-packages/_libDatabase.so + $(RM) -- $(DESTDIR)$(PREFIX)/lib/python2.7/site-packages/libDatabase.py + +.PHONY: all clean distclean install uninstall diff --git a/src/python/libDatabase.c b/src/python/libDatabase.c new file mode 100644 index 0000000..f4db103 --- /dev/null +++ b/src/python/libDatabase.c @@ -0,0 +1,85 @@ +/* $Id$ */ +/* Copyright (c) 2013 Pierre Pronchery */ +/* This file is part of DeforaOS Database libDatabase */ +/* This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . */ + + + +#include +#include "Database.h" + + +/* libDatabase */ +/* private */ +/* constants */ +static char const _libdatabase_database_name[] = "libDatabase::Database"; + + +/* prototypes */ +/* Database */ +static PyObject * _libdatabase_database_new(PyObject * self, PyObject * args); +static void _libdatabase_database_delete(PyObject * self); + + +/* variables */ +static PyMethodDef _libdatabase_methods[] = +{ + { "database_new", _libdatabase_database_new, METH_VARARGS, + "Instantiates a Database object." }, + { NULL, NULL, 0, NULL } +}; + + +/* public */ +/* prototypes */ +PyMODINIT_FUNC init_libDatabase(void); + + +/* functions */ +/* init_libDatabase */ +PyMODINIT_FUNC init_libDatabase(void) +{ + Py_InitModule("_libDatabase", _libdatabase_methods); +} + + +/* private */ +/* functions */ +/* Database */ +/* libdatabase_database_new */ +static PyObject * _libdatabase_database_new(PyObject * self, PyObject * args) +{ + Database * database; + char const * engine; + char const * section; + + if(!PyArg_ParseTuple(args, "ss", &engine, §ion)) + return NULL; + /* FIXME obtain the Config object */ + if((database = database_new(engine, config_new(), section)) == NULL) + return NULL; + return PyCapsule_New(database, _libdatabase_database_name, + _libdatabase_database_delete); +} + + +/* libdatabase_database_delete */ +static void _libdatabase_database_delete(PyObject * self) +{ + Database * database; + + if((database = PyCapsule_GetPointer(self, _libdatabase_database_name)) + == NULL) + return; + database_delete(database); +} diff --git a/src/python/libDatabase.py b/src/python/libDatabase.py new file mode 100644 index 0000000..58bfa9d --- /dev/null +++ b/src/python/libDatabase.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python2.7 +#$Id$ +#Copyright (c) 2013 Pierre Pronchery +#This file is part of DeforaOS Database libDatabase +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU Lesser General Public License as published by +#the Free Software Foundation, version 3 of the License. +# +#This program is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU Lesser General Public License for more details. +# +#You should have received a copy of the GNU Lesser General Public License +#along with this program. If not, see . + + + +import _libDatabase + + +#Database +class Database: + def __init__(self, engine, section): + self.database = _libDatabase.database_new(engine, section) diff --git a/src/python/project.conf b/src/python/project.conf new file mode 100644 index 0000000..826ccd0 --- /dev/null +++ b/src/python/project.conf @@ -0,0 +1,15 @@ +targets=_libDatabase +cppflags_force=-I ../../include +cflags_force=-W `pkg-config --cflags python-2.7 libDatabase` -fPIC +cflags=-Wall -g -O2 +ldflags_force=`pkg-config --libs python-2.7 libDatabase` +ldflags=-L.. -L$(LIBDIR) -Wl,-rpath,$(LIBDIR) +dist=Makefile,libDatabase.py + +[_libDatabase] +type=plugin +sources=libDatabase.c +install=$(PREFIX)/lib/python2.7/site-packages + +[libDatabase.py] +install=$(PREFIX)/lib/python2.7/site-packages