src/PythonHandler.h
branchv_0
changeset 12 ee69be2212fa
parent 11 5e0b317f4100
child 13 df354b680b89
equal deleted inserted replaced
11:5e0b317f4100 12:ee69be2212fa
    67 		relationalWriter.reset(writer::Factory::create(output));
    67 		relationalWriter.reset(writer::Factory::create(output));
    68 
    68 
    69 		pythonProgramName = Py_DecodeLocale("relpipe-tr-python", NULL);
    69 		pythonProgramName = Py_DecodeLocale("relpipe-tr-python", NULL);
    70 		Py_SetProgramName(pythonProgramName);
    70 		Py_SetProgramName(pythonProgramName);
    71 		Py_Initialize();
    71 		Py_Initialize();
    72 		
    72 
    73 		if (arguments.size() == 2) {
    73 		if (arguments.size() == 2) {
    74 			relationNameRegEx = wregex(arguments[0]);
    74 			relationNameRegEx = wregex(arguments[0]);
    75 			pythonCode = arguments[1];
    75 			pythonCode = arguments[1];
    76 		} else {
    76 		} else {
    77 			PyMem_RawFree(pythonProgramName);
    77 			PyMem_RawFree(pythonProgramName);
   101 		if (filterCurrentRelation) {
   101 		if (filterCurrentRelation) {
   102 			currentRecord[currentAttributeIndex] = value;
   102 			currentRecord[currentAttributeIndex] = value;
   103 			currentAttributeIndex++;
   103 			currentAttributeIndex++;
   104 
   104 
   105 			if (currentAttributeIndex > 0 && currentAttributeIndex % currentRecord.size() == 0) {
   105 			if (currentAttributeIndex > 0 && currentAttributeIndex % currentRecord.size() == 0) {
       
   106 
       
   107 				PyObject* pyModule;
       
   108 				PyObject* pyDict;
       
   109 				PyObject* pyWhere;
       
   110 
   106 				PyRun_SimpleString(convertor.to_bytes(pythonCode).c_str());
   111 				PyRun_SimpleString(convertor.to_bytes(pythonCode).c_str());
       
   112 				
       
   113 				// FIXME: check Python error and throw exception
       
   114 				// if (PyErr_Occurred()) throw cli::RelpipeCLIException(L"Python code failed.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: review exit code
       
   115 				// if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); }
       
   116 
       
   117 				pyModule = PyImport_AddModule((char*) "__main__"); // FIXME: variable and Py_DecodeLocale ?
       
   118 				pyDict = PyModule_GetDict(pyModule);
       
   119 				pyWhere = PyDict_GetItemString(pyDict, "WHERE");
       
   120 
       
   121 				if (pyWhere) includeCurrentRecord = PyLong_AsLong(pyWhere);
       
   122 
       
   123 
   107 				if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v);
   124 				if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v);
   108 				includeCurrentRecord = true;
   125 				includeCurrentRecord = true;
   109 			}
   126 			}
   110 
   127 
   111 			currentAttributeIndex = currentAttributeIndex % currentRecord.size();
   128 			currentAttributeIndex = currentAttributeIndex % currentRecord.size();