Skip to content

Commit

Permalink
NIFI-13528: fixed Python processor's customValidate function
Browse files Browse the repository at this point in the history
This closes #9061.

Signed-off-by: Peter Gyori <pgyori@apache.org>
  • Loading branch information
mark-bathori authored and pgyori committed Jul 16, 2024
1 parent 1ff5ebd commit 36fb6f1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,30 +292,11 @@ def __add_resource_definition(self, gateway, resource_definition, builder):
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:
Expand All @@ -339,12 +320,52 @@ def getProperties(self):

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",
Expand Down Expand Up @@ -435,3 +456,18 @@ def evaluateAttributeExpressions(self, attributeMap=None):

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()
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -58,7 +58,12 @@ def customValidate(self, context):
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
Expand Down

0 comments on commit 36fb6f1

Please sign in to comment.