File: make_interfaces.py

package info (click to toggle)
python2.1-libplot 1.0.2-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 256 kB
  • ctags: 447
  • sloc: ansic: 3,224; python: 388; makefile: 71; sh: 8
file content (195 lines) | stat: -rwxr-xr-x 5,276 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
#
# $Id: make_interfaces.py,v 1.2 2000/11/21 04:54:32 mrnolta Exp $
#
gnu_licence = """\
# Copyright (C) 2000 Mike Nolta <mrnolta@users.sourceforge.net>
#
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
"""

import os, re, string, sys

LIBPLOT_VER = sys.argv[1]
LIBPLOT_H = "plot-%s.h"	% LIBPLOT_VER	# library header file (input)
LIBPLOT_I = "_libplot.i"		# swig interface (output)
LIBPLOT_PY = "libplot.py"		# python interface (output)

NAME_CMODULE = "_libplot"
NAME_PYMODULE = "libplot"

###############################################################################

HEADER_I = """\
%module """ + NAME_CMODULE + """
%{
#include <plot.h>
%}

/* allows us to pass Python file objects to pl_newpl_r */
%typemap(python,in) FILE * {
	if ( $source == Py_None )
	{
		$target = NULL;
	}
	else if ( PyFile_Check( $source ) )
	{
		$target = PyFile_AsFile( $source );
	}
	else
	{
		PyErr_SetString( PyExc_TypeError, "not a file" );
		return NULL;
	}
}

/* we assume you only pass strings to pl_setplparam */
%typemap(python,in) void *value {
	if ( !PyString_Check( $source ) )
	{
		PyErr_SetString( PyExc_TypeError, "not a string" );
		return NULL;
	}
	$target = (void *) PyString_AsString( $source );
}

/* Marker symbols */

enum 
{ M_NONE, M_DOT, M_PLUS, M_ASTERISK, M_CIRCLE, M_CROSS, 
  M_SQUARE, M_TRIANGLE, M_DIAMOND, M_STAR, M_INVERTED_TRIANGLE, 
  M_STARBURST, M_FANCY_PLUS, M_FANCY_CROSS, M_FANCY_SQUARE, 
  M_FANCY_DIAMOND, M_FILLED_CIRCLE, M_FILLED_SQUARE, M_FILLED_TRIANGLE, 
  M_FILLED_DIAMOND, M_FILLED_INVERTED_TRIANGLE, M_FILLED_FANCY_SQUARE,
  M_FILLED_FANCY_DIAMOND, M_HALF_FILLED_CIRCLE, M_HALF_FILLED_SQUARE,
  M_HALF_FILLED_TRIANGLE, M_HALF_FILLED_DIAMOND,
  M_HALF_FILLED_INVERTED_TRIANGLE, M_HALF_FILLED_FANCY_SQUARE,
  M_HALF_FILLED_FANCY_DIAMOND, M_OCTAGON, M_FILLED_OCTAGON 
};

/* PlotterParams */

extern plPlotterParams * pl_newplparams (void);
extern int pl_deleteplparams (plPlotterParams *plotter_params);
extern plPlotterParams * pl_copyplparams (const plPlotterParams *plotter_params);
extern int pl_setplparam (plPlotterParams *plotter_params, const char *parameter, void *value);

/* Plotter */

extern plPlotter * pl_newpl_r (const char *type, FILE *infile, FILE *outfile, FILE *errfile, const plPlotterParams *plotter_params);
extern int pl_deletepl_r (plPlotter *plotter);

/* Plotter member functions */
"""

###############################################################################

HEADER_PY = """\
#
# libplot.py
#
""" + gnu_licence + """\

from """ + NAME_CMODULE + """ import *

class LibplotError( Exception ):
	pass

class Plotter:
	def __init__( self, type="X", params=None,
			outfile=None, infile=None, errfile=None ):

		self.plparams = pl_newplparams()
		if params:
			for key,value in params.items():
				pl_setplparam( self.plparams, key, value )

		self.this = pl_newpl_r( type, infile, outfile, errfile, self.plparams )
		if self.this < 0:
			raise LibplotError( "could not create plotter" )

	def __del__( self ):
		pl_deletepl_r( self.this )
		pl_deleteplparams( self.plparams )
"""

###############################################################################

re_pl_name = re.compile( r"pl_(\w+)_r" )
re_pl_cdef = re.compile( r"(.+)\((.+)\)" )

ignore_functions_list = [ "deletepl", "outfile" ]

def is_plotter_method( cdef ):
	word = string.split( cdef )
	if len(word) > 2 and (word[0] == "int" or word[0] == "double"):
		m = re_pl_name.match( word[1] )
		if m:
			return m.group(1) not in ignore_functions_list
	return 0 # false

def swig_def( cdef ):
	return "extern " + cdef

def last_word( str ):
	return string.split( str )[-1]

def python_def( cdef ):
	m = re_pl_cdef.match( cdef )

	cname = last_word( m.group(1) )
	pyname = re_pl_name.match( cname ).group(1)

	args = []
	for x in string.split( m.group(2), "," ):
		args.append( last_word(x) )
	args = args[1:] # strip off *plotter

	pyargs = string.join( ["self"] + args, "," )
	cargs = string.join( ["self.this"] + args, "," )

	pyargs = string.replace( pyargs, "*", "" )
	cargs = string.replace( cargs, "*", "" )

	return """
	def %s(%s):
		return %s(%s)
""" % (pyname, pyargs, cname, cargs)

###############################################################################

def preprocessed( file ):
	return os.popen( "gcc -E " + file )

if __name__ == '__main__':

	fi = open( LIBPLOT_I, 'w' )
	fpy = open( LIBPLOT_PY, 'w' )

	fi.write( HEADER_I )
	fpy.write( HEADER_PY )

	fh = preprocessed( LIBPLOT_H )

	for line in fh.readlines():
		if is_plotter_method( line ):
			fi.write( swig_def(line) )
			fpy.write( python_def(line) )

	fh.close()

	fi.close()
	fpy.close()