You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by pg...@apache.org on 2024/07/16 18:30:51 UTC

(nifi) branch main updated: NIFI-13528: fixed Python processor's customValidate function

This is an automated email from the ASF dual-hosted git repository.

pgyori pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/main by this push:
     new 36fb6f109f NIFI-13528: fixed Python processor's customValidate function
36fb6f109f is described below

commit 36fb6f109fd06b61ecaf47d43a40308a1f94f94b
Author: Mark Bathori <mb...@apache.org>
AuthorDate: Mon Jul 8 13:32:09 2024 +0200

    NIFI-13528: fixed Python processor's customValidate function
    
    This closes #9061.
    
    Signed-off-by: Peter Gyori <pg...@apache.org>
---
 .../src/main/python/src/nifiapi/properties.py      | 80 ++++++++++++++++------
 .../python/framework/PythonProcessorAdapter.py     |  9 ++-
 2 files changed, 65 insertions(+), 24 deletions(-)

diff --git a/nifi-extension-bundles/nifi-py4j-bundle/nifi-python-extension-api/src/main/python/src/nifiapi/properties.py b/nifi-extension-bundles/nifi-py4j-bundle/nifi-python-extension-api/src/main/python/src/nifiapi/properties.py
index 27f4877506..a37107ac18 100644
--- a/nifi-extension-bundles/nifi-py4j-bundle/nifi-python-extension-api/src/main/python/src/nifiapi/properties.py
+++ b/nifi-extension-bundles/nifi-py4j-bundle/nifi-python-extension-api/src/main/python/src/nifiapi/properties.py
@@ -292,30 +292,11 @@ class PropertyDescriptor:
         builder.identifiesExternalResource(cardinality, types[0], types[1:])
 
 
-class ProcessContext:
+class PropertyContext:
     __trivial_attribute_reference__ = re.compile(r"\$\{([^${}\[\],:;/*\' \t\r\n\\d][^${}\[\],:;/*\' \t\r\n]*)}")
     __escaped_attribute_reference__ = re.compile(r"\$\{'([^${}\[\],:;/*\' \t\r\n\\d][^${}\[\],:;/*\'\t\r\n]*)'}")
 
-    def __init__(self, java_context):
-        self.java_context = java_context
-
-        descriptors = java_context.getProperties().keySet()
-        self.name = java_context.getName()
-        self.property_values = {}
-        self.descriptor_value_map = {}
-
-        for descriptor in descriptors:
-            property_value = java_context.getProperty(descriptor.getName())
-            string_value = property_value.getValue()
-
-            property_value = self.__create_python_property_value(descriptor.isExpressionLanguageSupported(), property_value, string_value)
-            self.property_values[descriptor.getName()] = property_value
-
-            python_descriptor = PropertyDescriptor.from_java_descriptor(descriptor)
-            self.descriptor_value_map[python_descriptor] = string_value
-
-
-    def __create_python_property_value(self, el_supported, java_property_value, string_value):
+    def create_python_property_value(self, el_supported, java_property_value, string_value):
         el_present = java_property_value.isExpressionLanguagePresent()
         referenced_attribute = None
         if el_present:
@@ -339,12 +320,52 @@ class ProcessContext:
 
     def newPropertyValue(self, value):
         java_property_value = self.java_context.newPropertyValue(value)
-        return self.__create_python_property_value(True, java_property_value, value)
+        return self.create_python_property_value(True, java_property_value, value)
+
+
+class ProcessContext(PropertyContext):
+
+    def __init__(self, java_context):
+        self.java_context = java_context
+
+        descriptors = java_context.getProperties().keySet()
+        self.name = java_context.getName()
+        self.property_values = {}
+        self.descriptor_value_map = {}
+
+        for descriptor in descriptors:
+            property_value = java_context.getProperty(descriptor.getName())
+            string_value = property_value.getValue()
+
+            property_value = self.create_python_property_value(descriptor.isExpressionLanguageSupported(), property_value, string_value)
+            self.property_values[descriptor.getName()] = property_value
+
+            python_descriptor = PropertyDescriptor.from_java_descriptor(descriptor)
+            self.descriptor_value_map[python_descriptor] = string_value
 
     def getName(self):
         return self.name
 
 
+class ValidationContext(PropertyContext):
+
+    def __init__(self, java_context):
+        self.java_context = java_context
+
+        descriptors = java_context.getProperties().keySet()
+        self.property_values = {}
+        self.descriptor_value_map = {}
+
+        for descriptor in descriptors:
+            property_value = java_context.getProperty(descriptor)
+            string_value = property_value.getValue()
+
+            property_value = self.create_python_property_value(descriptor.isExpressionLanguageSupported(), property_value, string_value)
+            self.property_values[descriptor.getName()] = property_value
+
+            python_descriptor = PropertyDescriptor.from_java_descriptor(descriptor)
+            self.descriptor_value_map[python_descriptor] = string_value
+
 
 class TimeUnit(Enum):
     NANOSECONDS = "NANOSECONDS",
@@ -435,3 +456,18 @@ class PythonPropertyValue:
 
         return self
 
+
+class ValidationResult:
+    def __init__(self, subject="", explanation="", valid=False, input=None):
+        self.subject = subject
+        self.explanation = explanation
+        self.valid = valid
+        self.input = input
+
+    def to_java_validation_result(self):
+            return JvmHolder.gateway.jvm.org.apache.nifi.components.ValidationResult.Builder() \
+                .subject(self.subject) \
+                .explanation(self.explanation) \
+                .valid(self.valid) \
+                .input(self.input) \
+                .build()
diff --git a/nifi-extension-bundles/nifi-py4j-bundle/nifi-python-framework/src/main/python/framework/PythonProcessorAdapter.py b/nifi-extension-bundles/nifi-py4j-bundle/nifi-python-framework/src/main/python/framework/PythonProcessorAdapter.py
index 44d46febb8..34e995b959 100644
--- a/nifi-extension-bundles/nifi-py4j-bundle/nifi-python-framework/src/main/python/framework/PythonProcessorAdapter.py
+++ b/nifi-extension-bundles/nifi-py4j-bundle/nifi-python-framework/src/main/python/framework/PythonProcessorAdapter.py
@@ -13,7 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from nifiapi.properties import ProcessContext
+from nifiapi.properties import ProcessContext, ValidationContext
 
 
 def is_method_defined(processor, method_name):
@@ -58,7 +58,12 @@ class PythonProcessorAdapter:
         if not self.hasCustomValidate:
             return None
 
-        return self.processor.customValidate(ProcessContext(context))
+        validation_results = self.processor.customValidate(ValidationContext(context))
+
+        result_list = self.gateway.jvm.java.util.ArrayList()
+        for result in validation_results:
+            result_list.add(result.to_java_validation_result())
+        return result_list
 
     def getRelationships(self):
         # If self.relationships is None, it means that the Processor has implemented the method, and we need