Skip to content

Commit

Permalink
Allow warn to be a Callable
Browse files Browse the repository at this point in the history
When warn is set to a Callable, it is called with a ChevronError
instead of writing to stderr
  • Loading branch information
noahmorrison committed Mar 20, 2021
1 parent 90413a2 commit 9a5130e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
4 changes: 2 additions & 2 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .chevron.main import main, cli_main
from .chevron.renderer import render
from .chevron.tokenizer import ChevronError
from .chevron.error import ChevronError, ChevronKeyError

__all__ = ['main', 'render', 'cli_main', 'ChevronError']
__all__ = ['main', 'render', 'cli_main', 'ChevronError', 'ChevronKeyError']
4 changes: 2 additions & 2 deletions chevron/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .main import main, cli_main
from .renderer import render
from .tokenizer import ChevronError
from .error import ChevronError, ChevronKeyError

__all__ = ['main', 'render', 'cli_main', 'ChevronError']
__all__ = ['main', 'render', 'cli_main', 'ChevronError', 'ChevronKeyError']
5 changes: 5 additions & 0 deletions chevron/error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChevronError(SyntaxError):
pass

class ChevronKeyError(ChevronError):
pass
13 changes: 11 additions & 2 deletions chevron/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from collections import Sequence, Iterator, Callable
try:
from .tokenizer import tokenize
from .error import ChevronKeyError
except (ValueError, SystemError): # python 2
from tokenizer import tokenize
from .error import ChevronKeyError


import sys
Expand Down Expand Up @@ -92,7 +94,11 @@ def _get_key(key, scopes, warn=False):
# We couldn't find the key in any of the scopes

if warn:
sys.stderr.write("Could not find key '%s'%s" % (key, linesep))
warning = "Could not find key '%s'%s" % (key, linesep)
if isinstance(warn, Callable):
warn(ChevronKeyError(warning))
else:
sys.stderr.write(warning)

return ''

Expand Down Expand Up @@ -172,7 +178,10 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
scopes -- The list of scopes that get_key will look through
warn -- Issue a warning to stderr when a template substitution isn't found in the data
warn -- If True, issues a warning to stderr when a template
substitution isn't found in the data.
If warn is callable, a ChevronError is passed to the
callable
Returns:
Expand Down
7 changes: 4 additions & 3 deletions chevron/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
_CURRENT_LINE = 1
_LAST_TAG_LINE = None


class ChevronError(SyntaxError):
pass
try:
from .error import ChevronError
except (ValueError, SystemError): # python 2
from .error import ChevronError

#
# Helper functions
Expand Down
27 changes: 27 additions & 0 deletions test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,34 @@ def test_disabled_partials(self):
self.assertEqual(resultNone, expected)
self.assertEqual(resultEmpty, expected)
os.chdir('..')

# https://github.com/noahmorrison/chevron/issues/93
def test_custom_warning(self):
def render_return_warnings(args):
warnings = []
def add_warning(warning):
warnings.append(warning)

args['warn'] = add_warning

result = chevron.render(**args)
return (result, warnings)


args = {
'template': 'before, {{> with_missing_key }}, after',
'partials_dict': {
'with_missing_key': '{{#missing_key}}bloop{{/missing_key}}',
},
}

expected = 'before, , after'
(result, warnings) = render_return_warnings(args)

self.assertEqual(result, expected)
self.assertEqual(len(warnings), 1)
self.assertEqual(type(warnings[0]), chevron.ChevronKeyError)
self.assertEqual(warnings[0].msg, "Could not find key 'missing_key'\n")

# Run unit tests from command line
if __name__ == "__main__":
Expand Down

0 comments on commit 9a5130e

Please sign in to comment.