src/PythonHandler.h
branchv_0
changeset 10 2c8f5e05c7b7
parent 9 ed7ab0a06849
child 11 5e0b317f4100
equal deleted inserted replaced
9:ed7ab0a06849 10:2c8f5e05c7b7
    24 #include <sstream>
    24 #include <sstream>
    25 #include <locale>
    25 #include <locale>
    26 #include <codecvt>
    26 #include <codecvt>
    27 #include <regex>
    27 #include <regex>
    28 
    28 
       
    29 #include <Python.h>
       
    30 
    29 #include <relpipe/reader/typedefs.h>
    31 #include <relpipe/reader/typedefs.h>
    30 #include <relpipe/reader/TypeId.h>
    32 #include <relpipe/reader/TypeId.h>
    31 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    33 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    32 #include <relpipe/reader/handlers/AttributeMetadata.h>
    34 #include <relpipe/reader/handlers/AttributeMetadata.h>
    33 
    35 
    46 
    48 
    47 class GrepHandler : public RelationalReaderStringHadler {
    49 class GrepHandler : public RelationalReaderStringHadler {
    48 private:
    50 private:
    49 	shared_ptr<writer::RelationalWriter> relationalWriter;
    51 	shared_ptr<writer::RelationalWriter> relationalWriter;
    50 
    52 
       
    53 	wchar_t* pythonProgramName;
       
    54 
    51 	wregex relationNameRegEx;
    55 	wregex relationNameRegEx;
    52 	wregex attributeNameRegEx;
    56 	wregex attributeNameRegEx;
    53 	wregex searchRegEx;
    57 	wregex searchRegEx;
    54 
    58 
    55 	vector<boolean_t> currentSearchableAttributes;
    59 	vector<boolean_t> currentSearchableAttributes;
    61 public:
    65 public:
    62 
    66 
    63 	GrepHandler(ostream& output, const vector<string_t>& arguments) {
    67 	GrepHandler(ostream& output, const vector<string_t>& arguments) {
    64 		relationalWriter.reset(writer::Factory::create(output));
    68 		relationalWriter.reset(writer::Factory::create(output));
    65 
    69 
       
    70 		pythonProgramName = Py_DecodeLocale("relpipe-tr-python", NULL);
       
    71 		Py_SetProgramName(pythonProgramName);
       
    72 		Py_Initialize();
       
    73 		
       
    74 		//PyRun_SimpleString("print('Hello from Python!')");
       
    75 
    66 		if (arguments.size() == 3) {
    76 		if (arguments.size() == 3) {
    67 			relationNameRegEx = wregex(arguments[0]);
    77 			relationNameRegEx = wregex(arguments[0]);
    68 			attributeNameRegEx = wregex(arguments[1]);
    78 			attributeNameRegEx = wregex(arguments[1]);
    69 			searchRegEx = wregex(arguments[2]);
    79 			searchRegEx = wregex(arguments[2]);
    70 		} else {
    80 		} else {
       
    81 			PyMem_RawFree(pythonProgramName);
    71 			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-python <relationNameRegExp> <attributeNameRegExp> <searchRegExp>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
    82 			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-python <relationNameRegExp> <attributeNameRegExp> <searchRegExp>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
    72 		}
    83 		}
       
    84 	}
       
    85 
       
    86 	virtual ~GrepHandler() {
       
    87 		Py_FinalizeEx();
       
    88 		PyMem_RawFree(pythonProgramName);
    73 	}
    89 	}
    74 
    90 
    75 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
    91 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
    76 		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
    92 		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
    77 		vector<writer::AttributeMetadata> writerMetadata;
    93 		vector<writer::AttributeMetadata> writerMetadata;