Added non-babel output mode to extract_from_ast, integreated jinja2 doctests directly into the py.test suite (ugh, that's an ugly hack)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 53db78e · Browse files · 5 files·+97 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+97 added−19 removed
- tests:+45 added−4 removed
- other:+52 added−15 removed
Provenance
- Commit
abd3657- Tree
8c4ed31- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
53db78e
5 files changed+97 added−19 removed
modified:docs/Makefile+9 added-4 removed
14 unmodified lines15 16 help:17 @echo "Please use \`make <target>' where <target> is one of"18 @echo " html to make standalone HTML files"19- @echo " pickle to make pickle files (usable by e.g. sphinx-web)"19+ @echo " pickle to make pickle files"20+ @echo " json to make JSON files"21 @echo " htmlhelp to make HTML files and a HTML help project"22 @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"23 @echo " changes to make an overview over all changed/added/deprecated items"24 @echo " linkcheck to check all external links for integrity"10 unmodified lines35 pickle:36 mkdir -p _build/pickle _build/doctrees37 $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) _build/pickle38 @echo38- @echo "Build finished; now you can process the pickle files or run"39- @echo " sphinx-web _build/pickle"40- @echo "to start the sphinx-web server."39+ @echo "Build finished; now you can process the pickle files"40 41+json:42+ mkdir -p _build/json _build/doctrees43+ $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) _build/json44+ @echo45+ @echo "Build finished; now you can process the json files"46+47 web: pickle48 49 htmlhelp:50 mkdir -p _build/htmlhelp _build/doctrees25 unmodified linesmodified:jinja2/ext.py+37 added-5 removed
319 unmodified lines320 return nodes.Break(lineno=token.lineno)321 return nodes.Continue(lineno=token.lineno)322 323 324-def extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS):325- """Extract localizable strings from the given template node.324+def extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS,325+ babel_style=True):326+ """Extract localizable strings from the given template node. Per327+ default this function returns matches in babel style that means non string328+ parameters as well as keyword arguments are returned as `None`. This329+ allows Babel to figure out what you really meant if you are using330+ gettext functions that allow keyword arguments for placeholder expansion.331+ If you don't want that behavior set the `babel_style` parameter to `False`332+ which causes only strings to be returned and parameters are always stored333+ in tuples. As a consequence invalid gettext calls (calls without a single334+ string parameter or string parameters after non-string parameters) are335+ skipped.336 337+ This example explains the behavior:338+339+ >>> from jinja2 import Environment340+ >>> env = Environment()341+ >>> node = env.parse('{{ (_("foo"), _(), ngettext("foo", "bar", 42)) }}')342+ >>> list(extract_from_ast(node))343+ [(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))]344+ >>> list(extract_from_ast(node, babel_style=False))345+ [(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))]346+347 For every string found this function yields a ``(lineno, function,348 message)`` tuple, where:349 350 * ``lineno`` is the number of the line on which the string was found,14 unmodified lines365 strings.append(arg.value)366 else:367 strings.append(None)368 349- if len(strings) == 1:350- strings = strings[0]369+ for arg in node.kwargs:370+ strings.append(None)371+ if node.dyn_args is not None:372+ strings.append(None)373+ if node.dyn_kwargs is not None:374+ strings.append(None)375+376+ if not babel_style:377+ strings = tuple(x for x in strings if x is not None)378+ if not strings:379+ continue380 else:381+ if len(strings) == 1:382+ strings = strings[0]383+ else:352- strings = tuple(strings)384+ strings = tuple(strings)385 yield node.lineno, node.node.name, strings386 387 388 def babel_extract(fileobj, keywords, comment_tags, options):48 unmodified linesmodified:jinja2/loaders.py+1 added-1 removed
255 unmodified lines256 is tried.257 258 >>> loader = ChoiceLoader([259 ... FileSystemLoader('/path/to/user/templates'),260- ... PackageLoader('myapplication')260+ ... PackageLoader('mypackage')261 ... ])262 263 This is useful if you want to allow users to override builtin templates264 from a different location.12 unmodified linesmodified:jinja2/runtime.py+5 added-5 removed
344 unmodified lines345 True346 >>> foo + 42347 Traceback (most recent call last):348 ...349- jinja2.exceptions.UndefinedError: 'foo' is undefined349+ UndefinedError: 'foo' is undefined350 """351 __slots__ = ('_undefined_hint', '_undefined_obj', '_undefined_name',352 '_undefined_exception')353 60 unmodified lines414 True415 >>> foo + 42416 Traceback (most recent call last):417 ...418- jinja2.exceptions.UndefinedError: 'foo' is undefined418+ UndefinedError: 'foo' is undefined419 """420 __slots__ = ()421 422 def __unicode__(self):15 unmodified lines438 >>> foo = StrictUndefined(name='foo')439 >>> str(foo)440 Traceback (most recent call last):441 ...442- jinja2.exceptions.UndefinedError: 'foo' is undefined442+ UndefinedError: 'foo' is undefined443 >>> not foo444 Traceback (most recent call last):445 ...446- jinja2.exceptions.UndefinedError: 'foo' is undefined446+ UndefinedError: 'foo' is undefined447 >>> foo + 42448 Traceback (most recent call last):449 ...450- jinja2.exceptions.UndefinedError: 'foo' is undefined450+ UndefinedError: 'foo' is undefined451 """452 __slots__ = ()453 __iter__ = __unicode__ = __len__ = __nonzero__ = __eq__ = __ne__ = \454 Undefined._fail_with_undefined_error5 unmodified linesmodified:tests/conftest.py+45 added-4 removed
16 unmodified lines17 from jinja2 import Environment18 from jinja2.loaders import BaseLoader19 from jinja2.exceptions import TemplateNotFound20 21+22 try:23 # This code adds support for coverage.py (see24 # http://nedbatchelder.com/code/modules/coverage.html).25 # It prints a coverage report for the modules specified in all41 unmodified lines67 loader = GlobalLoader()68 simple_env = Environment(trim_blocks=True, loader=loader, cache_size=0)69 70 71+class Directory(py.test.collect.Directory):72+73+ def run(self):74+ rv = super(Directory, self).run()75+ if self.fspath.basename == 'tests':76+ rv.append('doctests')77+ return rv78+79+ def join(self, name):80+ if name == 'doctests':81+ return JinjaDocTestModule(name, parent=self)82+ return super(Directory, self).join(name)83+84+85 class Module(py.test.collect.Module):86 87 def __init__(self, *args, **kwargs):88 self.env = simple_env19 unmodified lines108 109 110 class JinjaDocTest(py.test.collect.Item):111 112+ def __init__(self, *args, **kwargs):113+ realmod = kwargs.pop('realmod', False)114+ super(JinjaDocTest, self).__init__(*args, **kwargs)115+ self.realmod = realmod116+117 def run(self):118+ if self.realmod:119+ mod = __import__(self.name, None, None, [''])120+ else:98- mod = py.std.types.ModuleType(self.name)99- mod.__doc__ = self.obj121+ mod = py.std.types.ModuleType(self.name)122+ mod.__doc__ = self.obj123+ mod.env = self.parent.env124+ mod.MODULE = self.parent.obj125 self.execute(mod)126 127 def execute(self, mod):103- mod.env = self.parent.env104- mod.MODULE = self.parent.obj128 failed, tot = py.compat.doctest.testmod(mod, verbose=True)129 if failed:130 py.test.fail('doctest %s: %s failed out of %s' % (131 self.fspath, failed, tot))132+133+134+class JinjaDocTestModule(py.test.collect.Module):135+136+ def __init__(self, *args, **kwargs):137+ super(JinjaDocTestModule, self).__init__(*args, **kwargs)138+ self.doctest_modules = [139+ 'jinja2.environment', 'jinja2.compiler', 'jinja2.parser',140+ 'jinja2.lexer', 'jinja2.ext', 'jinja2.sandbox',141+ 'jinja2.filters', 'jinja2.tests', 'jinja2.utils',142+ 'jinja2.runtime'143+ ]144+145+ def run(self):146+ return self.doctest_modules147+148+ def join(self, name):149+ return JinjaDocTest(name, parent=self, realmod=True)