Skip to content

Commit

Permalink
api change. jinja2_renderer being deprecated in favor of with_jinja=F…
Browse files Browse the repository at this point in the history
…alse becoming parameter of main render function
  • Loading branch information
knowsuchagency committed Jun 26, 2017
1 parent 15424cb commit 15a1630
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 12 deletions.
10 changes: 6 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ template-rendering power.
.. code-block:: python
from ninjadog import jinja2_renderer
from ninjadog import render
def stop_believing():
Expand All @@ -139,9 +139,11 @@ template-rendering power.
p {{ "Don't" if not stop_believing() }} stop believing
"""
print(jinja2_renderer(template_string,
context=context,
pretty=True))
print(render(template_string,
context=context,
pretty=True,
with_jinja=True))
.. code-block:: html
Expand Down
3 changes: 3 additions & 0 deletions ninjadog/ext/jinja2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
import typing as T
from functools import partial
from pathlib import Path
Expand Down Expand Up @@ -52,6 +53,8 @@ def jinja2_renderer(string: str = '',
Returns: html string
"""
warnings.warn("\nThis function will be deprecated in the upcoming minor version " \
"use ninjadog.render(... with_jinja=True) instead", PendingDeprecationWarning, stacklevel=2)
# lock arguments
pug_renderer = partial(_render,
context=context,
Expand Down
8 changes: 2 additions & 6 deletions ninjadog/ext/pyramid/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
from pathlib import Path

from pyramid_jinja2 import *

from ninjadog import jinja2_renderer, render


# TODO: See if it might not make more to just render all pug templates in the static directory first, then render them in jinja2 as normal as a configuration option
from ninjadog import render


class PugTemplateRenderer(Jinja2TemplateRenderer):
Expand All @@ -16,6 +11,7 @@ class PugTemplateRenderer(Jinja2TemplateRenderer):
Conforms to `IRenderer <http://docs.pylonsproject.org/projects/pyramid/en/latest/api/interfaces.html#pyramid.interfaces.IRenderer>`_
interface.
"""

def __call__(self, value, system):
try:
system.update(value)
Expand Down
16 changes: 14 additions & 2 deletions ninjadog/ninjadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
from ninjadog.constants import PUG_CLI_PATH
from ninjadog.utils import jsonify

from jinja2 import Environment


def render(string: str = '',
filepath: T.Union[Path, str] = None,
context: T.Any = None,
pretty: bool = False,
pug_cli_path: T.Union[Path, str] = None) -> str:
pug_cli_path: T.Union[Path, str] = None,
with_jinja: bool = False) -> str:
"""
Render a pug template through the pug cli.
Expand All @@ -25,6 +28,7 @@ def render(string: str = '',
context: the data to be passed to the template
pretty: pretty html output
pug_cli_path: path to the pug cli
with_jinja: render jinja2 template syntax as well
Returns: rendered html
Expand Down Expand Up @@ -70,8 +74,16 @@ def render(string: str = '',

input_file = shlex.quote(fp.name)

return sp.run(f'{cmd} {context_arg} {path} {pretty_print} < {input_file}',
pug_string = sp.run(f'{cmd} {context_arg} {path} {pretty_print} < {input_file}',
shell=True,
stdout=sp.PIPE,
cwd=filepath.parent if filepath else None,
).stdout.decode('utf8')

if with_jinja:
env = Environment()
env.globals = context if context else {}

return env.from_string(pug_string).render()

return pug_string
64 changes: 64 additions & 0 deletions tests/test_ppug.py → tests/test_ninjadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def test_jinja2_render_no_string_argument():


def test_jinja2_syntax_with_pug_syntax():
# TODO: get rid of this test
from textwrap import dedent
from ninjadog import jinja2_renderer
string = dedent("""
Expand Down Expand Up @@ -109,3 +110,66 @@ def test_jinja2_syntax_with_pug_syntax():
actual_output = jinja2_renderer(string, context=context, pretty=True).strip()

assert expected_output == actual_output


def test_with_jinja2():
from textwrap import dedent
from ninjadog import render
string = dedent("""
if person.name == "Bob"
h1 Hello Bob
else
h1 My name is #{ person.name }
p The persons's uppercase name is {{ person.get('name').upper() }}
p The person's name is #{ person.name }
if animal
h1 This should not output
else
p animal value is false
""").strip()

context = {'person': {'name': 'Bob'}, 'animal': None}

expected_output = dedent("""
<h1>Hello Bob</h1>
<p>The persons's uppercase name is BOB</p>
<p>The person's name is Bob</p>
<p>animal value is false</p>
""").strip()

actual_output = render(string, context=context, pretty=True, with_jinja=True).strip()

assert expected_output == actual_output


def test_jinja2_renderer_raises_pending_deprecation():
import warnings
from ninjadog import jinja2_renderer
warnings.simplefilter('error')
with pytest.raises(PendingDeprecationWarning):
jinja2_renderer('h1 foo')


def test_jinja2_renderer_not_yet_deprecated():
from configparser import ConfigParser
from pathlib import Path

config = ConfigParser()
with Path(__file__).parent.parent.joinpath('setup.cfg').open() as fp:
config.read_string(fp.read())


version = config.get('bumpversion', 'current_version')
_, minor, _ = (int(e) for e in version.split('.'))

jinja2_exists = False

try:
from ninjadog.ext.jinja2 import jinja2_renderer
jinja2_exists = True
except ImportError:
pass

assert jinja2_exists and minor < 3, 'Time to deprecate jinja2_renderer'

0 comments on commit 15a1630

Please sign in to comment.