After some trial and error I discovered that my check whether the sys.argv
list was empty was failing. I had been trying this
if not sys.argv
which I expected to work in the case where I was running from ExecuteScript
rather than the linux command line. In the case running from the command
line, I knew with certainty that I would have at a minimum the scrpt.py in
the list of sys.argv.

It turns out that when executing the script from the ExecuteScript
processor, the script.py is not in the sys.argv. But sys.argv does not
evaluate to empty - it evaluates to [''].

When I check with this
if str(sys.argv) == '[\'\']'
the logic works as needed.

I feel like I don't fully understand what's going on, but I do have it
working. Seems to me that the "if not sys.argv" should have worked, but it
does not. I think when I try str(sys.argv) I may force the two single quote
literals into the list, maybe? I am not sure why the "not sys.argv" failed.

In any case, I have something that works and wanted to circle back and post
what I found.

On Sun, Feb 18, 2018 at 7:54 PM, Matt Burgess <mattyb...@apache.org> wrote:

> Jim,
>
> I don't think that's possible because I don't think the user-defined
> properties are guaranteed to be in a particular order (insertion order
> in the processor config dialog, e.g.), but someone please correct me
> if I'm wrong. If true, then we wouldn't be able to have the
> user-defined properties set deterministically as "positional"
> arguments in an array.
>
> You might be able to check to see if sys.argvs is defined [1] or None
> or empty, and if so, then try the user-defined properties as variables
> using the same technique. You wouldn't be able to support a "usage()"
> type of thing (where in a command-line script you print the usage if
> the arguments are not valid/complete), but you should be able to
> determine whether the script is being run from the command-line Python
> or from ExecuteScript Jython.
>
> Regards,
> Matt
>
> [1] https://stackoverflow.com/questions/1592565/determine-
> if-variable-is-defined-in-python
>
>
> On Sun, Feb 18, 2018 at 10:25 AM, James McMahon <jsmcmah...@gmail.com>
> wrote:
> > Checking back in. Can we reference the user-defined custom config parms
> > passed in from ExecuteScript as sys.argvs in the python script being
> > executed by ExecuteScript?
> >
> > On Fri, Feb 16, 2018 at 1:14 PM, James McMahon <jsmcmah...@gmail.com>
> wrote:
> >>
> >> Good afternoon. I have a python script I would like to run from
> >> ExecuteScript. Since my script can run from either a command line in
> debug
> >> mode or from the ES processor, I'd like to set up initial logic to
> determine
> >> which command line parms i have, and direct actions in the script
> >> accordingly.
> >>
> >> At the command line I'll execute like this:  myScript.py
> [configFile_nifi]
> >> [loggingOutput as either of NIFI | SCRIPT] - two arguments.
> >>
> >> From Matt B's helpful post here...
> >>
> >> https://community.hortonworks.com/articles/77739/
> executescript-cookbook-part-3.html
> >> ...I know that I can feed the script these arguments from ExecuteScript
> by
> >> creating two new properties within ExecuteScript and setting their
> values.
> >> In the script I can read those into the script like this:
> >> configFile = configFile_nifi.getValue()
> >> targetOutput = loggingOutput.getValue()
> >>
> >> But i need to read these arguments in one way if they are from the
> command
> >> line, and using the above syntax if they are from the ExecuteScript
> call of
> >> the script. If set by the NiFi ES processor, can i refer to these parms
> as
> >> len(sys.argv), sys.argv[1], and sys.argv[2]?
> >>
> >> If not, how can I direct the setting of my script variables based on
> >> whether the arguments come from the command line or from the NiFi
> processor
> >> configuration?
> >>
> >> Jim
> >
> >
>

Reply via email to