You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by al...@apache.org on 2019/04/15 15:46:19 UTC

[nifi-minifi-cpp] branch master updated: MINIFICPP-802: add package support to python processors

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

aldrin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git


The following commit(s) were added to refs/heads/master by this push:
     new 275455c  MINIFICPP-802: add package support to python processors
275455c is described below

commit 275455c573ef659e88d1b32dd06c69373e1368c0
Author: Marc Parisi <ph...@apache.org>
AuthorDate: Thu Apr 11 15:55:21 2019 -0400

    MINIFICPP-802: add package support to python processors
    
    This closes #534
    
    Signed-off-by: Aldrin Piri <al...@apache.org>
---
 extensions/script/README.md              |  6 ++++-
 extensions/script/python/PythonCreator.h | 41 ++++++++++++++++++++++++++++++--
 2 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/extensions/script/README.md b/extensions/script/README.md
index 909c4a9..480311b 100644
--- a/extensions/script/README.md
+++ b/extensions/script/README.md
@@ -67,7 +67,11 @@ class VaderSentiment(object):
 
 ## Configuration
 
-To enable python Processor capabilities, the following options need to be provided in minifi.properties
+To enable python Processor capabilities, the following options need to be provided in minifi.properties. The directory specified
+can contain processors. Note that the processor name will be the reference in your flow. Directories are treated like package names.
+Therefore if the nifi.python.processor.dir is /tmp/ and you have a subdirectory named packagedir with the file name file.py, it will
+produce a processor with the name org.apache.nifi.minifi.processors.packagedir.file. Note that each subdirectory will append a package 
+to the reference class name. 
 
     in minifi.properties
 	#directory where processors exist
diff --git a/extensions/script/python/PythonCreator.h b/extensions/script/python/PythonCreator.h
index 8a0fbff..fb87592 100644
--- a/extensions/script/python/PythonCreator.h
+++ b/extensions/script/python/PythonCreator.h
@@ -29,6 +29,7 @@
 #include "agent/agent_version.h"
 #include "agent/build_description.h"
 #include "utils/file/FileUtils.h"
+#include "utils/StringUtils.h"
 
 namespace org {
 namespace apache {
@@ -79,13 +80,22 @@ class PythonCreator : public minifi::core::CoreComponent {
 
       for (const auto &path : classpaths_) {
         const auto &scriptname = getScriptName(path);
-        PyProcCreator::getPythonCreator()->addClassName(scriptname, path);
+        const auto &package = getPackage(pathListings, path);
+        if (!package.empty()) {
+          std::string script_with_package = "org.apache.nifi.minifi.processors." + package + "." + scriptname;
+          PyProcCreator::getPythonCreator()->addClassName(script_with_package, path);
+        } else {
+          // maintain backwards compatibility with the standard package.
+          PyProcCreator::getPythonCreator()->addClassName(scriptname, path);
+        }
       }
 
       core::ClassLoader::getDefaultClassLoader().registerResource("", "createPyProcFactory");
 
       for (const auto &path : classpaths_) {
+
         const auto &scriptName = getScriptName(path);
+
         utils::Identifier uuid;
         auto processor = std::dynamic_pointer_cast<core::Processor>(core::ClassLoader::getDefaultClassLoader().instantiate(scriptName, uuid));
         if (processor) {
@@ -93,10 +103,16 @@ class PythonCreator : public minifi::core::CoreComponent {
             processor->initialize();
             auto proc = std::dynamic_pointer_cast<python::processors::ExecutePythonProcessor>(processor);
             minifi::BundleDetails details;
+            const auto &package = getPackage(pathListings, path);
+            std::string script_with_package = "org.apache.nifi.minifi.processors.";
+            if (!package.empty()) {
+              script_with_package += package + ".";
+            }
+            script_with_package += scriptName;
             details.artifact = getFileName(path);
             details.version = minifi::AgentBuild::VERSION;
             details.group = "python";
-            minifi::ClassDescription description("org.apache.nifi.minifi.processors." + scriptName);
+            minifi::ClassDescription description(script_with_package);
             description.dynamic_properties_ = proc->getPythonSupportDynamicProperties();
             auto properties = proc->getPythonProperties();
 
@@ -122,6 +138,27 @@ class PythonCreator : public minifi::core::CoreComponent {
   }
 
  private:
+  std::string getPackage(const std::string &basePath, const std::string pythonscript) {
+    const auto script_directory = getPath(pythonscript);
+    const auto loc = script_directory.find_first_of(basePath);
+    if (loc != 0 || script_directory.size() <= basePath.size()) {
+      return "";
+    }
+    const auto python_dir = script_directory.substr(basePath.length() + 1);
+    auto python_package = python_dir.substr(0, python_dir.find_last_of("/\\"));
+    utils::StringUtils::replaceAll(python_package, "/", ".");
+    utils::StringUtils::replaceAll(python_package, "\\", ".");
+    if (python_package.length() > 1 && python_package.at(0) == '.') {
+      python_package = python_package.substr(1);
+    }
+    std::transform(python_package.begin(), python_package.end(), python_package.begin(), ::tolower);
+    return python_package;
+  }
+
+  std::string getPath(const std::string &pythonscript) {
+    std::string path = pythonscript.substr(0, pythonscript.find_last_of("/\\"));
+    return path;
+  }
 
   std::string getFileName(const std::string &pythonscript) {
     std::string path = pythonscript.substr(pythonscript.find_last_of("/\\") + 1);