summaryrefslogtreecommitdiff
path: root/modules/modpy/py.cpp
blob: 51cb38f2482cae9038b409b7102212e66ededd3d (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 *	bitz-server, An ICAP server implementation in C++
 *	Copyright (C) 2012	Uditha Atukorala
 *
 *	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 3 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
 */

#include "py.h"
#include "interface.h"

#include <bitz/config.h>
#include <bitz/logger.h>


namespace bitz {

	Py::Py() : Modifier() {

		// defaults
		_pymodule = NULL;

		// initialise Py::config_t values
		_config.module_name = "modpy";
		_config.module_path = "";

		// load configs
		load_configs();

		// initialise python
		init_python();

	}


	Py::~Py() {

		// cleanup python
		cleanup_python();

	}


	icap::Response * Py::modify( icap::Request * request ) throw() {

		// get the modify response
		return python_response( request, "modify" );

	}


	icap::Response * Py::preview( icap::Request * request ) throw() {

		// get the preview response
		return python_response( request, "preview" );

	}


	void Py::load_configs() throw() {

		std::string s;
		Config &server_config = Config::instance();

		// interface module name
		s = server_config.module_config( "modpy", "module_name" );
		if ( s != "" ) {
			_config.module_name = s;
		}

		// module lookup path
		_config.module_path = server_config.module_config( "modpy", "module_path" );

	}


	void Py::init_python() throw() {

		PyObject * sys_path;
		PyObject * pymodule_path, * pymodule_name;
		PyObject * pymethod;
		PyObject * pyreturn;

		// logger
		Logger &logger = Logger::instance();

		// python core
		Py_Initialize();

		// bitz python module
		if ( Py_InitModule( "bitz", bitz_methods ) == NULL ) {
			logger.warn( "[modpy] failed to init C interface module: bitz" );
		}

		// setup python environment
		if ( _config.module_path != "" ) {

			logger.debug( std::string( "[modpy] appending to sys.path, module_path: " ).append( _config.module_path ) );

			sys_path      = PySys_GetObject( (char *) "path" );
			pymodule_path = PyString_FromString( _config.module_path.c_str() );
			PyList_Append( sys_path, pymodule_path );

		}

		// load the interface module
		logger.debug( std::string( "[modpy] interface module: " ).append( _config.module_name ) );

		pymodule_name = PyString_FromString( _config.module_name.c_str() );
		_pymodule     = PyImport_Import( pymodule_name );

		if ( _pymodule != NULL ) {

			logger.debug( "[modpy] interface module loaded successfully" );

			// call init() in the interface module
			pymethod = PyObject_GetAttrString( _pymodule, "init" );

			if ( pymethod && PyCallable_Check( pymethod ) ) {
				pyreturn = PyObject_CallObject( pymethod, NULL );
				Py_DECREF( pyreturn );
			} else {
				logger.warn ( "[modpy] failed to call init() in interface module" );
			}

			Py_DECREF( pymethod );

		} else {
			logger.warn( "[modpy] failed to load interface module" );
		}


		// cleanup
		Py_DECREF( pymodule_name );
		Py_DECREF( pymodule_path );
		Py_DECREF( sys_path );

	}


	void Py::cleanup_python() throw() {

		PyObject * pymethod;
		PyObject * pyreturn;

		// logger
		Logger &logger = Logger::instance();


		// cleanup
		if ( _pymodule != NULL ) {

			// call cleanup() in the interface module
			pymethod = PyObject_GetAttrString( _pymodule, "cleanup" );

			if ( pymethod && PyCallable_Check( pymethod ) ) {
				pyreturn = PyObject_CallObject( pymethod, NULL );
				Py_DECREF( pyreturn );
			} else {
				logger.warn ( "[modpy] failed to call cleanup() in interface module" );
			}

			Py_DECREF( pymethod );
			Py_DECREF( _pymodule );

		}

		// finalise python core
		Py_Finalize();

	}


	icap::Response * Py::python_response( icap::Request * request, const std::string &method ) throw() {

		/*
		*  notes:
		*    + pass icap::Request to python module as a PyCapsule object
		*      so it can be used to grab the data needed using the C/C++ interface.
		*    + process data (within the python module) and use the C/C++ interface
		*      to create a icap::Response and pass it back to this method
		*      using a PyCapsule object
		*/

		icap::Response * response;

		PyObject * pymethod;
		PyObject * pyargs;
		PyObject * pyrequest, * pyresponse;

		// logger
		Logger &logger = Logger::instance();

		// initialise the response object
		response = NULL;

		// check for the interface module
		if ( _pymodule != NULL ) {

			/* call the [method]() in the interface module */
			pymethod  = PyObject_GetAttrString( _pymodule, method.c_str() );
			pyargs    = PyTuple_New( 1 );
			pyrequest = PyCapsule_New( (void *) request, "request", NULL );
			PyTuple_SetItem( pyargs, 0, pyrequest );

			if ( pymethod && PyCallable_Check( pymethod ) ) {

				// get the response capsule
				pyresponse = PyObject_CallObject( pymethod, pyargs );

				// sanity check
				if ( pyresponse != NULL ) {

					void * p = PyCapsule_GetPointer( pyresponse, "response" );

					// sanity check
					if ( p != NULL ) {

						// construct the response
						response = static_cast<icap::Response *>(p);

					} else {
						logger.warn( std::string( "[modpy] invalid capsule response, method: " ).append( method ) );
					}

					Py_DECREF( pyresponse );

				} else {
					logger.warn( std::string( "[modpy] response is NULL, method: " ).append( method ) );
				}


			} else {
				logger.warn ( std::string( "[modpy] failed to call the method in interface module, method: " ).append( method ) );
			}

			// cleanup
			// pyrequest is created from the reference passed in
			Py_DECREF( pyargs );
			Py_DECREF( pymethod );

		}

		// sanity check
		if ( response == NULL ) {
			response = new icap::Response( icap::ResponseHeader::SERVER_ERROR );
		}

		return response;

	}

} /* end of namespace bitz */