noahmorrison / chevron

A Python implementation of mustache
MIT License
510 stars 54 forks source link

Improve warn behavior to return Error instead of printing to stderr #93

Open ankitpatel96 opened 3 years ago

ankitpatel96 commented 3 years ago

Hi, I was wondering if we could improve the behavior of chevron.render (with warn=True) so that it hands back an error type containing the issues along with the rendered string, rather than just printing to stderr? This would make it easier to use the library programmatically. This seems like a breaking api change so switching to a different method or cutting a new major release would probably be necessary. I'm open to submitting a PR for this if you're open to this change.

noahmorrison commented 3 years ago

Yeah this sounds reasonable. I guess the way to do this would be to rename and change the current function to return an Error on failures, and replace the function with one that calls it, and on error prints it to stderr?

I might try to write this up this weekend, but if I don't feel free to beat me to it with your own PR!

dmorrison42 commented 3 years ago

I'm pretty sure we made this backward compatible!

If this looks good we can merge it in.

noahmorrison commented 3 years ago

Here's the relevant commit

With a test case here

If this satisfies what you wanted I can merge it in.

jonhartnett commented 3 years ago

Can't speak for the OP, but this commit solves my issue. Currently I'm intercepting stderr to detect warnings, but I'd love to switch to something more robust.

ex3cv commented 3 years ago

Hi @noahmorrison. Do you see a chance to merge this any time soon and make a release?

stefanpl commented 2 years ago

Hey guys 👋

May I kindly ask what's the status here? To me it looks like there's a mergeable solution ready, so why not push it across the finish line?

@noahmorrison would you be so kind? 😊

ankitpatel96 commented 2 years ago

Sorry I switched to another project at work but I 100% support just putting up a PR with this branch! The change is pretty clever and would totally work for my use case. I'd gladly review and approve @noahmorrison's change.

gl-yziquel commented 1 year ago

On this cloned repo https://github.com/gl-yziquel/chevron you may pass a function to the warn argument, and, in the case of a missing key, the missing key will be passed to the callable warn. And what the function returns will be what will be interpolated.

Fail instead of returning something, if you want...

Just stating this for anyone who wishes an off the shelf solution.