Added Python bindings, for real this time!

git-svn-id: svn://svn.savannah.nongnu.org/lash/trunk@62 1de19dc7-4e3f-0410-a61d-eddf686bf0b7
This commit is contained in:
Dave Robillard 2007-04-09 01:16:13 +00:00
parent 405a616f2f
commit e84150bcc4
5 changed files with 285 additions and 0 deletions

37
pylash/Makefile.am Normal file
View File

@ -0,0 +1,37 @@
#
# Copyright (C) 2007 Nedko Arnaudov <nedko@arnaudov.name>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
include $(top_srcdir)/common.am
pkgpyexecdir = $(pythondir)
pkgpyexec_LTLIBRARIES = _lash.la
INCLUDES = $(PYTHON_INCLUDES)
_lash_la_LDFLAGS = -module -avoid-version ../liblash/liblash.la
_lash_la_SOURCES = lash.c lash.h
BUILT_SOURCES = lash_wrap.c
pkgpyexec_SCRIPTS = lash.py
CLEANFILES = lash_wrap.c lash.py lash.pyc zynjacku.defs
EXTRA_DIST = test.py lash.i
lash_wrap.c lash.py: lash.i lash.h
$(SWIG) -o lash_wrap.c -I$(top_srcdir) -python $(top_srcdir)/$(subdir)/lash.i

29
pylash/lash.c Normal file
View File

@ -0,0 +1,29 @@
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*****************************************************************************
*
* Python bindings for LASH
*
* Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*****************************************************************************/
#include "lash/lash.h"
lash_client_t * init(int * argc, char *** argv, const char * client_class, int client_flags)
{
return lash_init(lash_extract_args(argc, argv), client_class, client_flags, LASH_PROTOCOL_VERSION);
}

29
pylash/lash.h Normal file
View File

@ -0,0 +1,29 @@
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*****************************************************************************
*
* Python bindings for LASH
*
* Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*****************************************************************************/
#ifndef LASH_H__74601D15_FF3A_4086_B6F6_8D626BBCD7A2__INCLUDED
#define LASH_H__74601D15_FF3A_4086_B6F6_8D626BBCD7A2__INCLUDED
lash_client_t * init(int * argc, char *** argv, const char * client_class, int client_flags);
#endif /* #ifndef LASH_H__74601D15_FF3A_4086_B6F6_8D626BBCD7A2__INCLUDED */

135
pylash/lash.i Normal file
View File

@ -0,0 +1,135 @@
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*****************************************************************************
*
* Python bindings for LASH
*
* Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*****************************************************************************/
#ifndef LASH_I__74601D15_FF3A_4086_B6F6_8D626BBCD7A2__INCLUDED
#define LASH_I__74601D15_FF3A_4086_B6F6_8D626BBCD7A2__INCLUDED
#ifdef SWIG
%module lash
%typemap(in) (int *argc, char ***argv)
{
int i;
if (!PyList_Check($input))
{
PyErr_SetString(PyExc_ValueError, "Expecting a list");
goto fail;
}
$1 = (int *) malloc(sizeof(int));
if ($1 == NULL)
{
PyErr_SetString(PyExc_ValueError, "malloc() failed.");
goto fail;
}
*$1 = PyList_Size($input);
$2 = (char ***) malloc(sizeof(char **));
if ($2 == NULL)
{
PyErr_SetString(PyExc_ValueError, "malloc() failed.");
goto fail;
}
*$2 = (char **) malloc((*$1+1)*sizeof(char *));
if (*$2 == NULL)
{
PyErr_SetString(PyExc_ValueError, "malloc() failed.");
goto fail;
}
for (i = 0; i < *$1; i++)
{
PyObject *s = PyList_GetItem($input,i);
if (!PyString_Check(s))
{
PyErr_SetString(PyExc_ValueError, "List items must be strings");
goto fail;
}
(*$2)[i] = PyString_AsString(s);
}
(*$2)[i] = 0;
}
%typemap(argout) (int *argc, char ***argv)
{
int i;
for (i = 0; i < *$1; i++)
{
PyObject *s = PyString_FromString((*$2)[i]);
PyList_SetItem($input,i,s);
}
while (i < PySequence_Size($input))
{
PySequence_DelItem($input,i);
}
}
%typemap(freearg) (int *argc, char ***argv)
{
if ($1 != NULL)
{
//printf("freeing argc pointer storage\n");
free($1);
$1 = NULL;
}
if ($2 != NULL)
{
if (*$2 != NULL)
{
//printf("freeing array pointed by argv pointer storage\n");
free(*$2);
}
//printf("freeing argv pointer storage\n");
free($2);
$1 = NULL;
}
}
%{
#include <lash/client_interface.h>
#include <lash/types.h>
#include <lash/event.h>
#include <lash/config.h>
#include "lash.h"
typedef unsigned char * uuid_t_compat;
#define uuid_t uuid_t_compat
%}
#endif
%include <lash/client_interface.h>
%include <lash/types.h>
%include <lash/event.h>
%include <lash/config.h>
%include <lash.h>
#endif /* #ifndef LASH_I__74601D15_FF3A_4086_B6F6_8D626BBCD7A2__INCLUDED */

55
pylash/test.py Normal file
View File

@ -0,0 +1,55 @@
#!/usr/bin/env python
#
# This file shows example usage of python bindings for LASH
# As such code here is public domain you can use it as you wish and in
# particular use this code as template for adding LASH support to your program.
#
import sys
import time
import lash
def lash_check_events(lash_client):
event = lash.lash_get_event(lash_client)
while event:
print repr(event)
event_type = lash.lash_event_get_type(event)
if event_type == lash.LASH_Quit:
print "LASH ordered quit."
return False
elif event_type == lash.LASH_Save_File:
print "LASH ordered to save data in directory %s" % lash.lash_event_get_string(event)
lash.lash_send_event(lash_client, event)
elif event_type == lash.LASH_Save_Data_Set:
print "LASH ordered to save data"
lash.lash_send_event(lash_client, event)
elif event_type == lash.LASH_Restore_Data_Set:
print "LASH ordered to restore data"
lash.lash_event_destroy(event)
elif event_type == lash.LASH_Restore_File:
print "LASH ordered to restore data from directory %s" % lash.lash_event_get_string(event)
lash.lash_event_destroy(event)
else:
print "Got unhandled LASH event, type " + str(event_type)
lash.lash_event_destroy(event)
return True
event = lash.lash_get_event(lash_client)
return True
# sys.argv is modified by this call
lash_client = lash.init(sys.argv, "pylash test", lash.LASH_Config_Data_Set | lash.LASH_Terminal)
print "Successfully connected to LASH server at " + lash.lash_get_server_name(lash_client)
# Send our client name to server
lash_event = lash.lash_event_new_with_type(lash.LASH_Client_Name)
lash.lash_event_set_string(lash_event, "pylash test")
lash.lash_send_event(lash_client, lash_event)
# loop until we receive quit order from LASH server
while lash_check_events(lash_client):
time.sleep(1)