in scriptators/python/pythonnator_single.cpp [76:220]
TyErrorId initialize(AnnotatorContext &ac) {
// PyObject *main_module, *user_module;
PyObject *user_module;
PyObject *dict;
swig_type_info *ac_type;
#ifdef PATH_MAX
char srcfile[PATH_MAX + 256];
#else
char srcfile[1000 + 256];
#endif
for (unsigned int i=0; i<FUNCTIONCOUNT; i++) {
function[i] = 0;
}
debug = 0;
if (ac.isParameterDefined("DebugLevel")) {
ac.extractValue("DebugLevel", debug);
}
if (debug > 100) {
cerr<< MODULENAME ": Initialize - debug=" << debug <<endl;
}
if (!ac.isParameterDefined("SourceFile")) {
cerr<< MODULENAME ": Missing Python SourceFile" <<endl;
return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
}
UnicodeString fn;
ac.extractValue(UnicodeString("SourceFile"), fn);
if (fn == "") {
cerr<< MODULENAME ": Empty Python SourceFile" <<endl;
return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
}
UErrorCode err = U_ZERO_ERROR;
fn.extract(srcfile,sizeof(srcfile),0,err);
if (U_FAILURE(err)) {
cerr << MODULENAME ": Unable to extract parameter, got " << u_errorName(err) << endl;
return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
}
#ifdef REBIND_PYTHON_SO
if (refcount == 0)
library = dlopen(PYTHONLIBRARYNAME , RTLD_LAZY | RTLD_GLOBAL);
if (library == 0) {
cerr<< MODULENAME ": unable to bind to python library " <<
PYTHONLIBRARYNAME <<endl;
cerr<< MODULENAME ": dlerror reports " << dlerror() << endl;
return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
}
#endif
if (refcount++ == 0) {
// This is done here because older versions of UIMA cannot be counted on to properly
// initialize ICU, and this is necessary for thread safety
UErrorCode status = U_ZERO_ERROR;
u_init(&status);
if (U_FAILURE(status)) {
cerr<< MODULENAME ": ICU library reports failure to initialize" << endl;
return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
}
Py_Initialize();
}
// main_module = PyImport_AddModule("__main__");
if (PyImport_ImportModule("pythonnator") == 0) {
cerr << MODULENAME ":" << fn << ": failed to import pythonnator module, PYTHONPATH problem? " <<endl;
return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
}
user_module = PyImport_ImportModule(srcfile);
if (user_module == 0) {
cerr << MODULENAME ": could not import python module " << srcfile <<endl;
return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
}
dict = PyModule_GetDict(user_module);
for (unsigned int i=0; i < FUNCTIONCOUNT; i++) {
function[i] = PyDict_GetItemString(dict, function_name[i] );
if (function[i]) {
Py_INCREF(function[i]);
if (debug>100) {
cerr << MODULENAME ": Registered function " <<
function_name[i] << endl;
}
} else {
if (debug>100) {
cerr << MODULENAME ": no function registered for " <<
function_name[i] << endl;
}
}
}
// convert cas and rs to python variables (parameters)
swig_module_info *module = SWIG_Python_GetModule(NULL);
if (!module) {
cerr << MODULENAME ": could not get Python swig module" << endl;
return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
}
ts_type = SWIG_TypeQueryModule(module,module, "_p_TypeSystem");
if (!ts_type) {
cerr << MODULENAME ": could lookup TypeSystem type in swig" << endl;
return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
}
ac_type = SWIG_TypeQueryModule(module,module, "_p_AnnotatorContext");
if (!ac_type) {
cerr << MODULENAME ": could lookup AnnotatorContext type in swig" << endl;
return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
}
cas_type = SWIG_TypeQueryModule(module,module, "_p_CAS");
if (!cas_type) {
cerr << MODULENAME ": could lookup cas type in swig" << endl;
return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
}
// tcas_type = SWIG_TypeQueryModule(module,module, "_p_TCAS");
// if (!tcas_type) {
// cerr << MODULENAME ": could lookup tcas type in swig" << endl;
// return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
// }
rs_type = SWIG_TypeQueryModule(module,module,
"_p_ResultSpecification");
if (!rs_type) {
cerr << MODULENAME ": could lookup rs type in swig" << endl;
return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
}
if (function[FUNCTION_INITIALIZE]) {
PyObject *arg1 =
SWIG_Python_NewPointerObj(NULL,
reinterpret_cast<void *>( &ac),
ac_type, 0);
PyObject *rv = PyObject_CallFunctionObjArgs(
function[FUNCTION_INITIALIZE], arg1, NULL);
Py_DECREF(arg1);
if (rv == 0) {
cerr << MODULENAME ": python error " << endl;
PyErr_Print();
return UIMA_ERR_USER_ANNOTATOR_COULD_NOT_INIT;
}
Py_DECREF(rv);
}
return UIMA_ERR_NONE;
}