Ideas on user-defined templates for a command line tool

I wrote a python package to interact with the Apple Photos app on a Mac. One of the features it provides is to export photos from the user’s Photos library. I’d like the user to be able to specify, via template, the export directory and and the the exported file name. For example:

osxphotos export ~/Desktop/export --filename="${photo.original_filename}_${}" --directory="${}/${}"

I already use mako templating system for writing XMP sidecars files. It would be possible to use this to allow the user to specify filename or directory using format above while giving the user access to the underlying object (photo in above example) which they could use to define the desired name.

This implementation would be very powerful and give the user full control over the naming of exported images. The downside of this implementation is that it also allows the user to enter arbitrary python code that would be executed by osxphotos. Probably not a big deal as the user already has full access to the terminal (and to use this script, would already have to have granted “Full Disk Access” in the Mac security subsystem) but it still provides a vector to inject code, for example, if the user relied on a shell script to run osxphotos and didn’t realize what they were feeding in.

This would also require the user to have a basic understanding of simple python syntax. An alternative would be to roll my own templating system that does simple substitutions e.g. --filename="{original_name}_{yyyy}_{mm}" and substitute for values in the {} but that would be less flexible than a full templating system. e.g. in this implementation, I’d have to have a mapping that says “replace original_name in the template with the corresponding property of the underlying photo object” and do this for every possible property I want to expose to the user.

Any ideas for the best way to implement this kind of thing?

1 Like