Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow templates to be nested in templates (e.g. use another template field as the DEFAULT value for a template) #363

Closed
saruter opened this issue Feb 3, 2021 · 9 comments
Labels
feature request New feature or request

Comments

@saruter
Copy link

saruter commented Feb 3, 2021

Hi,
thanks for developing this great tool, found nothing better!

I have one question concerning templating the folder structure when exporting.

What I want to achieve:
Exporting Photos to a folder-structure like this (DEFAULT value is another field):
--directory "{created.year}/{folder_album,created.mm}"

Which means that photos which are included in albums are exported in a folder like this (example):
2020/myalbum/img-xxxxx.jpg

and for photos which are not present in any album a default value of the month in which the photo was taken:
2020/03/img-xxxxx.jpg

resulting in a folder structure like this:

2020/myalbum1/
2020/myalbum2/
2020/01/
2020/02
2020/..

Is this currently possible?

@RhetTbull RhetTbull added the feature request New feature or request label Feb 3, 2021
@RhetTbull
Copy link
Owner

Unfortunately this isn't currently possible. I've considered this use case several times -- it would make the templating system very powerful to be able to nest templates. The template system is currently implemented with a regular expression (pattern matching) algorithm that does simple substitutions and can't handle nested patterns. To implement this would require significant re-work of the templating engine. Coupled with the boolean templates I'm working on implementing you could do some really complex export scenarios quite easily. It's something I'd like to tackle eventually but have a number of higher priority things at the moment.

However, I do think it would be easy to create a work-around for your specific use case. I could add --in-album and --not-in-album options which would allow you to do what you want, albeit it with two separate export runs:

osxphotos export /path/to/export --directory "{created.year}/{folder_album}" --in-album

osxphotos export /path/to/export --directory "{created.year}/{created.mm}" --not-in-album

@saruter
Copy link
Author

saruter commented Feb 3, 2021

Thanks, this workaround would be great!

@RhetTbull
Copy link
Owner

I've implemented --album and --not-in-album in v0.40.8 which should let you do the double-export as referenced above. This will even work with --update as both exports will use the same export database in your export directory to track exported files. The only limitation will be you cannot use --cleanup as this would think the photos from the second export were not needed and delete them. Also, be advised that with using {album} as a template could result in duplicate photos if a photo is in more than one album. Also, if you have any album names that match {created.mm} format (e.g. "03"), you'll possibly get name collisions.

@RhetTbull
Copy link
Owner

I've been experimenting with pyparsing and I think I can implement a full PEG parser for the template system which would allow arbitrary nesting of templates. This will take a bit of a learning curve and rework of the templating system so it won't happen quickly but definitely something for the roadmap. If anyone has experience with pyparsing and wants to help, I'll take it!

@RhetTbull RhetTbull changed the title Possibility to use other template-field as DEFAULT value Allow templates to be nested in templates (e.g. use another template field as the DEFAULT value for a template) Feb 4, 2021
@RhetTbull
Copy link
Owner

After spending a couple hours studying pyparsing, I'm still finding it difficult to understand. Luckily I found textx and am making progress on grammar implementation for the osxphotos template language which should allow the templating system to be much more capable.

@RhetTbull
Copy link
Owner

So textx is amazing and easy to use! I've been able to get a full parser working that allows arbitrary nesting of templates! It'll take some work to refactor the template code in osxphotos to use this but I think it'll be much easier than I first expected.

@saruter this means you'll be able to do something like this once implemented:

--directory "{created.year}/{inalbum?{folder_album},{created.mm}}"

@RhetTbull
Copy link
Owner

I got the new template parser working -- need to add some more tests then I'll release it in next week or so. It is much more powerful than the current template system -- allows arbitrary nesting of templates and allows any template to be used as a boolean.

@saruter
Copy link
Author

saruter commented Feb 8, 2021

Wohoo, sounds amazing. Thanks for your work in this!

@RhetTbull
Copy link
Owner

I've released v0.41.0 with a completely re-written template engine which now allows nested templates.

For example, this now works:

--directory "{created.year}/{album?{folder_album},{created.mm}}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants