src/PythonHandler.h
branchv_0
changeset 10 2c8f5e05c7b7
parent 9 ed7ab0a06849
child 11 5e0b317f4100
--- a/src/PythonHandler.h	Thu Dec 13 18:17:36 2018 +0100
+++ b/src/PythonHandler.h	Thu Dec 13 19:12:38 2018 +0100
@@ -26,6 +26,8 @@
 #include <codecvt>
 #include <regex>
 
+#include <Python.h>
+
 #include <relpipe/reader/typedefs.h>
 #include <relpipe/reader/TypeId.h>
 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
@@ -48,6 +50,8 @@
 private:
 	shared_ptr<writer::RelationalWriter> relationalWriter;
 
+	wchar_t* pythonProgramName;
+
 	wregex relationNameRegEx;
 	wregex attributeNameRegEx;
 	wregex searchRegEx;
@@ -63,15 +67,27 @@
 	GrepHandler(ostream& output, const vector<string_t>& arguments) {
 		relationalWriter.reset(writer::Factory::create(output));
 
+		pythonProgramName = Py_DecodeLocale("relpipe-tr-python", NULL);
+		Py_SetProgramName(pythonProgramName);
+		Py_Initialize();
+		
+		//PyRun_SimpleString("print('Hello from Python!')");
+
 		if (arguments.size() == 3) {
 			relationNameRegEx = wregex(arguments[0]);
 			attributeNameRegEx = wregex(arguments[1]);
 			searchRegEx = wregex(arguments[2]);
 		} else {
+			PyMem_RawFree(pythonProgramName);
 			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-python <relationNameRegExp> <attributeNameRegExp> <searchRegExp>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
 		}
 	}
 
+	virtual ~GrepHandler() {
+		Py_FinalizeEx();
+		PyMem_RawFree(pythonProgramName);
+	}
+
 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
 		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
 		vector<writer::AttributeMetadata> writerMetadata;