# HG changeset patch # User František Kučera # Date 1544726429 -3600 # Node ID 5e0b317f4100638bdbf7d70f8c5feae427ad77d1 # Parent 2c8f5e05c7b7ecf23f61a8e9e46419c86e701c50 execute arbitrary python code, e.g. relpipe-tr-python 'fstab' 'import sys; print("hello from Python", file=sys.stderr)' diff -r 2c8f5e05c7b7 -r 5e0b317f4100 src/PythonHandler.h --- a/src/PythonHandler.h Thu Dec 13 19:12:38 2018 +0100 +++ b/src/PythonHandler.h Thu Dec 13 19:40:29 2018 +0100 @@ -50,16 +50,15 @@ private: shared_ptr relationalWriter; + wstring_convert> convertor; // TODO: support also other encodings. wchar_t* pythonProgramName; wregex relationNameRegEx; - wregex attributeNameRegEx; - wregex searchRegEx; + string_t pythonCode; - vector currentSearchableAttributes; vector currentRecord; integer_t currentAttributeIndex = 0; - boolean_t includeCurrentRecord = false; + boolean_t includeCurrentRecord = true; boolean_t filterCurrentRelation = false; public: @@ -71,15 +70,12 @@ Py_SetProgramName(pythonProgramName); Py_Initialize(); - //PyRun_SimpleString("print('Hello from Python!')"); - - if (arguments.size() == 3) { + if (arguments.size() == 2) { relationNameRegEx = wregex(arguments[0]); - attributeNameRegEx = wregex(arguments[1]); - searchRegEx = wregex(arguments[2]); + pythonCode = arguments[1]; } else { PyMem_RawFree(pythonProgramName); - throw cli::RelpipeCLIException(L"Usage: relpipe-tr-python ", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND); + throw cli::RelpipeCLIException(L"Usage: relpipe-tr-python ", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND); } } @@ -97,33 +93,22 @@ currentRecord.resize(attributes.size()); - currentSearchableAttributes.resize(attributes.size(), false); filterCurrentRelation = regex_match(name, relationNameRegEx); - if (filterCurrentRelation) { - for (int i = 0; i < currentSearchableAttributes.size(); i++) { - currentSearchableAttributes[i] = regex_match(attributes[i].getAttributeName(), attributeNameRegEx); - } - } - relationalWriter->startRelation(name, writerMetadata, true); } void attribute(const string_t& value) override { if (filterCurrentRelation) { currentRecord[currentAttributeIndex] = value; - - if (currentSearchableAttributes[currentAttributeIndex]) { - includeCurrentRecord |= regex_search(value, searchRegEx); - } - currentAttributeIndex++; - if (currentAttributeIndex > 0 && currentAttributeIndex % currentSearchableAttributes.size() == 0) { + if (currentAttributeIndex > 0 && currentAttributeIndex % currentRecord.size() == 0) { + PyRun_SimpleString(convertor.to_bytes(pythonCode).c_str()); if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v); - includeCurrentRecord = false; + includeCurrentRecord = true; } - currentAttributeIndex = currentAttributeIndex % currentSearchableAttributes.size(); + currentAttributeIndex = currentAttributeIndex % currentRecord.size(); } else { relationalWriter->writeAttribute(value); }