Skip to content

Commit

Permalink
working on template system
Browse files Browse the repository at this point in the history
  • Loading branch information
knowsuchagency committed Jun 29, 2017
1 parent fbdd323 commit 4abed00
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 28 deletions.
12 changes: 9 additions & 3 deletions fabfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ def clean():


@task
def test():
"""Run tests quickly with default Python."""
local('py.test')
def test(capture=True):
"""
Run tests quickly with default Python.
Args:
capture: capture stdout [default: True]
"""
disable_capturing = ' -s' if not true(capture) else ''
local('py.test' + disable_capturing)


@task(alias='tox')
Expand Down
2 changes: 2 additions & 0 deletions ninjadog/constants.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import re
import subprocess as sp
from pathlib import Path
from tempfile import gettempdir


EXTENSION_PATT = re.compile('(?:extends|include)\s+(.+)')
PUG_CLI_PATH = str(Path(sp.Popen(('which', 'pug'), stdout=sp.PIPE).communicate()[0].decode('utf8').strip()).absolute()) \
if sp.Popen(('which', 'npm'), stdout=sp.DEVNULL).wait() == 0 else None
TEMPDIR = Path(gettempdir(), '.ninjadog/')

48 changes: 47 additions & 1 deletion ninjadog/ext/pyramid.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from pprint import pprint
from shutil import rmtree as delete_directory
from pathlib import Path

from pyramid_jinja2 import (
Jinja2TemplateRenderer,
TemplateNotFound,
Expand All @@ -10,6 +14,16 @@
)

from ninjadog.ninjadog import render
from ninjadog.constants import TEMPDIR

settings = {}


def asbool(value):
if isinstance(value, bool):
return value
elif isinstance(value, str):
return value.lower().startswith('t')


class PugTemplateRenderer(Jinja2TemplateRenderer):
Expand All @@ -28,6 +42,29 @@ def __call__(self, value, system):
raise ValueError('renderer was passed non-dictionary '
'as value: %s' % str(ex))
template = self.template_loader()

# cheap hack to be able to alter rendering based on config settings
global settings
# doctor pug static only option
static_only = asbool(settings.get('pug.static_only'))
reload = any(settings.get(val) for val in ('reload_all', 'reload_templates'))

pprint(settings)

if static_only and not reload:
template_path = Path(TEMPDIR, Path(template.filename).name)
if not template_path.exists():
html = render(
template.render(system),
file=template.filename,
context=system,
with_jinja=True
)
template_path.write_text(html)
return html

return template_path.read_text()

return render(
template.render(system),
file=template.filename,
Expand Down Expand Up @@ -61,7 +98,7 @@ def template_loader():
return PugTemplateRenderer(template_loader)


def add_pug_renderer(config, name, settings_prefix='jinja2.', package=None):
def add_pug_renderer(config, name, settings_prefix='pug.', package=None):
"""
This function is added as a method of a :term:`Configurator`, and
should not be called directly. Instead it should be called like so after
Expand All @@ -72,6 +109,11 @@ def add_pug_renderer(config, name, settings_prefix='jinja2.', package=None):
``settings_prefix`` prefix. This renderer will be active for files using
the specified extension ``name``.
"""

# set global settings dictionary
global settings
settings = config.get_settings()

renderer_factory = PugRendererFactory()
config.add_renderer(name, renderer_factory)

Expand Down Expand Up @@ -106,3 +148,7 @@ def register():
def includeme(config):
config.add_directive('add_pug_renderer', add_pug_renderer)
config.add_pug_renderer('.pug')

# start with fresh temporary directory
delete_directory(TEMPDIR, ignore_errors=True)
TEMPDIR.mkdir()
File renamed without changes.
File renamed without changes.
44 changes: 44 additions & 0 deletions tests/pyramid/test_pyramid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from pprint import pprint

from webtest import TestApp as _TestApp

import pytest


@pytest.fixture
def settings():
from textwrap import dedent
from configparser import ConfigParser

config_ini = dedent("""
[app:main]
pyramid.reload_templates = true
pug.static_only = false
""")
config = ConfigParser()
config.read_string(config_ini)
return dict(config.items('app:main'))

@pytest.fixture
def testapp(settings):
from pyramid.config import Configurator
with Configurator(settings={'pug.static_only': True}) as config:
config.include('ninjadog')
config.add_route('home', '/')
config.add_view(
lambda request: {'title': 'title', 'subtitle': 'subtitle', 'content': 'This is a paragraph'},
route_name='home',
renderer='./templates/child.pug',
)
app = config.make_wsgi_app()

#pprint(config.registry.settings)

yield _TestApp(app)


def test_rendering(testapp):
response = testapp.get('/', status=200)
assert b'title' in response.body
assert b'subtitle' in response.body
assert b'This is a paragraph' in response.body
24 changes: 0 additions & 24 deletions tests/test_pyramid.py

This file was deleted.

0 comments on commit 4abed00

Please sign in to comment.