execute arbitrary python code, v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 13 Dec 2018 19:40:29 +0100
branchv_0
changeset 11 5e0b317f4100
parent 10 2c8f5e05c7b7
child 12 ee69be2212fa
execute arbitrary python code, e.g. relpipe-tr-python 'fstab' 'import sys; print("hello from Python", file=sys.stderr)'
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<writer::RelationalWriter> relationalWriter;
 
+	wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
 	wchar_t* pythonProgramName;
 
 	wregex relationNameRegEx;
-	wregex attributeNameRegEx;
-	wregex searchRegEx;
+	string_t pythonCode;
 
-	vector<boolean_t> currentSearchableAttributes;
 	vector<string_t> 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 <relationNameRegExp> <attributeNameRegExp> <searchRegExp>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
+			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-python <relationNameRegExp> <pythonCode>", 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);
 		}