[120682]: src / hugin_script_interface / hpi_classes.cpp  Maximize  Restore  History

Download this file

193 lines (175 with data), 4.9 kB

  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
/** @file hpi_classes.cpp
*
* @brief core classes of the hpi interface, not for user code
*
* @author Kay F. Jahnke
*
* hpi_classes.h defines the Python interface. It's not meant to
* be included by hugin code except for hpi.cpp, which holds
* the single instance of class python_interface and provides
* a function to call hpi plugins.
* the classes here define more functionality than is actually
* used currently, but this does no harm.
*/
/* 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 software 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 software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* KFJ 2011-01-18
*
*/
#include "hpi_classes.h"
namespace hpi
{
PyObject* python_interface::load_module ( const char* name )
{
#ifdef HPI_VERBOSE
fprintf ( stdout , "HPI: loading module %s\n" , name ) ;
#endif
PyObject* pModule = PyImport_ImportModule ( name );
if ( ! pModule )
{
PyErr_Print () ;
fprintf ( stderr , "HPI ERROR: Failed to load module %s\n" , name );
return NULL ;
}
return pModule;
}
python_interface::~python_interface()
{
if ( activated ) // the interface was used, do cleanup
{
#ifdef HPI_VERBOSE
fprintf ( stdout , "HPI: finalizing Python\n" ) ;
#endif
// cleaning up
Py_XDECREF ( hsi_module ) ;
Py_XDECREF ( hpi_module ) ;
Py_Finalize();
}
}
bool python_interface::activate()
{
if ( activated )
{
return true ;
}
#ifdef HPI_VERBOSE
fprintf ( stdout , "HPI: initializing Python\n" ) ;
#endif
Py_Initialize();
// load hsi module
hsi_module = load_module ( "hsi" ) ;
if ( hsi_module )
{
// load hpi module
hpi_module = load_module ("hpi");
if ( hpi_module )
{
activated = true ;
}
else
{
Py_DECREF ( hsi_module ) ;
}
}
if ( ! activated )
{
Py_Finalize() ;
}
return activated ;
}
int python_interface::call_hpi ( const char* hpi_func ,
PyObject* pArgs )
{
int result = -1 ;
#ifdef HPI_VERBOSE
fprintf ( stdout , "HPI: calling %s\n" , hpi_func ) ;
#endif
// look up the desired function in the hpi module's namespace
PyObject* pFunc = PyObject_GetAttrString ( hpi_module , hpi_func ) ;
// if it's found and can be called, go ahead call it
if ( pFunc && PyCallable_Check ( pFunc ) )
{
PyObject* pValue = PyObject_CallObject ( pFunc , pArgs ) ;
if ( pValue != NULL )
{
// the function should have returned an integer
result = PyInt_AsLong ( pValue ) ;
Py_DECREF(pValue);
}
}
Py_XDECREF ( pFunc ) ;
#ifdef HPI_VERBOSE
fprintf ( stdout , "HPI: call returns: %d\n" , result );
#endif
return result ;
};
PyObject* python_arglist::make_hsi_object ( const char* hsi_type ,
void* hugin_value )
{
swig_type_info* swigtype = SWIG_Python_TypeQuery ( hsi_type );
#ifdef HPI_VERBOSE
fprintf ( stdout ,
"HPI: making a %s from %p\n" ,
hsi_type ,
hugin_value );
#endif
if ( ! swigtype )
{
fprintf ( stderr,
"HPI ERROR: can't find SWIG type for %s\n" ,
hsi_type );
return NULL;
}
// we have managed to gain the SWIG type information,
// se we can generate the Python object
return SWIG_NewPointerObj ( hugin_value , swigtype , 0 );
};
python_arglist::python_arglist ( int _argc ) : argc ( _argc ) , have ( 0 )
{
pArgs = PyTuple_New ( argc );
}
python_arglist::~python_arglist()
{
Py_DECREF ( pArgs ) ;
}
bool python_arglist::add ( PyObject* python_arg )
{
if ( ( ! python_arg ) || ( have >= argc ) )
{
return false ;
}
PyTuple_SetItem ( pArgs , have++ , python_arg ) ;
return true ;
}
bool python_arglist::add ( const char* str )
{
#ifdef HPI_VERBOSE
fprintf ( stdout , "HPI: making a PyString from '%s'\n" , str ) ;
#endif
#if PY_MAJOR_VERSION>=3
return add ( PyUnicode_FromString(str));
#else
return add ( PyString_FromString(str));
#endif
}
PyObject* python_arglist::yield()
{
if ( argc != have )
{
return NULL ;
}
return pArgs ;
}
} //namespace

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks