Friday, May 10, 2019

Python indentation in the Jamf Pro script editor

The Jamf Pro web interface includes a built-in script editor which allows you to make changes to existing scripts, or to compose new scripts entirely within the Jamf Pro interface. There is one gotcha with this editor that is not really Jamf's fault, and one that experienced programmers can probably see coming: when working with Python scripts, you need to be sure to use consistent indentation, or else you'll see policy failures with errors indicating so.

What happens, at least in my case, is that I will write a Python script in Notepad++, nano, or some other editor that I have set up to replace tab characters with spaces, then copy-and-paste that script into the Jamf Pro web interface. Then, later on, when I go to edit the script, I'll hit the tab key, and the editor dutifully inserts a tab character. This confuses Python, which uses differing levels of indentation to determine code blocks (versus C-style languages, which use curly brackets).

When this happens, you can manually correct the indentation by replacing tabs with spaces, or you can copy-and-paste the indentation from another line at the same level. Another way to get around this, of course, is to copy the script from the Jamf Pro interface into your text editor of choice, make your changes, then copy it back into Jamf Pro, which avoids this issue, but adds some extra work.

This isn't a problem with bash or other scripting languages, as these aren't so strongly influenced by indentation. This also isn't fundamentally a problem with the Jamf Pro script editor, which inserts the character that you're asking it for, but more of a problem with muscle memory working against you when moving between two editors that are not set up in exactly the same way.

No comments:

Post a Comment

Tableau, TabPy, and the Case of No Input Rows

 I haven't scientifically confirmed this or anything, but it sure seems like if you pass an empty dataframe to a TabPy script, then no m...