Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hpi_classes.cpp
Go to the documentation of this file.
1 
15 /* This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public
17  * License as published by the Free Software Foundation; either
18  * version 2 of the License, or (at your option) any later version.
19  *
20  * This software is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public
26  * License along with this software. If not, see
27  * <http://www.gnu.org/licenses/>.
28  *
29  * KFJ 2011-01-18
30  *
31  */
32 
33 #include "hpi_classes.h"
34 
35 namespace hpi
36 {
37 
38 PyObject* python_interface::load_module ( const char* name )
39 {
40 #ifdef HPI_VERBOSE
41  fprintf ( stdout , "HPI: loading module %s\n" , name ) ;
42 #endif
43  PyObject* pModule = PyImport_ImportModule ( name );
44  if ( ! pModule )
45  {
46  PyErr_Print () ;
47  fprintf ( stderr , "HPI ERROR: Failed to load module %s\n" , name );
48  return NULL ;
49  }
50  return pModule;
51 }
52 
54 {
55  if ( activated ) // the interface was used, do cleanup
56  {
57 #ifdef HPI_VERBOSE
58  fprintf ( stdout , "HPI: finalizing Python\n" ) ;
59 #endif
60  // cleaning up
61  Py_XDECREF ( hsi_module ) ;
62  Py_XDECREF ( hpi_module ) ;
63  Py_Finalize();
64  }
65 }
66 
68 {
69  if ( activated )
70  {
71  return true ;
72  }
73 #ifdef HPI_VERBOSE
74  fprintf ( stdout , "HPI: initializing Python\n" ) ;
75 #endif
76  Py_Initialize();
77  // load hsi module
78  hsi_module = load_module ( "hsi" ) ;
79  if ( hsi_module )
80  {
81  // load hpi module
82  hpi_module = load_module ("hpi");
83  if ( hpi_module )
84  {
85  activated = true ;
86  }
87  else
88  {
89  Py_DECREF ( hsi_module ) ;
90  }
91  }
92  if ( ! activated )
93  {
94  Py_Finalize() ;
95  }
96  return activated ;
97 }
98 
99 int python_interface::call_hpi ( const char* hpi_func ,
100  PyObject* pArgs )
101 {
102  int result = -1 ;
103 #ifdef HPI_VERBOSE
104  fprintf ( stdout , "HPI: calling %s\n" , hpi_func ) ;
105 #endif
106  // look up the desired function in the hpi module's namespace
107  PyObject* pFunc = PyObject_GetAttrString ( hpi_module , hpi_func ) ;
108  // if it's found and can be called, go ahead call it
109  if ( pFunc && PyCallable_Check ( pFunc ) )
110  {
111  PyObject* pValue = PyObject_CallObject ( pFunc , pArgs ) ;
112  if ( pValue != NULL )
113  {
114  // the function should have returned an integer
115  result = PyInt_AsLong ( pValue ) ;
116  Py_DECREF(pValue);
117  }
118  }
119  Py_XDECREF ( pFunc ) ;
120 
121 #ifdef HPI_VERBOSE
122  fprintf ( stdout , "HPI: call returns: %d\n" , result );
123 #endif
124  return result ;
125 };
126 
127 PyObject* python_arglist::make_hsi_object ( const char* hsi_type ,
128  void* hugin_value )
129 {
130  swig_type_info* swigtype = SWIG_Python_TypeQuery ( hsi_type );
131 #ifdef HPI_VERBOSE
132  fprintf ( stdout ,
133  "HPI: making a %s from %p\n" ,
134  hsi_type ,
135  hugin_value );
136 #endif
137  if ( ! swigtype )
138  {
139  fprintf ( stderr,
140  "HPI ERROR: can't find SWIG type for %s\n" ,
141  hsi_type );
142  return NULL;
143  }
144 
145  // we have managed to gain the SWIG type information,
146  // se we can generate the Python object
147  return SWIG_NewPointerObj ( hugin_value , swigtype , 0 );
148 };
149 
150 python_arglist::python_arglist ( int _argc ) : argc ( _argc ) , have ( 0 )
151 {
152  pArgs = PyTuple_New ( argc );
153 }
154 
156 {
157  Py_DECREF ( pArgs ) ;
158 }
159 
160 bool python_arglist::add ( PyObject* python_arg )
161 {
162  if ( ( ! python_arg ) || ( have >= argc ) )
163  {
164  return false ;
165  }
166  PyTuple_SetItem ( pArgs , have++ , python_arg ) ;
167  return true ;
168 }
169 
170 bool python_arglist::add ( const char* str )
171 {
172 #ifdef HPI_VERBOSE
173  fprintf ( stdout , "HPI: making a PyString from '%s'\n" , str ) ;
174 #endif
175 
176 #if PY_MAJOR_VERSION>=3
177  return add ( PyUnicode_FromString(str));
178 #else
179  return add ( PyString_FromString(str));
180 #endif
181 }
182 
184 {
185  if ( argc != have )
186  {
187  return NULL ;
188  }
189  return pArgs ;
190 }
191 
192 } //namespace
bool activated
flag, true if activated
Definition: hpi_classes.h:54
core classes of the hpi interface, not for user code
PyObject * hsi_module
pointer to loaded hsi module
Definition: hpi_classes.h:56
python_arglist(int _argc)
the constructor is called with the number of arguments the argument list is meant to contain...
PyObject * pArgs
Definition: hpi_classes.h:92
PyObject * make_hsi_object(const char *hsi_type, void *hugin_value)
general function to make a Python object from a hugin object.
bool activate()
: loads the necessary modules hsi and hpi.
Definition: hpi_classes.cpp:67
PyObject * yield()
returns the generated PyObject Note that this will only succeed if the argument count is correct...
~python_arglist()
destructor, does cleanup
PyObject * load_module(const char *name)
general module-loading function
Definition: hpi_classes.cpp:38
PyObject * hpi_module
pointer to loaded hpi module
Definition: hpi_classes.h:58
bool add(PyObject *python_arg)
add a python object to the argument list.
int call_hpi(const char *hpi_func, PyObject *pArgs)
call a routine in the hsi module with a bunch of parameters.
Definition: hpi_classes.cpp:99